Class: GithubControl::CLI
- Inherits:
-
Object
- Object
- GithubControl::CLI
- Defined in:
- lib/github-control/cli.rb
Class Method Summary collapse
Instance Method Summary collapse
- #action_name ⇒ Object
- #available_actions ⇒ Object
- #config ⇒ Object
- #config_file ⇒ Object
- #console ⇒ Object
- #create_action ⇒ Object
- #current_action ⇒ Object
- #environment ⇒ Object
-
#initialize(args) ⇒ CLI
constructor
A new instance of CLI.
- #inspect ⇒ Object
- #option_parser ⇒ Object
- #options ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize(args) ⇒ CLI
Returns a new instance of CLI.
9 10 11 |
# File 'lib/github-control/cli.rb', line 9 def initialize(args) @args = args end |
Class Method Details
.execute(args) ⇒ Object
5 6 7 |
# File 'lib/github-control/cli.rb', line 5 def self.execute(args) new(args).run end |
Instance Method Details
#action_name ⇒ Object
36 37 38 39 |
# File 'lib/github-control/cli.rb', line 36 def action_name @action_name ||= @args.shift || raise(ProblemWithOptions, "Please provide an action to run\nChoose one of #{available_actions}") end |
#available_actions ⇒ Object
41 42 43 |
# File 'lib/github-control/cli.rb', line 41 def available_actions Action.set.keys.join(", ") end |
#config ⇒ Object
54 55 56 |
# File 'lib/github-control/cli.rb', line 54 def config @config ||= YAML.load_file(config_file)[environment] end |
#config_file ⇒ Object
58 59 60 61 |
# File 'lib/github-control/cli.rb', line 58 def config_file [:config] || raise(ProblemWithOptions, "You need to provide the path to the YAML filename") end |
#console ⇒ Object
45 46 47 |
# File 'lib/github-control/cli.rb', line 45 def console @console ||= Console.new(config) end |
#create_action ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/github-control/cli.rb', line 28 def create_action if action = Action.set[action_name] action.new(self) else raise ProblemWithOptions, "#{action_name} is not a valid action\nChoose one of #{available_actions}" end end |
#current_action ⇒ Object
24 25 26 |
# File 'lib/github-control/cli.rb', line 24 def current_action @current_action ||= create_action end |
#environment ⇒ Object
49 50 51 52 |
# File 'lib/github-control/cli.rb', line 49 def environment @env ||= [:environment] || raise(ProblemWithOptions, "Please provide an environment to use") end |
#inspect ⇒ Object
101 102 103 |
# File 'lib/github-control/cli.rb', line 101 def inspect "#<#{self.class} logged in as #{current_user.name.inspect}>" end |
#option_parser ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/github-control/cli.rb', line 70 def option_parser @option_parser ||= OptionParser.new do |opts| opts. = "Usage: github-control #{@action_name || '[action]'} [options] ..." opts.on_tail("-h", "--help", "Show this message") do $stderr.puts opts exit end opts.on("--debug", "Display debugging information") { [:debug] = true $debug = true } opts.on("-c filename", "--config filename", "Load the config from this YAML file") do |filename| [:config] = filename end opts.on("-e ENV", "Access this environment") do |env| [:environment] = env end opts.on("-v", "--version", "Display the github version, and exit.") do puts "Github Control version #{GithubControl::VERSION}" exit end opts.separator "" end end |
#options ⇒ Object
63 64 65 66 67 68 |
# File 'lib/github-control/cli.rb', line 63 def @options ||= { :debug => false, :argv => @args, } end |
#run ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/github-control/cli.rb', line 13 def run current_action.(option_parser) option_parser.parse!(@args) current_action.call rescue ProblemWithOptions, OptionParser::ParseError => e $stderr.puts e. $stderr.puts $stderr.puts option_parser exit 2 end |