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.



16
17
18
19
20
# File 'lib/nexus/config.rb', line 16

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



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

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

Instance Method Details

#always_promptObject



89
90
91
92
93
94
95
96
97
# File 'lib/nexus/config.rb', line 89

def always_prompt
  @conf[ :always_prompt, nil ] = true
  
  config.delete( :token )

  clear_credentials( false )

  @conf.store
end

#always_prompt?Boolean

Returns:

  • (Boolean)


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

def always_prompt?
  @conf[ :always_prompt ]
end

#authorizationObject



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

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



178
179
180
181
182
183
184
185
186
187
# File 'lib/nexus/config.rb', line 178

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



72
73
74
75
# File 'lib/nexus/config.rb', line 72

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

#clear_credentials(store = true) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/nexus/config.rb', line 77

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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/nexus/config.rb', line 99

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



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/nexus/config.rb', line 114

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)


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

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

#new_secrets(new) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/nexus/config.rb', line 127

def new_secrets( new )
  old = @conf.all[ :secrets ]
  if old and new
    FileUtils.mv( old, new )
  end
  if new
    @secr = ConfigFile.new( new )
  end
    
  if new.nil? && old
    @conf.merge!( @secr )
    FileUtils.rm_f( old )
    @secr = nil
  end

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

    @secr.store
  end

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

#password=(pass) ⇒ Object



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

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

#reposObject



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

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

#ssl_verify_modeObject



198
199
200
# File 'lib/nexus/config.rb', line 198

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

#to_sObject



202
203
204
# File 'lib/nexus/config.rb', line 202

def to_s
  config.file
end

#urlObject



189
190
191
# File 'lib/nexus/config.rb', line 189

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

#url=(u) ⇒ Object



193
194
195
196
# File 'lib/nexus/config.rb', line 193

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