Class: GithubControl::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/github-control/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

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_nameObject



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_actionsObject



41
42
43
# File 'lib/github-control/cli.rb', line 41

def available_actions
  Action.set.keys.join(", ")
end

#configObject



54
55
56
# File 'lib/github-control/cli.rb', line 54

def config
  @config ||= YAML.load_file(config_file)[environment]
end

#config_fileObject



58
59
60
61
# File 'lib/github-control/cli.rb', line 58

def config_file
  options[:config] || raise(ProblemWithOptions,
    "You need to provide the path to the YAML filename")
end

#consoleObject



45
46
47
# File 'lib/github-control/cli.rb', line 45

def console
  @console ||= Console.new(config)
end

#create_actionObject



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_actionObject



24
25
26
# File 'lib/github-control/cli.rb', line 24

def current_action
  @current_action ||= create_action
end

#environmentObject



49
50
51
52
# File 'lib/github-control/cli.rb', line 49

def environment
  @env ||= options[:environment] || raise(ProblemWithOptions,
    "Please provide an environment to use")
end

#inspectObject



101
102
103
# File 'lib/github-control/cli.rb', line 101

def inspect
  "#<#{self.class} logged in as #{current_user.name.inspect}>"
end

#option_parserObject



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.banner = "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") {
      options[:debug] = true
      $debug = true
    }

    opts.on("-c filename", "--config filename", "Load the config from this YAML file") do |filename|
      options[:config] = filename
    end

    opts.on("-e ENV", "Access this environment") do |env|
      options[: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

#optionsObject



63
64
65
66
67
68
# File 'lib/github-control/cli.rb', line 63

def options
  @options ||= {
    :debug => false,
    :argv  => @args,
  }
end

#runObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/github-control/cli.rb', line 13

def run
  current_action.add_options(option_parser)
  option_parser.parse!(@args)
  current_action.call
rescue ProblemWithOptions, OptionParser::ParseError => e
  $stderr.puts e.message
  $stderr.puts
  $stderr.puts option_parser
  exit 2
end