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.



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

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



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

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

Instance Method Details

#always_promptObject



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

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

  clear_credentials( false )

  @conf.store
end

#always_prompt?Boolean

Returns:

  • (Boolean)


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

def always_prompt?
  @conf[ :always_prompt ]
end

#authorizationObject



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/nexus/config.rb', line 161

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



173
174
175
176
177
178
179
180
181
182
# File 'lib/nexus/config.rb', line 173

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



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

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

#clear_credentials(store = true) ⇒ Object



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

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



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

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



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

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)


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

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

#new_secrets(new) ⇒ Object



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

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



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

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

#reposObject



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/nexus/config.rb', line 148

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

#to_sObject



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

def to_s
  config.file
end

#urlObject



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

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

#url=(u) ⇒ Object



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

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