class Bosh::Cli::Config

Constants

VALID_ID

Attributes

colorize[RW]

@return [Boolean] Should CLI output be colorized?

commands[R]

@return [Hash<String,Bosh::Cli::CommandDefinition>] Available commands

interactive[RW]

@return [Boolean] Is CLI being used interactively?

max_parallel_downloads[RW]

@return [Integer] CLI max parallel downloads

output[RW]

@return [IO] Where output goes

poll_interval[RW]

@return [Integer] CLI polling interval

filename[R]

Public Class Methods

new(filename, work_dir = Dir.pwd) click to toggle source
# File lib/cli/config.rb, line 50
def initialize(filename, work_dir = Dir.pwd)
  @filename = File.expand_path(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.message}"
end
register_command(command) click to toggle source

Register command with BOSH CLI @param [Bosh::Cli::CommandDefinition] command @return [void]

# 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?() click to toggle source
# 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

Public Instance Methods

access_token(target) click to toggle source

@param [String] target Target director url @return [String] Token associated with target

# File lib/cli/config.rb, line 128
def access_token(target)
  credentials_for(target)["access_token"]
end
aliases(category) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

@return [Hash] Director credentials

# 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() click to toggle source

Read the deployment configuration. Return the deployment for the current target.

@return [String?] The deployment path for the current target.

# 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?() click to toggle source

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.

@return [Boolean] Whether config is using the old deployment format.

# File lib/cli/config.rb, line 144
def is_old_deployment_config?
  @config_file["deployment"].is_a?(String)
end
max_parallel_downloads() click to toggle source

Read the max parallel downloads configuration.

@return [Integer] The maximum number of parallel downloads

# 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) click to toggle source

@param [String] target Target director url @return [String] Password associated with target

# File lib/cli/config.rb, line 122
def password(target)
  credentials_for(target)["password"]
end
read(attr, try_local_first = true) click to toggle source
# 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) click to toggle source

@param [String] target Target director url @return [String] Refresh token associated with target

# File lib/cli/config.rb, line 134
def refresh_token(target)
  credentials_for(target)["refresh_token"]
end
release() click to toggle source
# File lib/cli/config.rb, line 202
def release
  read(:release, false)
end
release=(value) click to toggle source
# File lib/cli/config.rb, line 206
def release=(value)
  write_global(:release, value)
end
resolve_alias(category, alias_name) click to toggle source
# 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() click to toggle source
# 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.message
end
save_ca_cert_path(cert_path, for_target=nil) click to toggle source
# File lib/cli/config.rb, line 229
def save_ca_cert_path(cert_path, for_target=nil)
  expanded_path = cert_path ? File.expand_path(cert_path) : nil
  cert_target = for_target || target
  @config_file['ca_cert'] ||= {}
  @config_file['ca_cert'][cert_target] = expanded_path

  expanded_path
end
set_alias(category, alias_name, value) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

Sets the deployment file for the current target. If the deployment is the old deployment configuration, it will turn it into the format.

@raise [MissingTarget] If there is no target set. @param [String] deployment_file_path The string path to the

deployment file.
# 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() click to toggle source
# File lib/cli/config.rb, line 178
def target
  read(:target, false)
end
target=(value) click to toggle source
# File lib/cli/config.rb, line 182
def target=(value)
  write_global(:target, value)
end
target_name() click to toggle source
# File lib/cli/config.rb, line 186
def target_name
  read(:target_name, false)
end
target_name=(value) click to toggle source
# File lib/cli/config.rb, line 190
def target_name=(value)
  write_global(:target_name, value)
end
target_uuid() click to toggle source
# File lib/cli/config.rb, line 210
def target_uuid
  read(:target_uuid, false)
end
target_uuid=(value) click to toggle source
# File lib/cli/config.rb, line 214
def target_uuid=(value)
  write_global(:target_uuid, value)
end
target_version() click to toggle source
# File lib/cli/config.rb, line 194
def target_version
  read(:target_version, false)
end
target_version=(value) click to toggle source
# File lib/cli/config.rb, line 198
def target_version=(value)
  write_global(:target_version, value)
end
username(target) click to toggle source

@param [String] target Target director url @return [String] Username associated with target

# File lib/cli/config.rb, line 116
def username(target)
  credentials_for(target)["username"]
end
write(attr, value) click to toggle source
# 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) click to toggle source
# File lib/cli/config.rb, line 260
def write_global(attr, value)
  @config_file[attr.to_s] = value
end