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
34
35
36
37
38
39
40
41
42
# File 'lib/buildbox/cli.rb', line 7

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

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

    opts.on("--help", "You're looking at it.") do
      puts @commands['auth:login']
      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['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['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



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
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/buildbox/cli.rb', line 44

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"
    elsif command == "auth:login"
      if @argv.length == 0
        puts "No api key provided"
        exit 1
      end

      api_key = @argv.first

      begin
        Buildbox::API.new.authenticate(api_key)
        Buildbox.config.update(:api_key => api_key)

        puts "Successfully added your api_key"
        puts "You can now add agents with: buildbox agent:setup [agent_token]"
      rescue
        puts "Could not authenticate your api_key"
        exit 1
      end
    end
  else
    puts global.help
  end
end