Class: ToolsConfig
Class Method Summary collapse
- .check_config ⇒ Object
- .config_backup ⇒ Object
-
.create_config_file(config_name, config_file, config_type = :json, data = {}) ⇒ Object
Create a Config file in work area.
-
.create_config_file_old(*arguments) ⇒ Object
Create a config file in work area.
- .daily_backup_config ⇒ Object
-
.insert_in_config(source, command) ⇒ Object
Merge data in config file in work area.
-
.load_config(source) ⇒ Object
Load a content from a config file in work area.
- .method_missing(method, *args, &block) ⇒ Object
-
.purge_backup_config ⇒ Object
execupa o purge de todos os backup com mais de 14 dias.
-
.test_config_type(source) ⇒ Object
Test and register a config file in work area.
- .validate_config ⇒ Object
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ ToolsConfig
constructor
A new instance of ToolsConfig.
Constructor Details
#initialize(options = {}) ⇒ ToolsConfig
Returns a new instance of ToolsConfig.
5 6 |
# File 'lib/lib/config.rb', line 5 def initialize( = {}) end |
Class Method Details
.check_config ⇒ Object
187 188 189 190 191 192 193 |
# File 'lib/lib/config.rb', line 187 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
204 205 206 207 208 209 |
# File 'lib/lib/config.rb', line 204 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 {}
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/lib/config.rb', line 24 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') #data = {} ap data config.write JSON.pretty_generate(data) config.close ToolsLog.tools_info "Json config file '#{config_file}' was created!'" return true when :yaml 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 |
.create_config_file_old(*arguments) ⇒ Object
Create a config file in work area
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/lib/config.rb', line 63 def self.create_config_file_old *arguments directory = arguments.extract_first file_name = arguments.extract_first if arguments.extract_symbol :json config_type = :json else if arguments.extract_symbol :yaml config_type = :yaml else config_type = :default end end config_type = :json if config_type.eql? :default ToolsLog.tools_info "Setting config_type to '#{config_type}!'" ToolsUtil.set_variable 'config_file_type', config_type unless File.exists? directory ToolsLog.tools_error "Invalid directory in new config file in '#{directory}'.", :light_red ToolsLog.tools_exit end unless directory.end_with? '/' directory += '/' end config_file_location = directory + file_name ToolsUtil.set_variable 'config_file_location', config_file_location ToolsLog.tools_info "Setting config_file_location to '#{config_file_location}!'" unless File.exists? config_file_location case config_type when :json config_file = File.open(config_file_location, 'w') config = {} config_file.write JSON.pretty_generate(config) config_file.close ToolsLog.tools_info "Json config file created!'" return true when :yaml end else ToolsLog.tools_warn "The file #{config_file_location} really exist. leaving operation...." end end |
.daily_backup_config ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/lib/config.rb', line 211 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
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/lib/config.rb', line 148 def self.insert_in_config source, command test_config_type source case ToolsUtil.get_variable 'config_file_type' when :json file = open(source) 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 end end |
.load_config(source) ⇒ Object
Load a content from a config file in work area
132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/lib/config.rb', line 132 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 end end |
.method_missing(method, *args, &block) ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/lib/config.rb', line 45 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
195 196 197 198 199 200 201 202 |
# File 'lib/lib/config.rb', line 195 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
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/lib/config.rb', line 111 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
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/lib/config.rb', line 166 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 |