class Environment

Public Instance Methods

add(env = options[:environment]) click to toggle source
   # File lib/cloudstack-cli/commands/environment.rb
21 def add(env = options[:environment])
22   config = {}
23   unless options[:url]
24     say "Add a new environment...", :green
25     if env
26       say "Environment name: #{env}"
27     else
28       env = ask("Environment name:", :magenta)
29     end
30     say "What's the URL of your Cloudstack API?", :yellow
31     say "Example: https://my-cloudstack-service/client/api/", :green
32     config[:url] = ask("URL:", :magenta)
33   end
34 
35   unless options[:api_key]
36     config[:api_key] = ask("API Key:", :magenta)
37   end
38 
39   unless options[:secret_key]
40     config[:secret_key] = ask("Secret Key:", :magenta)
41   end
42 
43   if env
44     config = {env => config}
45     config[:default] = env if options[:default]
46   end
47 
48   if File.exists? options[:config_file]
49     old_config = parse_config_file
50     if !env || old_config.has_key?(env)
51       say "This environment already exists!", :red
52       exit unless yes?("Do you want to override your settings? [y/N]", :yellow)
53     end
54     config = old_config.merge(config)
55   else
56     newfile = true
57     config[:default] = env if env
58   end
59 
60   write_config_file(config)
61   if newfile
62     say "OK. Created configuration file at #{options[:config_file]}.", :green
63   else
64     say "Added environment #{env}", :green
65   end
66 end
default(env = nil) click to toggle source
    # File lib/cloudstack-cli/commands/environment.rb
 94 def default(env = nil)
 95   config = parse_config_file
 96 
 97   unless env
 98     default_env = config[:default] || '-'
 99     say "The current default environment is \"#{default_env}\""
100     exit 0
101   end
102 
103   if env == '-' && config.key?(:url)
104     config.delete :default
105   else
106     unless config.has_key?(env)
107       say "Environment #{env} does not exist.", :red
108       exit 1
109     end
110     config[:default] = env
111   end
112 
113   write_config_file(config)
114   say "Default environment set to #{env}."
115 end
delete(env) click to toggle source
   # File lib/cloudstack-cli/commands/environment.rb
69 def delete(env)
70   config = parse_config_file
71   if env == '-'
72     config.delete(:url)
73     config.delete(:api_key)
74     config.delete(:secret_key)
75     # check if the config file is empty, delete it if true
76     if config.keys.select { |key| !key.is_a? Symbol}.size == 0
77       exit unless yes?("Do you really want to delete environment #{env}? [y/N]", :yellow)
78       File.delete(options[:config_file])
79       say "OK.", :green
80       exit
81     end
82   elsif config.delete(env)
83   else
84     say "Environment #{env} does not exist.", :red
85     exit 1
86   end
87   exit unless yes?("Do you really want to delete environment #{env}? [y/N]", :yellow)
88   config.delete :default if config[:default] == env
89   write_config_file(config)
90   say "OK.", :green
91 end
list() click to toggle source
   # File lib/cloudstack-cli/commands/environment.rb
 4 def list
 5   config = parse_config_file
 6   table = [%w(Name URL Default)]
 7   table << ['-', config[:url], !config[:default]] if config.key?(:url)
 8   config.each_key do |key|
 9     unless key.class == Symbol
10       table << [key, config[key][:url], key == config[:default]]
11     end
12   end
13   print_table table
14 end
parse_config_file() click to toggle source
    # File lib/cloudstack-cli/commands/environment.rb
119 def parse_config_file
120   if File.exists? options[:config_file]
121     begin
122       return YAML::load(IO.read(options[:config_file]))
123     rescue
124       say "Error loading configuration from file #{options[:config_file]}.", :red
125       exit 1
126     end
127   else
128     say "Can't load configuration from file #{options[:config_file]}.", :red
129     exit 1
130   end
131 end
write_config_file(config) click to toggle source
    # File lib/cloudstack-cli/commands/environment.rb
133 def write_config_file(config)
134   begin
135     return File.open(options[:config_file], 'w+') {|f| f.write(config.to_yaml) }
136   rescue
137     say "Can't open configuration file #{options[:config_file]} for writing.", :red
138     exit 1
139   end
140 end