Class: Bosh::Cli::Config

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

Constant Summary collapse

VALID_ID =
/^[-a-z0-9_.]+$/i

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, work_dir = Dir.pwd) ⇒ Config

Returns a new instance of Config.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cli/config.rb', line 40

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| YAML.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

Class Attribute Details

.cacheBosh::Cli::Cache

Returns CLI cache (to save task logs etc.).

Returns:



21
22
23
# File 'lib/cli/config.rb', line 21

def cache
  @cache
end

.colorizeBoolean

Returns Should CLI output be colorized?.

Returns:

  • (Boolean)

    Should CLI output be colorized?



12
13
14
# File 'lib/cli/config.rb', line 12

def colorize
  @colorize
end

.commandsHash<String,Bosh::Cli::CommandDefinition> (readonly)

Returns Available commands.

Returns:

  • (Hash<String,Bosh::Cli::CommandDefinition>)

    Available commands



9
10
11
# File 'lib/cli/config.rb', line 9

def commands
  @commands
end

.interactiveBoolean

Returns Is CLI being used interactively?.

Returns:

  • (Boolean)

    Is CLI being used interactively?



18
19
20
# File 'lib/cli/config.rb', line 18

def interactive
  @interactive
end

.outputIO

Returns Where output goes.

Returns:

  • (IO)

    Where output goes



15
16
17
# File 'lib/cli/config.rb', line 15

def output
  @output
end

Class Method Details

.register_command(command) ⇒ void

This method returns an undefined value.

Register command with BOSH CLI

Parameters:

  • command (Bosh::Cli::CommandDefinition)


33
34
35
36
37
38
# File 'lib/cli/config.rb', line 33

def self.register_command(command)
  if @commands.has_key?(command.usage)
    raise CliError, "Duplicate command `#{command.usage}'"
  end
  @commands[command.usage] = command
end

Instance Method Details

#aliases(category) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/cli/config.rb', line 85

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

#credentials_for(target) ⇒ Hash

Returns Director credentials.

Returns:

  • (Hash)

    Director credentials



60
61
62
63
64
65
66
67
68
69
# File 'lib/cli/config.rb', line 60

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

#deploymentString?

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

Returns:

  • (String?)

    The deployment path for the current target.



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/cli/config.rb', line 133

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.

Returns:

  • (Boolean)

    Whether config is using the old deployment format.



125
126
127
# File 'lib/cli/config.rb', line 125

def is_old_deployment_config?
  @config_file["deployment"].is_a?(String)
end

#password(target) ⇒ String

Returns Password associated with target.

Parameters:

  • target (String)

    Target director url

Returns:

  • (String)

    Password associated with target



115
116
117
# File 'lib/cli/config.rb', line 115

def password(target)
  credentials_for(target)["password"]
end

#read(attr, try_local_first = true) ⇒ Object



170
171
172
173
174
175
176
177
178
# File 'lib/cli/config.rb', line 170

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

#resolve_alias(category, alias_name) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/cli/config.rb', line 93

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

#saveObject



189
190
191
192
193
194
195
196
# File 'lib/cli/config.rb', line 189

def save
  File.open(@filename, "w") do |f|
    YAML.dump(@config_file, f)
  end

rescue SystemCallError => e
  raise ConfigError, e.message
end

#set_alias(category, alias_name, value) ⇒ Object



79
80
81
82
83
# File 'lib/cli/config.rb', line 79

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, username, password) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/cli/config.rb', line 71

def set_credentials(target, username, password)
  @config_file["auth"] ||= {}
  @config_file["auth"][target] = {
    "username" => username,
    "password" => password
  }
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.

Parameters:

  • deployment_file_path (String)

    The string path to the deployment file.

Raises:



152
153
154
155
156
157
# File 'lib/cli/config.rb', line 152

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

#username(target) ⇒ String

Returns Username associated with target.

Parameters:

  • target (String)

    Target director url

Returns:

  • (String)

    Username associated with target



109
110
111
# File 'lib/cli/config.rb', line 109

def username(target)
  credentials_for(target)["username"]
end

#write(attr, value) ⇒ Object



180
181
182
183
# File 'lib/cli/config.rb', line 180

def write(attr, value)
  @config_file[@work_dir] ||= {}
  @config_file[@work_dir][attr.to_s] = value
end

#write_global(attr, value) ⇒ Object



185
186
187
# File 'lib/cli/config.rb', line 185

def write_global(attr, value)
  @config_file[attr.to_s] = value
end