Class: Nexus::Config

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = nil, repo = nil) ⇒ Config

Returns a new instance of Config.



12
13
14
15
16
# File 'lib/nexus/config.rb', line 12

def initialize(file = nil, repo = nil)
  @repo = repo
  @conf = ConfigFile.new(file || self.class.default_file)
  @secr = ConfigFile.new(@conf[:secrets]) if @conf.key? :secrets
end

Class Method Details

.default_fileObject



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

def self.default_file
  File.join(Gem.user_home, '.gem', 'nexus')
end

Instance Method Details

#always_promptObject



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

def always_prompt
  @conf[:always_prompt, nil] = true

  config.delete(:token)

  clear_credentials(false)

  @conf.store
end

#always_prompt?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/nexus/config.rb', line 64

def always_prompt?
  @conf[:always_prompt]
end

#authorizationObject



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/nexus/config.rb', line 155

def authorization
  auth = self[:authorization]
  if @cipher && auth && self[:iv]
    @cipher.iv = self[:iv]
    @cipher.decrypt(auth)
  elsif @cipher && auth
    _authorization = auth
  else
    auth
  end
end

#authorization=(auth) ⇒ Object



167
168
169
170
171
172
173
174
175
176
# File 'lib/nexus/config.rb', line 167

def authorization=(auth)
  if @cipher && auth
    self[:authorization] = @cipher.encrypt(auth)
    self[:iv] = @cipher.iv
  else
    self[:authorization] = auth
  end
  config.store
  auth
end

#clear_always_promptObject



68
69
70
71
# File 'lib/nexus/config.rb', line 68

def clear_always_prompt
  @conf.delete(:always_prompt)
  @conf.store
end

#clear_credentials(store = true) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/nexus/config.rb', line 73

def clear_credentials(store = true)
  secrets = @conf[:secrets]
  if secrets && !config.key?(:token)
    FileUtils.rm_f(secrets)
    @map = @conf
  else
    config.delete :iv, :authorization
  end
  @conf.delete(:secrets)
  @conf.store if store
end

#decrypt_credentialsObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/nexus/config.rb', line 95

def decrypt_credentials
  unless encrypted?
    warn 'not encrypted - nothing to do'
    return
  end
  encrypt_or_decrypt_credentials do |c|
    @cipher.iv = c[:iv]
    c[:authorization] = @cipher.decrypt(c[:authorization])
    c.delete(:iv)
  end
  config.all.delete(:token)
  config.store
  @cipher = nil
end

#encrypt_credentialsObject



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/nexus/config.rb', line 110

def encrypt_credentials
  if encrypted?
    warn 'already encrypted - nothing to do'
    return
  end
  encrypt_or_decrypt_credentials do |c|
    c[:authorization] = @cipher.encrypt(c[:authorization])
    c[:iv] = @cipher.iv
  end
  config.all[:token] = @cipher.token
  config.store
end

#encrypted?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/nexus/config.rb', line 56

def encrypted?
  config.key?(:token)
end

#new_secrets(new) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/nexus/config.rb', line 123

def new_secrets(new)
  old = @conf.all[:secrets]
  FileUtils.mv(old, new) if old && new
  @secr = ConfigFile.new(new) if new

  if new.nil? && old
    @conf.merge!(@secr)
    FileUtils.rm_f(old)
    @secr = nil
  end

  if old.nil? && new
    move_credentials(@conf, @secr)

    @secr.store
  end

  # store the new location
  @conf[:secrets, nil] = new
  @conf.store
end

#password=(pass) ⇒ Object



60
61
62
# File 'lib/nexus/config.rb', line 60

def password=(pass)
  @cipher = Cipher.new(pass, config[:token])
end

#reposObject



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

def repos
  result = @conf.section(:url)
  url = result.delete(:url)
  result['DEFAULT'] = url if url
  result.each_key do |key|
    result[key] = result[key][:url] if key != 'DEFAULT'
  end
  result
end

#ssl_verify_modeObject



187
188
189
# File 'lib/nexus/config.rb', line 187

def ssl_verify_mode
  @conf[:ssl_verify_mode, @repo]
end

#to_sObject



191
192
193
# File 'lib/nexus/config.rb', line 191

def to_s
  config.file
end

#urlObject



178
179
180
# File 'lib/nexus/config.rb', line 178

def url
  @conf[:url, @repo]
end

#url=(repo_url) ⇒ Object



182
183
184
185
# File 'lib/nexus/config.rb', line 182

def url=(repo_url)
  @conf[:url, @repo] = repo_url
  @conf.store
end