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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Config.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cli/config.rb', line 14

def initialize(filename, work_dir = Dir.pwd)
  @filename = File.expand_path(filename)
  @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

.cacheObject

Returns the value of attribute cache.



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

def cache
  @cache
end

.colorizeObject

Returns the value of attribute colorize.



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

def colorize
  @colorize
end

.interactiveObject

Returns the value of attribute interactive.



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

def interactive
  @interactive
end

.outputObject

Returns the value of attribute output.



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

def output
  @output
end

Instance Method Details

#authObject



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

def auth
  if @config_file.has_key?("auth") && @config_file["auth"].is_a?(Hash)
    @config_file["auth"][target]
  else
    nil
  end
end

#deploymentString?

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

Returns:

  • (String?)

    The deployment path for the current target.



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/cli/config.rb', line 89

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.



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

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

#passwordObject



71
72
73
# File 'lib/cli/config.rb', line 71

def password
  auth ? auth["password"] : nil
end

#read(attr, try_local_first = true) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/cli/config.rb', line 126

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



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cli/config.rb', line 53

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



145
146
147
148
149
150
151
152
# File 'lib/cli/config.rb', line 145

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



47
48
49
50
51
# File 'lib/cli/config.rb', line 47

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



41
42
43
44
45
# File 'lib/cli/config.rb', line 41

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:



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

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

#usernameObject



67
68
69
# File 'lib/cli/config.rb', line 67

def username
  auth ? auth["username"] : nil
end

#write(attr, value) ⇒ Object



136
137
138
139
# File 'lib/cli/config.rb', line 136

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

#write_global(attr, value) ⇒ Object



141
142
143
# File 'lib/cli/config.rb', line 141

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