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



47
48
49
# File 'lib/github-control/cli.rb', line 47

def action_name
  @action_name ||= @args.shift || raise(ProblemWithOptions, "Please provide an action to run")
end

#configObject



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

def config
  @config ||= YAML.load_file(config_filename)
end

#config_filenameObject



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

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

#consoleObject



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

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

#create_actionObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/github-control/cli.rb', line 30

def create_action
  case action_name
  when "list"
    Actions::Repositories.new(self)
  when "shell"
    Actions::Shell.new(self)
  when "collab"
    Actions::Collaborators.new(self)
  when "add_collab"
    Actions::AddCollaborators.new(self)
  when "remove_collab"
    Actions::RemoveCollaborators.new(self)
  else
    raise ProblemWithOptions, "#{action_name} is not a valid action"
  end
end

#current_actionObject



26
27
28
# File 'lib/github-control/cli.rb', line 26

def current_action
  @current_action ||= create_action
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



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 74

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] = filename
    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



67
68
69
70
71
72
# File 'lib/github-control/cli.rb', line 67

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

#runObject



13
14
15
16
17
18
19
20
21
22
23
24
# 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
  puts e.backtrace
  $stderr.puts
  $stderr.puts e.message
  $stderr.puts
  $stderr.puts option_parser
  exit 2
end

#user_configObject



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

def user_config
  config["user"] || raise(ProblemWithOptions, "You need to provide user data in the YAML file")
end