Module: Forj::Settings

Defined in:
lib/forj-settings.rb,
lib/forj-settings.rb,
lib/forj-settings.rb,
lib/forj-settings.rb,
lib/forj-settings.rb

Overview

This module helps you to setup your forge’s account

Class Method Summary collapse

Class Method Details

._no_update?(key_to_set, old_value, key_value, config) ⇒ Boolean

Returns:

  • (Boolean)


264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/forj-settings.rb', line 264

def self._no_update?(key_to_set, old_value, key_value, config)
  if key_value == '' && config.where?(key_to_set)[0] != 'local'
    puts format('%-15s: No update', key_to_set)
    return true
  end

  if old_value == key_value
    puts format('%-15s: No update', key_to_set)
    return true
  end
  false
end

.account_get(oConfig, account_name, key) ⇒ Object



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/forj-settings.rb', line 404

def self.(oConfig, , key)
  PrcLib.fatal(1, "Unable to load account '%s'. Not found.",
               ) unless oConfig.ac_load 
  Forj::CloudConnection.connect(oConfig)

  if oConfig.where?(key)
    puts format(
      "%s: '%s'",
      oConfig.where?(key)[0],
      oConfig.get(key)
    )
  elsif oConfig.where?(key.parameterize.underscore.to_sym)
    key_symb = key.parameterize.underscore.to_sym
    puts format(
      "%s: '%s'",
      oConfig.where?(key_symb)[0],
      oConfig.get(key_symb)
    )
  else
    PrcLib.message("key '%s' not found", key)
  end
end

.account_get_all(oConfig, account_name) ⇒ Object



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/forj-settings.rb', line 321

def self.(oConfig, )
  PrcLib.fatal(1, "Unable to load account '%s'. Not found.",
               ) unless oConfig.ac_load 
  Forj::CloudConnection.connect(oConfig)

  puts format(
    'legend: default = Application defaults, local = Local' \
      " default config, %s = '%s' account config\n\n",
    ,
    )

  puts format(
    '%s %-15s(%-7s) %-12s:' + "\n" + '--------------------------' \
      '--------------', 'U',
    'key', 'origin',
    'section name'
  )

  (oConfig, )

  puts "\nOn values identified by '+' you can:"

  puts format(
    'Use `forj set <key>=<value> -a %s` to update account data.',
    
  )
  puts format(
    'Or  `forj set <key>= -a %s`        '\
      'to restore key default value.',
    
  )
end

.account_set(account_name, *p) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/forj-settings.rb', line 151

def self.(, *p)
  config = Lorj::Account.new

  b_dirty = false

  config.ac_load 
  Forj::CloudConnection.connect(config)

  p.flatten!
  p.each do |key_val|
    mkey_val = valid_key_value?(key_val)

    key_to_set = mkey_val[1]
    key_value = mkey_val[2]

    (config, , key_to_set)

    section, key_to_set = Lorj.data.first_section(key_to_set)
    full_key = format('%s/%s', section, key_to_set)

    old_value = config.get(key_to_set)
    s_bef = format_old_key(config, old_value, key_to_set)

    if old_value == key_value
      puts format('%-25s: No update', full_key)
      next
    end
    b_dirty = true

    if key_value == ''
      config.del(key_to_set, :name => 'account')
    else
      config.set(key_to_set, key_value, :name => 'account')
    end

    s_aft = format_new_key(config, key_to_set)

    puts format(
      '%-25s: %s => %s',
      full_key,
      s_bef,
      ANSI.bold + s_aft + ANSI.clear
    )
  end
  config.ac_save if b_dirty
end

.account_show_all(account_name) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/forj-settings.rb', line 58

def self.()
  config = Lorj::Account.new

  config.ac_load 
  Forj::CloudConnection.connect(config)

  puts format(
    "List of account settings for provider '%s': ",
    config.get(:provider)
  )
  puts format(
    "%-15s %-12s :\n------------------------------",
    'key',
    'section name'
  )

  config.meta_each do |section, found_key, hValue|
    next if hValue.rh_get(:readonly) || hValue.rh_get(:get) == false
    s_desc = hValue.rh_get(:desc)
    puts format('%-15s %-12s : %s', found_key, section, s_desc)
  end
  puts format(
    "\nUse `forj set KeyName=Value -a %s` to set one.",
    []
  )
  puts format(
    'Use `forj get -a %s`               to check current values.',
    
  )
end

.common_options(options) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/forj-settings.rb', line 22

def self.common_options(options)
  opts = options.to_hash
  if opts.rh_key_to_symbol?
    opts = opts.rh_key_to_symbol
    PrcLib.debug('Thor options converted to use Symbol instead of String '\
                 'in keys')
  end
  PrcLib.level = Logger::INFO
  PrcLib.level = Logger::ERROR if opts[:real_quiet]
  PrcLib.level = Logger::WARN if opts[:quiet]
  PrcLib.level = Logger::INFO if opts[:verbose]
  PrcLib.level = Logger::DEBUG if opts[:debug]
  unless opts[:lorj_debug].nil?
    PrcLib.core_level = opts[:lorj_debug].to_i
    PrcLib.level = Logger::DEBUG
  end
  latest_version?(opts[:account_name]) if opts[:account_name]

  opts
end

.config_get(oConfig, key) ⇒ Object



427
428
429
430
431
432
433
434
# File 'lib/forj-settings.rb', line 427

def self.config_get(oConfig, key)
  where = oConfig.where?(key)
  if where
    puts format("%s:'%s'", where[0], oConfig[key])
  else
    PrcLib.message("key '%s' not found", key)
  end
end

.config_get_all(oConfig) ⇒ Object



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/forj-settings.rb', line 357

def self.config_get_all(oConfig)
  puts 'legend: default = Application defaults, local = Local default' \
       " config\n\n"
  puts format("%s %-19s(%-7s) %-12s:\n"\
              '----------------------------------------',
              'U',  'key', 'origin', 'section name')

  a_processes = [{ :process_module => :cloud },
                 { :process_module => :forj_core }]

  # Loading CloudCore embedding provider controller + its process.
  Lorj::Core.new(oConfig, a_processes)

  oConfig.meta_each do |section, found_key, hValue|
    next if hValue.rh_get(:get) == false
    s_upd_msg = '+'
    s_upd_msg = ' ' if hValue.rh_get(:readonly)
    found_key = hValue.rh_get(:default) if hValue.rh_exist?(:default)

    where = oConfig.where?(found_key, :section => section)
    if where
      where = where[0]
      highlight = ''
      highlight = ANSI.bold + ANSI.yellow if where == 'local'
      puts format(
        "%s %-19s(%s%-7s%s) %-12s: '%s'",
        s_upd_msg,
        found_key,
        highlight,
        where,
        ANSI.clear,
        section,
        oConfig.get(section.to_s + '#' + found_key.to_s)
      )
    else
      puts format(
        '%s %-19s(       ) %-12s: unset',
        s_upd_msg,
        found_key,
        section
      )
    end
  end
  puts "\nUse 'forj set <key>=<value>' to update defaults on values" \
    " identified with '+'"
end

.config_set(*p) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/forj-settings.rb', line 225

def self.config_set(*p)
  config = Lorj::Account.new
  b_dirty = false

  p.flatten!

  p.each do |key_val|
    mkey_val = valid_key_value?(key_val)

    key_to_set = mkey_val[1]
    key_value = mkey_val[2]

    s_bef = 'unset'
    s_aft = 'unset'

    key_to_set = valid_key?(key_to_set)

    old_value = config.get(key_to_set)
    s_bef = format("%s: '%s'", config.where?(key_to_set)[0],
                   config.get(key_to_set)) if config.exist?(key_to_set)

    next if _no_update?(key_to_set, old_value, key_value, config)

    b_dirty = true

    if key_value != ''
      config.local_set(key_to_set, key_value)
    else
      config.local_del(key_to_set)
    end

    s_aft = format("%s: '%s'", config.where?(key_to_set)[0],
                   config.get(key_to_set)) if config.exist?(key_to_set)

    puts format('%-15s: %s => %s', key_to_set, s_bef, ANSI.bold(s_aft))
  end
  config.save_local_config if b_dirty
end

.config_show_allObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/forj-settings.rb', line 89

def self.config_show_all
  config = Lorj::Account.new

  a_processes = [{ :process_module => :cloud },
                 { :process_module => :forj_core }]

  # Loading CloudCore embedding provider controller + its process.
  Lorj::Core.new(config, a_processes)

  puts 'List of available FORJ default settings:'
  puts format("%-15s %-12s :\n------------------------------",
              'key', 'section name')

  config.meta_each do |section, found_key, hValue|
    next if hValue.rh_get(:readonly) || hValue.rh_get(:get) == false
    s_desc = hValue.rh_get(:desc)
    puts format('%-15s %-12s : %s', found_key, section, s_desc)
  end
  puts "\nUse `forj set KeyName=Value` to set one. "
  puts 'Use `forj get`               to get current values. '
end

.format_highlight(account_name, config_where, where_format) ⇒ Object

Function to highlight the data origin

> yellow : account

> white : local config

> normal : all other config



283
284
285
286
287
288
289
290
291
# File 'lib/forj-settings.rb', line 283

def self.format_highlight(, config_where, where_format)
  res = format(where_format, 'default')
  return res unless config_where == 'local' || config_where == 'account'

  res = ANSI.bold(format(where_format, config_where))
  return res if config_where == 'local'

  ANSI.yellow(ANSI.bold(format(where_format, )))
end

.format_new_key(account, key_to_set) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/forj-settings.rb', line 139

def self.format_new_key(, key_to_set)
  s_aft = 'unset'

  s_aft = format(
    "'%s' (%s)",
    .get(key_to_set),
    .where?(key_to_set)[0]
  ) if .exist?(key_to_set)

  s_aft
end

.format_old_key(account, old_value, key_to_set) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/forj-settings.rb', line 127

def self.format_old_key(, old_value, key_to_set)
  s_bef = 'unset'

  s_bef = format(
    "'%s' (%s)",
    old_value,
    .where?(key_to_set)[0]
  ) if .exist?(key_to_set)

  s_bef
end

.get_account_values(account, account_name) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/forj-settings.rb', line 293

def self.(, )
  Lorj.data.meta_each do |section, mykey, hValue|
    next if hValue.rh_get(:get) == false
    config_where = .where?(mykey, :section => section,
                                         :names => .layers -
                                                   %w(runtime))

    s_upd_msg = '+'
    s_upd_msg = ' ' if hValue.rh_get(:readonly)

    if config_where
      where_highlight = format_highlight(,
                                         config_where[0], '%-9s')
      default_key = nil
      if hValue.rh_exist?(:default) && config_where[0] != 'account'
        default_key = format(" (from default key '%s')",
                             hValue.rh_get(:default))
      end
      puts format("%s %-15s(%s) %-12s: '%s'%s",
                  s_upd_msg, mykey, where_highlight, section,
                  .get(mykey, nil, :section => section), default_key)
    else
      puts format('%s %-15s(         ) %-12s: unset',
                  s_upd_msg, mykey, section)
    end
  end
end

.latest_version?(account_name) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/forj-settings.rb', line 43

def self.latest_version?()
  config = Lorj::Account.new(nil, Forj.file_version)

  config.ac_load 

  PrcLib.fatal(1,
               "Your account '%s' is obsolete, use `forj setup`," \
               ' to update it.',
               ) \
               unless config.latest_version?('account')
end

.set_settings(options, p) ⇒ Object



444
445
446
447
448
449
450
# File 'lib/forj-settings.rb', line 444

def self.set_settings(options, p)
  if options[:account_name]
    (options[:account_name], p)
  else
    config_set(p)
  end
end

.show_settings(options) ⇒ Object



436
437
438
439
440
441
442
# File 'lib/forj-settings.rb', line 436

def self.show_settings(options)
  if !options[:account_name]
    config_show_all
  else
    (options[:account_name])
  end
end

.valid_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/forj-settings.rb', line 209

def self.valid_key?(key)
  key = key.to_sym if key.is_a?(String)

  return nil unless key.is_a?(Symbol)

  if Lorj.defaults.get_meta_auto(key).nil?
    PrcLib.warning("key '%s' is not a recognized default key by forj"\
                   ' model. ', key)
  end

  key
end

.valid_key_value?(key_val) ⇒ Boolean

Returns:

  • (Boolean)


198
199
200
201
202
203
204
205
206
207
# File 'lib/forj-settings.rb', line 198

def self.valid_key_value?(key_val)
  mkey_val = key_val.match(/^(.*) *= *(.*)$/)

  PrcLib.fatal(
    1,
    "Syntax error. Please set your value like: 'key=value' and retry."
  ) unless mkey_val

  mkey_val
end

.validate_account_set(account, account_name, account_key) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/forj-settings.rb', line 111

def self.(, , )
  PrcLib.fatal(1,
               "Unable to update protected '%s'. use `forj setup`," \
               ' to update it.',
               ) if .readonly?()

  if .meta_type?() == :default
    PrcLib.fatal(1, "Unable set '%s' value. To update this one, use forj" \
                 ' set %s, WITHOUT -a %s',
                 , , )
  end
end