Class: Buildbox::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/buildbox/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/buildbox/cli.rb', line 7

def initialize(argv)
  @argv     = argv
  @commands = {}
  @options  = {}

  @commands['agent:setup'] = OptionParser.new do |opts|
    opts.banner = "Usage: buildbox agent:setup [token]"

    opts.on("--help", "You're looking at it.") do
      puts @commands['agent:setup']
      exit
    end
  end

  @commands['agent:start'] = OptionParser.new do |opts|
    opts.banner = "Usage: buildbox agent:start"

    opts.on("--help", "You're looking at it.") do
      puts @commands['agent:start']
      exit
    end
  end

  @commands['version'] = OptionParser.new do |opts|
    opts.banner = "Usage: buildbox version"
  end
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



5
6
7
# File 'lib/buildbox/cli.rb', line 5

def argv
  @argv
end

Instance Method Details

#parseObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/buildbox/cli.rb', line 35

def parse
  global.order!

  command = @argv.shift

  if command
    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 == "agent:start"
      Buildbox::Server.new.start
    elsif command == "agent:setup"
      if @argv.length == 0
        puts "No token provided"
        exit 1
      end

      access_token = @argv.first
      agent_access_tokens = Buildbox.config.agent_access_tokens
      Buildbox.config.update(:agent_access_tokens => agent_access_tokens << access_token)

      puts "Successfully added agent access token"
      puts "You can now start the agent with: buildbox agent:start."
      puts "If the agent is already running, you'll have to restart it for the new changes to take effect"
    end
  else
    puts global.help
  end
end