-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcli.rb
More file actions
97 lines (82 loc) · 2.93 KB
/
Copy pathcli.rb
File metadata and controls
97 lines (82 loc) · 2.93 KB
1
2
3
4
5
6
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
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
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
module CloudstackCli
class Cli < CloudstackCli::Base
include Thor::Actions
package_name "cloudstack-cli"
class_option :config_file,
default: CloudstackClient::Configuration.locate_config_file ||
File.join(Dir.home, ".cloudstack.yml"),
aliases: '-c',
desc: 'Location of your cloudstack configuration file'
class_option :env,
aliases: '-e',
desc: 'Environment to use'
class_option :debug,
desc: 'Enable debug output',
type: :boolean,
default: false
desc "version", "Print cloudstack-cli version number"
def version
say "cloudstack-cli version #{CloudstackCli::VERSION}"
say " (cloudstack_client version #{CloudstackClient::VERSION})"
end
desc "setup", "Initial configuration of Cloudstack connection settings"
def setup(env = options[:environment])
invoke "environment:add", [env],
:config_file => options[:config_file]
end
desc "completion", "Load the shell scripts for <tab> auto-completion"
option :shell, default: 'bash'
def completion
shell_script = File.join(
File.dirname(__FILE__), '..', '..',
'completions', "cloudstack-cli.#{options[:shell]}"
)
unless File.file? shell_script
say "Specified cloudstack-cli shell auto-completion rules for #{options[:shell]} not found.", :red
exit 1
end
puts File.read shell_script
end
desc "command COMMAND [arg1=val1 arg2=val2...]", "Run a custom api command"
option :format, default: 'yaml',
enum: %w(json yaml), desc: "output format"
option :pretty_print, default: true, type: :boolean,
desc: "pretty print json output"
def command(command, *args)
params = { 'command' => command }
args.each do |arg|
arg = arg.split('=')
params[arg[0]] = arg[1]
end
unless client.api.commands.has_key? command
say "ERROR: ", :red
say "Unknown API command '#{command}'."
exit!
end
unless client.api.all_required_params?(command, params)
raise CloudstackClient::ParameterError, client.api.missing_params_msg(command)
end
data = client.send_request(params)
if options[:format] == 'json'
puts options[:pretty_print] ? JSON.pretty_generate(data) : data.to_json
else
puts data.to_yaml
end
end
# Require and describe subcommands
Dir[File.dirname(__FILE__) + '/commands/*.rb'].each do |command_path|
require command_path
command = File.basename(command_path, ".rb")
class_names = command.split('_').collect(&:capitalize)
desc "#{command} SUBCOMMAND ...ARGS",
"#{class_names.join(' ')} commands"
subcommand command,
Object.const_get(class_names.join)
end
# Additional command maps (aliases)
map %w(-v --version) => :version
map 'env' => :environment
map 'vm' => :virtual_machine
map 'server' => :virtual_machine
end
end