Class: ToolsConfig

Inherits:
Object show all
Includes:
Singleton
Defined in:
lib/lib/config.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ToolsConfig

Returns a new instance of ToolsConfig.



5
6
# File 'lib/lib/config.rb', line 5

def initialize(options = {})
end

Class Method Details

.change_value_in_config(*args) ⇒ Object

Change data in config file in work area

Parameters:

  • args —

    Sequence keys to locate the value in hash



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

def self.change_value_in_config *args
  source = args.extract_first
  value  = args.extract_first
  test_config_type source
  case ToolsUtil.get_variable 'config_file_type'
  when :json
    file = open(source)
    json = file.read
    parsed = JSON.parse(json)
    parsed.nested_set(args, value)
    file.close
    file = open(source, 'w')
    file.write JSON.pretty_generate(parsed)
    file.close
  when :yaml
    file = open(source)
    yaml = file.read
    parsed = YAML.load(yaml)
    parsed.nested_set(args, value)
    file.close
    file = open(source, 'w')
    file.write parsed.to_yaml
    file.close
  end
end

.check_config ⇒ Object



177
178
179
180
181
182
183
# File 'lib/lib/config.rb', line 177

def self.check_config
#    ToolsLog.info "\t\tChecking config file: #{Tools.home+'/.tools/config'}."
  # file = open(Tools.home+'/.tools/config')
  # json = file.read
  # parsed = JSON.parse(json)
  # file.close
end

.config_backup ⇒ Object



194
195
196
197
198
199
# File 'lib/lib/config.rb', line 194

def self.config_backup
#    ToolsLog.info "\t\tBackuping config file in #{Tools.home+'/.tools/backup'}."
#   FileUtils.cp Tools.home+'/.tools/config',
#                Tolls.home+'/.tools/backup/config_' +
#                (ToolsUtil.get_date '%Y%m%d-%H%M') + '.backup'
end

.create_config_file(config_name, config_file, config_type = :json, data = {}) ⇒ Object

Create a Config file in work area

Sample

ToolsConfig.create_config_file 'sample', '/myhome/tools/sample.log' log = ToolsUtil.get_variable "sample_config" => Logger Object

ToolsConfig.create_log_file 'xyko', '/home/francisco/2018/xykotools/tools/xyko.config' config = ToolsConfig.xyko_load ToolsConfig.xyko_add {}

Parameters:

  • config_name
  • config_file
  • config_type (defaults to: :json)
  • data (defaults to: {})

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lib/config.rb', line 26

def self.create_config_file config_name, config_file, config_type = :json, data = {}
  unless File.exists? config_file
    case config_type
    when :json
      config = File.open(config_file, 'w')
      config.write JSON.pretty_generate(data)
      config.close
      ToolsLog.tools_info "Json config file '#{config_file}' was created!'"
      return true
    when :yaml
      config = File.open(config_file, 'w')
      config.write data.to_yaml
      config.close
      ToolsLog.tools_info "Json config file '#{config_file}' was created!'"
      return true
    end
  else
    ToolsLog.tools_warn "The file #{config_file} really exist. leaving operation...."
  end
  ToolsUtil.set_variable "#{config_name}_config_file", config_file
  ToolsUtil.set_variable "#{config_name}_config_type", config_type
end

.daily_backup_config ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/lib/config.rb', line 201

def self.daily_backup_config
#     #captura o backup mais recente
#     least = Dir.glob(File.join(Cmdapi.home+'/.cmdapi3/backup', 'config*.backup')).max { |a,b| File.ctime(a) <=> File.ctime(b) }
#     unless least.nil?
#       #se não houver sido feito hoje
#       unless File.ctime(least).to_date == Time.now.to_date
# #        ToolsLog.info "\t\tStarting config backup."
#         ToolsConfig.config_backup
#       end
#     else
# #      ToolsLog.info "\t\tStarting config backup."
#       ToolsConfig.config_backup
#     end
#     return true
end

.insert_in_config(source, command) ⇒ Object

Merge data in config file in work area

Parameters:

  • source

Returns:

  • content



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/lib/config.rb', line 104

def self.insert_in_config source, command
  test_config_type source
  file = open(source)
  case ToolsUtil.get_variable 'config_file_type'
  when :json
    json = file.read
    parsed = JSON.parse(json)
    parsed.rmerge!(command)
    file.close
    file = open(source, 'w')
    file.write JSON.pretty_generate(parsed)
    file.close
  when :yaml
    yaml = file.read
    parsed = YAML.load(yaml)
    parsed.rmerge!(command)
    file.close
    file = open(source, 'w')
    file.write parsed.to_yaml
    file.close
  end
end

.load_config(source) ⇒ Object

Load a content from a config file in work area

Parameters:

  • source

Returns:

  • content



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/lib/config.rb', line 84

def self.load_config source
  test_config_type source
  case ToolsUtil.get_variable 'config_file_type'
  when :json
    file    = open(source)
    json    = file.read
    parsed  = JSON.parse(json)
    ToolsUtil.set_variable 'config_data', parsed
    return parsed
  when :yaml
    file    = open(source)
    parsed = YAML.load(file.read)
    ToolsUtil.set_variable 'config_data', parsed
    return parsed
  end
end

.method_missing(method, *args, &block) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/lib/config.rb', line 50

def self.method_missing(method, *args, &block)
  #expected call format =>    STRING_LOGGER_TYPE + '_' + LOGGER_TYPE
  # Ex.:  tools_info
  config_name   = method.to_s.split('_').first
  config_method = method.to_s.split('_').last
  config_file   = ToolsUtil.get_variable "#{config_name}_config_file"
  config_type   = ToolsUtil.get_variable "#{config_name}_config_type"
end

.purge_backup_config ⇒ Object

execupa o purge de todos os backup com mais de 14 dias



185
186
187
188
189
190
191
192
# File 'lib/lib/config.rb', line 185

def self.purge_backup_config   # execupa o purge de todos os backup com mais de 14 dias
#    ToolsLog.info "\t\tPurge backup config file: #{Tools.home+'/.tools/config'}."
  #  to_clean = Dir.glob(File.join(Tools.home+'/.tools/backup', '*')).select { |a|
  #   Time.now - File.ctime(a) > 14*24*60*60 }
  #  to_clean.each do |file_to_delete|
  #   File.delete(file_to_delete)
  # end
end

.test_config_type(source) ⇒ Object

Test and register a config file in work area

Parameters:

  • source

Returns:

  • boolean



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/lib/config.rb', line 63

def self.test_config_type source
  file        = open(source)
  content     = file.read
  if ToolsUtil.valid_json? content
    config_type = :json
  else
    if ToolsUtil.valid_yaml? content
      config_type = :yaml
    else
      config_type = :invalid
      ToolsLog.tools_error "Invalid format in config file in '#{source}'.", :light_red
      ToolsLog.tools_exit
    end
  end
  ToolsUtil.set_variable 'config_file_type', config_type
end

.validate_config ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/lib/config.rb', line 156

def self.validate_config
#    ToolsLog.info "\t\tValidanting config file in #{Tools.home+'/.tools/config'}."
  # ToolsConfig.daily_backup_config
  # ToolsConfig.purge_backup_config
  # file   = open(Cmdapi.home+'/.tools/config')
  # config = file.read
  # begin
  #   JSON.parse(config)
  #   return true
  # rescue Exception => e
  #   CMDAPIUtil.show_info "\tInvalid JSON config file in #{Cmdapi.home+'/.tools/config'}."
  #   CMDAPIUtil.show_info "\t Follow the instructions below to fix the problem."
  #   CMDAPIUtil.show_info "\t  1. Rename the config file #{Cmdapi.home+'/.tools/config'}."
  #   CMDAPIUtil.show_info "\t  2. Cmdapi run again. The cmdapi will create a new configuration file."
  #   CMDAPIUtil.show_info "\t  3. Copy the keys from the previous file to the new file."
  #   CMDAPIUtil.show_info "\t  4. Run cmdapi again.."
  #   CMDAPIUtil.show_info "\t Aborting operation...."
  #   exit
  # end
end