Class: Bosh::Cli::Config
Constant Summary collapse
- VALID_ID =
/^[-a-z0-9_.]+$/i
Class Attribute Summary collapse
-
.colorize ⇒ Boolean
Should CLI output be colorized?.
-
.commands ⇒ Hash<String,Bosh::Cli::CommandDefinition>
readonly
Available commands.
-
.interactive ⇒ Boolean
Is CLI being used interactively?.
-
.max_parallel_downloads ⇒ Integer
CLI max parallel downloads.
-
.output ⇒ IO
Where output goes.
-
.poll_interval ⇒ Integer
CLI polling interval.
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
Class Method Summary collapse
-
.register_command(command) ⇒ void
Register command with BOSH CLI.
- .use_color? ⇒ Boolean
Instance Method Summary collapse
-
#access_token(target) ⇒ String
Token associated with target.
- #aliases(category) ⇒ Object
- #ca_cert(for_target = nil) ⇒ Object
-
#credentials_for(target) ⇒ Hash
Director credentials.
-
#deployment ⇒ String?
Read the deployment configuration.
-
#initialize(filename, work_dir = Dir.pwd) ⇒ Config
constructor
A new instance of Config.
-
#is_old_deployment_config? ⇒ Boolean
Deployment used to be a string that was only stored for your current target.
-
#max_parallel_downloads ⇒ Integer
Read the max parallel downloads configuration.
-
#password(target) ⇒ String
Password associated with target.
- #read(attr, try_local_first = true) ⇒ Object
-
#refresh_token(target) ⇒ String
Refresh token associated with target.
- #release ⇒ Object
- #release=(value) ⇒ Object
- #resolve_alias(category, alias_name) ⇒ Object
- #save ⇒ Object
- #save_ca_cert_path(cert_path, for_target = nil) ⇒ Object
- #set_alias(category, alias_name, value) ⇒ Object
- #set_credentials(target, credentials) ⇒ Object
-
#set_deployment(deployment_file_path) ⇒ Object
Sets the deployment file for the current target.
- #target ⇒ Object
- #target=(value) ⇒ Object
- #target_name ⇒ Object
- #target_name=(value) ⇒ Object
- #target_uuid ⇒ Object
- #target_uuid=(value) ⇒ Object
- #target_version ⇒ Object
- #target_version=(value) ⇒ Object
-
#username(target) ⇒ String
Username associated with target.
- #write(attr, value) ⇒ Object
- #write_global(attr, value) ⇒ Object
Constructor Details
#initialize(filename, work_dir = Dir.pwd) ⇒ Config
Returns a new instance of Config.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/cli/config.rb', line 50 def initialize(filename, work_dir = Dir.pwd) @filename = File.(filename || Bosh::Cli::DEFAULT_CONFIG_PATH) @work_dir = work_dir unless File.exists?(@filename) File.open(@filename, "w") { |f| Psych.dump({}, f) } File.chmod(0600, @filename) end @config_file = load_yaml_file(@filename, nil) unless @config_file.is_a?(Hash) @config_file = {} # Just ignore it if it's malformed end rescue SystemCallError => e raise ConfigError, "Cannot read config file: #{e.}" end |
Class Attribute Details
.colorize ⇒ Boolean
Returns Should CLI output be colorized?.
12 13 14 |
# File 'lib/cli/config.rb', line 12 def colorize @colorize end |
.commands ⇒ Hash<String,Bosh::Cli::CommandDefinition> (readonly)
Returns Available commands.
9 10 11 |
# File 'lib/cli/config.rb', line 9 def commands @commands end |
.interactive ⇒ Boolean
Returns Is CLI being used interactively?.
18 19 20 |
# File 'lib/cli/config.rb', line 18 def interactive @interactive end |
.max_parallel_downloads ⇒ Integer
Returns CLI max parallel downloads.
24 25 26 |
# File 'lib/cli/config.rb', line 24 def max_parallel_downloads @max_parallel_downloads end |
.output ⇒ IO
Returns Where output goes.
15 16 17 |
# File 'lib/cli/config.rb', line 15 def output @output end |
.poll_interval ⇒ Integer
Returns CLI polling interval.
21 22 23 |
# File 'lib/cli/config.rb', line 21 def poll_interval @poll_interval end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
273 274 275 |
# File 'lib/cli/config.rb', line 273 def filename @filename end |
Class Method Details
.register_command(command) ⇒ void
This method returns an undefined value.
Register command with BOSH CLI
35 36 37 38 39 40 |
# File 'lib/cli/config.rb', line 35 def self.register_command(command) if @commands.has_key?(command.usage) raise CliError, "Duplicate command '#{command.usage}'" end @commands[command.usage] = command end |
.use_color? ⇒ Boolean
42 43 44 45 46 47 48 |
# File 'lib/cli/config.rb', line 42 def self.use_color? # colorization explicitly enabled, or output is tty return false if Bosh::Cli::Config.colorize == false # colorization explicitly enabled, or output is tty Bosh::Cli::Config.colorize || Bosh::Cli::Config.output.tty? end |
Instance Method Details
#access_token(target) ⇒ String
Returns Token associated with target.
128 129 130 |
# File 'lib/cli/config.rb', line 128 def access_token(target) credentials_for(target)["access_token"] end |
#aliases(category) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/cli/config.rb', line 92 def aliases(category) if @config_file.has_key?("aliases") && @config_file["aliases"].is_a?(Hash) @config_file["aliases"][category.to_s] else nil end end |
#ca_cert(for_target = nil) ⇒ Object
218 219 220 221 222 223 224 225 226 |
# File 'lib/cli/config.rb', line 218 def ca_cert(for_target=nil) if for_target return @config_file.fetch('ca_cert', {}).fetch(for_target, nil) end return nil if target.nil? @config_file.fetch('ca_cert', {}).fetch(target, nil) end |
#credentials_for(target) ⇒ Hash
Returns Director credentials.
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/cli/config.rb', line 70 def credentials_for(target) if @config_file["auth"].is_a?(Hash) && @config_file["auth"][target] @config_file["auth"][target] else { "username" => nil, "password" => nil } end end |
#deployment ⇒ String?
Read the deployment configuration. Return the deployment for the current target.
152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/cli/config.rb', line 152 def deployment return nil if target.nil? if @config_file.has_key?("deployment") if is_old_deployment_config? set_deployment(@config_file["deployment"]) save end if @config_file["deployment"].is_a?(Hash) return @config_file["deployment"][target] end end end |
#is_old_deployment_config? ⇒ Boolean
Deployment used to be a string that was only stored for your current target. As soon as you switched targets, the deployment was erased. If the user has the old config we convert it to the new config.
144 145 146 |
# File 'lib/cli/config.rb', line 144 def is_old_deployment_config? @config_file["deployment"].is_a?(String) end |
#max_parallel_downloads ⇒ Integer
Read the max parallel downloads configuration.
241 242 243 |
# File 'lib/cli/config.rb', line 241 def max_parallel_downloads self.class.max_parallel_downloads || @config_file.fetch("max_parallel_downloads", 1) end |
#password(target) ⇒ String
Returns Password associated with target.
122 123 124 |
# File 'lib/cli/config.rb', line 122 def password(target) credentials_for(target)["password"] end |
#read(attr, try_local_first = true) ⇒ Object
245 246 247 248 249 250 251 252 253 |
# File 'lib/cli/config.rb', line 245 def read(attr, try_local_first = true) attr = attr.to_s if try_local_first && @config_file[@work_dir].is_a?(Hash) && @config_file[@work_dir].has_key?(attr) @config_file[@work_dir][attr] else @config_file[attr] end end |
#refresh_token(target) ⇒ String
Returns Refresh token associated with target.
134 135 136 |
# File 'lib/cli/config.rb', line 134 def refresh_token(target) credentials_for(target)["refresh_token"] end |
#release ⇒ Object
202 203 204 |
# File 'lib/cli/config.rb', line 202 def release read(:release, false) end |
#release=(value) ⇒ Object
206 207 208 |
# File 'lib/cli/config.rb', line 206 def release=(value) write_global(:release, value) end |
#resolve_alias(category, alias_name) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/cli/config.rb', line 100 def resolve_alias(category, alias_name) category = category.to_s if @config_file.has_key?("aliases") && @config_file["aliases"].is_a?(Hash) && @config_file["aliases"].has_key?(category) && @config_file["aliases"][category].is_a?(Hash) && !@config_file["aliases"][category][alias_name].blank? @config_file["aliases"][category][alias_name].to_s else nil end end |
#save ⇒ Object
264 265 266 267 268 269 270 271 |
# File 'lib/cli/config.rb', line 264 def save File.open(@filename, "w") do |f| Psych.dump(@config_file, f) end rescue SystemCallError => e raise ConfigError, e. end |
#save_ca_cert_path(cert_path, for_target = nil) ⇒ Object
229 230 231 232 233 234 235 236 |
# File 'lib/cli/config.rb', line 229 def save_ca_cert_path(cert_path, for_target=nil) = cert_path ? File.(cert_path) : nil cert_target = for_target || target @config_file['ca_cert'] ||= {} @config_file['ca_cert'][cert_target] = end |
#set_alias(category, alias_name, value) ⇒ Object
86 87 88 89 90 |
# File 'lib/cli/config.rb', line 86 def set_alias(category, alias_name, value) @config_file["aliases"] ||= {} @config_file["aliases"][category.to_s] ||= {} @config_file["aliases"][category.to_s][alias_name] = value end |
#set_credentials(target, credentials) ⇒ Object
81 82 83 84 |
# File 'lib/cli/config.rb', line 81 def set_credentials(target, credentials) @config_file["auth"] ||= {} @config_file["auth"][target] = credentials end |
#set_deployment(deployment_file_path) ⇒ Object
Sets the deployment file for the current target. If the deployment is the old deployment configuration, it will turn it into the format.
171 172 173 174 175 176 |
# File 'lib/cli/config.rb', line 171 def set_deployment(deployment_file_path) raise MissingTarget, "Must have a target set" if target.nil? @config_file["deployment"] = {} if is_old_deployment_config? @config_file["deployment"] ||= {} @config_file["deployment"][target] = deployment_file_path end |
#target ⇒ Object
178 179 180 |
# File 'lib/cli/config.rb', line 178 def target read(:target, false) end |
#target=(value) ⇒ Object
182 183 184 |
# File 'lib/cli/config.rb', line 182 def target=(value) write_global(:target, value) end |
#target_name ⇒ Object
186 187 188 |
# File 'lib/cli/config.rb', line 186 def target_name read(:target_name, false) end |
#target_name=(value) ⇒ Object
190 191 192 |
# File 'lib/cli/config.rb', line 190 def target_name=(value) write_global(:target_name, value) end |
#target_uuid ⇒ Object
210 211 212 |
# File 'lib/cli/config.rb', line 210 def target_uuid read(:target_uuid, false) end |
#target_uuid=(value) ⇒ Object
214 215 216 |
# File 'lib/cli/config.rb', line 214 def target_uuid=(value) write_global(:target_uuid, value) end |
#target_version ⇒ Object
194 195 196 |
# File 'lib/cli/config.rb', line 194 def target_version read(:target_version, false) end |
#target_version=(value) ⇒ Object
198 199 200 |
# File 'lib/cli/config.rb', line 198 def target_version=(value) write_global(:target_version, value) end |
#username(target) ⇒ String
Returns Username associated with target.
116 117 118 |
# File 'lib/cli/config.rb', line 116 def username(target) credentials_for(target)["username"] end |
#write(attr, value) ⇒ Object
255 256 257 258 |
# File 'lib/cli/config.rb', line 255 def write(attr, value) @config_file[@work_dir] ||= {} @config_file[@work_dir][attr.to_s] = value end |
#write_global(attr, value) ⇒ Object
260 261 262 |
# File 'lib/cli/config.rb', line 260 def write_global(attr, value) @config_file[attr.to_s] = value end |