Class: NexusCli::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/nexus_cli/configuration.rb

Constant Summary collapse

DEFAULT_FILE =
(ENV['HOME'] ? "~/.nexus_cli" : "/root/.nexus_cli").freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Configuration

Returns a new instance of Configuration.



84
85
86
87
88
89
90
91
92
93
# File 'lib/nexus_cli/configuration.rb', line 84

def initialize(options)
  @url = options[:url] || options['url']
  @repository = options[:repository] || options['repository']
  @username = options[:username] || options['username']
  @password = options[:password] || options['password']

  if @repository.is_a?(String)
    @repository = @repository.gsub(' ', '_')
  end
end

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



82
83
84
# File 'lib/nexus_cli/configuration.rb', line 82

def password
  @password
end

#repositoryObject

Returns the value of attribute repository.



80
81
82
# File 'lib/nexus_cli/configuration.rb', line 80

def repository
  @repository
end

#urlObject

Returns the value of attribute url.



79
80
81
# File 'lib/nexus_cli/configuration.rb', line 79

def url
  @url
end

#usernameObject

Returns the value of attribute username.



81
82
83
# File 'lib/nexus_cli/configuration.rb', line 81

def username
  @username
end

Class Method Details

.file_pathString

The filepath to the nexus cli configuration file

Returns:

  • (String)


11
12
13
# File 'lib/nexus_cli/configuration.rb', line 11

def file_path
  File.expand_path(ENV['NEXUS_CONFIG'] || File.expand_path(DEFAULT_FILE))
end

.from_fileNexusCli::Configuration

Creates a new instance of the Configuration object from the config file



32
33
34
35
36
# File 'lib/nexus_cli/configuration.rb', line 32

def from_file
  config = load_config
  raise MissingSettingsFileException unless config
  new(config)
end

.from_overrides(overrides) ⇒ NexusCli::Configuration

Creates a new instance of the Configuration object based on some overrides

Parameters:

  • overrides (Hash)

Returns:

Raises:



20
21
22
23
24
25
26
# File 'lib/nexus_cli/configuration.rb', line 20

def from_overrides(overrides)
  raise MissingSettingsFileException unless overrides

  configuration = load_config || Hash.new
  configuration.merge!(overrides)
  new(configuration)
end

.validate!(config) ⇒ Object

Validates an instance of the Configuration object and raises when there is an error with it



44
45
46
47
48
# File 'lib/nexus_cli/configuration.rb', line 44

def validate!(config)
  unless config.valid?
    raise InvalidSettingsException.new(config.errors)
  end
end

Instance Method Details

#[](attr) ⇒ Object



95
96
97
# File 'lib/nexus_cli/configuration.rb', line 95

def [](attr)
  self.instance_variable_get('@' + attr.to_s)
end

#[]=(attr, value) ⇒ Object



99
100
101
102
103
104
# File 'lib/nexus_cli/configuration.rb', line 99

def []=(attr, value)
  self.instance_variable_set('@' + attr.to_s, value)
  if @repository.is_a?(String)
    @repository = @repository.gsub(' ', '_')
  end
end

#errorsObject



72
73
74
75
76
77
# File 'lib/nexus_cli/configuration.rb', line 72

def errors
  result = Hash.new
  result[:url] = ["url required"] unless url.is_a?(String) && url.size > 0
  result[:repository] = ["repository required"] unless repository.is_a?(String) && repository.size > 0
  result
end

#valid?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/nexus_cli/configuration.rb', line 68

def valid?
  errors.empty?
end

#validate!Object



64
65
66
# File 'lib/nexus_cli/configuration.rb', line 64

def validate!
  self.class.validate!(self)
end