bin/buildbox in buildbox-0.0.1 vs bin/buildbox in buildbox-0.0.2

- old
+ new

@@ -1,31 +1,99 @@ #!/usr/bin/env ruby dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')) $LOAD_PATH.unshift(dir) unless $LOAD_PATH.include?(dir) -require 'trigger' +require 'buildbox' require 'optparse' -COMMANDS = %w(start stop) +commands = {} +options = {} -options = {}.tap do |options| - OptionParser.new do |opts| - opts.version = Trigger::VERSION - opts.banner = 'Usage: trigger command [options]' +help = <<HELP - opts.separator "" - opts.separator "Options:" + auth # authentication (login, logout) + monitor # monitor builds (start, stop) + version # display version - opts.on("-d", "--daemon", "Runs trigger as a daemon") do |user_agent| - options[:daemon] = true - end +HELP - end.parse! +global = OptionParser.new do |opts| + opts.version = Buildbox::VERSION + opts.banner = 'Usage: buildbox COMMAND [command-specific-actions]' + + opts.separator help end -client = Trigger::Client.new(options) +commands['auth:login'] = OptionParser.new do |opts| + opts.banner = "Usage: buildbox auth:login API_KEY" +end -if command = ARGV[0] - client.public_send(command) +commands['auth:logout'] = OptionParser.new do |opts| + opts.banner = "Usage: buildbox auth:logout" +end + +commands['monitor:start'] = OptionParser.new do |opts| + opts.banner = "Usage: buildbox monitor:start" + + opts.on("-d", "--daemon", "Runs as a daemon") do + options[:daemon] = true + end + + opts.on("--help", "You're looking at it.") do + puts commands['monitor:start'] + exit + end +end + +commands['monitor:stop'] = OptionParser.new do |opts| + opts.banner = "Usage: buildbox monitor:stop" +end + +commands['version'] = OptionParser.new do |opts| + opts.banner = "Usage: buildbox version" +end + +global.order! + +args = ARGV + +if command = args.shift + if commands.has_key?(command) + commands[command].parse! + else + puts "`#{command}` is an unknown command" + exit 1 + end + + if command == "version" + puts Buildbox::VERSION + exit + end + + if command =~ /^monitor/ + client = Buildbox::Client.new(options) + if command == "monitor:start" + client.start + elsif command == "monitor:stop" + client.stop + end + end + + if command =~ /^auth/ + auth = Buildbox::Auth.new + + if command == "auth:login" + api_key = args[0] + + unless api_key + puts commands['auth:login'] + exit + end + + auth.login(:api_key => args[0]) + elsif command == "auth:logout" + auth.logout + end + end else - abort 'No command specified' + puts global.help end