Class: Codebase::CLI

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

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.invoke(*args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/codebase/cli.rb', line 8

def self.invoke(*args)
  command = args.shift
  cmd = self.new
  if command.nil?
    raise Error, "usage: codebase [command] *args"
  elsif cmd.respond_to?(command)
    cmd.send(command, *args)
  else
    raise Error, "Command '#{command}' not found"
  end
rescue Error => e
  $stderr.puts e.message
  Process.exit(1)
rescue ArgumentError
  $stderr.puts "Invalid arguments provided for command ('#{command}')"
  Process.exit(1)
end

Instance Method Details

#deploy(start_ref, end_ref, *options) ⇒ Object

Raises:



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
# File 'lib/codebase/cli.rb', line 47

def deploy(start_ref, end_ref, *options)
  options = options_to_hash(options)

  hash = {
    :start_ref => start_ref,
    :end_ref => end_ref,
    :environment => options['e'] || options['environment'],
    :servers => options['s'] || options['servers'],
    :branch => options['b'] || options['branch']
  }

  host = options['h'] || options['host']
  repo = options['r'] || options['repo']

  raise Error, "You must specify at least one server using the -s or --servers flag" if blank?(hash[:servers])
  raise Error, "You must specify the repo using the -r or --repo flag (as project:repo)" if blank?(repo)
  raise Error, "You must specify the host using the -h or --host flag" if blank?(host)

  project, repo = repo.split(':')

  puts "Sending deployment information to #{host} (project: '#{project}' repo: '#{repo}')"

  puts "   Commits......: #{hash[:start_ref]} ... #{hash[:end_ref]}"
  puts "   Environment..: #{hash[:environment] || '-'}"
  puts "   Branch.......: #{hash[:branch] || '-'}"
  puts "   Server(s)....: #{hash[:servers]}"

  token = Codebase.config.tokens.is_a?(Hash) && Codebase.config.tokens[host]
  if token.nil?
    raise Error, "This account has no token configured locally, use 'codebase token [account] [token]' to configure it"
  end

  puts "   Token........: #{token[0,7]}******"
  hash[:access_token] = token

  Codebase.request("https://#{host}/projects/#{project}/repositories/#{repo}/deployments/add", hash)
  puts "Deployment added successfully"
end

#test(account_name) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/codebase/cli.rb', line 33

def test()
  token = Codebase.config.tokens.is_a?(Hash) && Codebase.config.tokens[]
  unless token
    raise Error, "This account has no token configured locally, use 'codebase token [account] [token]' to configure it"
  end

  begin
    Codebase.request("https://#{}/check_access_token", {:access_token => token})
    puts "OK"
  rescue
    puts "Test failed, are your credentials in ~/.codebase4 correct?"
  end
end

#token(account_name, token) ⇒ Object



26
27
28
29
30
31
# File 'lib/codebase/cli.rb', line 26

def token(, token)
  Codebase.config.set(:tokens, Hash.new) unless Codebase.config.tokens.is_a?(Hash)
  Codebase.config.tokens[] = token
  Codebase.config.save
  puts "Added token for '#{}'"
end