tools 0.0.1 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,364 @@
1
+ require 'singleton'
2
+ class ToolsUtil
3
+ include Singleton
4
+
5
+ def initialize(options = {})
6
+ I18n.load_path = Dir[Tools.files + '/pt-BR.yml']
7
+ I18n.locale = 'pt-BR'.to_sym
8
+ ToolsDisplay.instance
9
+ ToolsFiles.instance
10
+ ToolsConfig.instance
11
+ ToolsLog.instance
12
+ tools_logfile = Tools.home+'/.tools/tools.log'
13
+ ToolsLog.create_log_file 'tools', tools_logfile
14
+ end
15
+
16
+ # Test a valid json string.
17
+ #
18
+ # @param Json string to be tested
19
+ # @return boolean
20
+ def self.valid_json? source
21
+ begin
22
+ !!JSON.parse(source)
23
+ rescue JSON::ParserError
24
+ false
25
+ end
26
+ end
27
+
28
+ # Test a valid yaml string.
29
+ #
30
+ # @param Yaml string to be tested
31
+ # @return boolean
32
+ def self.valid_yaml? source
33
+ begin
34
+ !!YAML.parse(source)
35
+ rescue Psych::SyntaxError
36
+ false
37
+ end
38
+ end
39
+
40
+ # Return a formated DateTime.
41
+ #
42
+ # @param format with a DateTime format default are: 12 Outubro 2018, 08:29
43
+ # @return [String] formated date
44
+ def self.get_date format='%e %B %Y, %H:%M'
45
+ return I18n.l(DateTime.now, :format => format)
46
+ end
47
+
48
+
49
+ # Modify the class variable.. String => change value, Hash => merge, Array => insert
50
+ #
51
+ # @param variable name variable
52
+ # @param value value veriable
53
+ # @return []
54
+ def self.set_variable_ext variable, value
55
+ if self.instance_variable_defined? ("@#{variable}")
56
+ aux = self.get_variable variable
57
+ case aux.class.to_s
58
+ when 'String'
59
+ self.set_variable variable, value
60
+ when 'Hash'
61
+ begin
62
+ aux.merge! value
63
+ self.set_variable variable, aux
64
+ rescue
65
+ ToolsDisplay.show "\tToolsDisplay error [set_variable_ext]. Attempt insert #{variable.class} into Hash variable....".light_red
66
+ end
67
+ when 'Array'
68
+ aux.insert(-1,value)
69
+ self.set_variable variable, aux
70
+ end
71
+ end
72
+ end
73
+
74
+ # Set a new class variable.
75
+ #
76
+ # @param variable variable name to set
77
+ # @param value value for variable
78
+ # @return []
79
+ def self.set_variable variable, value
80
+ self.instance_variable_set("@#{variable}", value)
81
+ end
82
+
83
+ # Get a existent class variable.
84
+ #
85
+ # @param variable variable name to retrive
86
+ # @return variable value
87
+ def self.get_variable variable
88
+ return self.instance_variable_get("@#{variable}")
89
+ end
90
+
91
+ # Check a existent class variable.
92
+ #
93
+ # @param variable variable name to retrive
94
+ # @return boolean
95
+ def self.instance_variable? variable
96
+ ap self.instance_variables
97
+ exit
98
+ return self.instance_variable_get("@#{variable}")
99
+ end
100
+
101
+
102
+
103
+ ##### sem minitest
104
+
105
+ def self.purge_files path, select, time #Cmdapi.configuration.home+'/.cmdapi/backup', '*', 14*24*60*60
106
+ to_clean = Dir.glob(File.join(path, select)).select { |a|
107
+ Time.now - File.ctime(a) > time }
108
+ to_clean.each do |file_to_delete|
109
+ File.delete(file_to_delete)
110
+ end
111
+ end
112
+
113
+ end
114
+
115
+ module HashRecursiveBlank
116
+ def rblank
117
+ r = {}
118
+ each_pair do |key, val|
119
+ r[key] = val.rblank if val.is_a?(Hash)
120
+ end
121
+ return r.keep_if { |key, val| val.is_a?(Hash) }
122
+ end
123
+
124
+ def rblank!
125
+ each_pair do |key, val|
126
+ self[key] = val.rblank! if val.is_a?(Hash)
127
+ end
128
+ return keep_if { |key, val| val.is_a?(Hash) }
129
+ end
130
+ end
131
+
132
+ module HashRecursiveMerge
133
+ def rmerge(other_hash, concat_if_array = false)
134
+ r = {}
135
+ return merge(other_hash) do |key, oldval, newval|
136
+ if oldval.is_a?(Hash)
137
+ r[key] = oldval.rmerge(newval, concat_if_array)
138
+ elsif oldval.is_a?(Array) and newval.is_a?(Array)
139
+ r[key] = concat_if_array ? oldval + newval : newval
140
+ else
141
+ newval
142
+ end
143
+ end
144
+ end
145
+
146
+ def rmerge!(other_hash, concat_if_array = false)
147
+ return merge!(other_hash) do |key, oldval, newval|
148
+ if oldval.is_a?(Hash)
149
+ oldval.rmerge!(newval, concat_if_array)
150
+ elsif oldval.is_a?(Array) and newval.is_a?(Array)
151
+ concat_if_array ? oldval + newval : newval
152
+ else
153
+ newval
154
+ end
155
+ end
156
+ end
157
+ end
158
+
159
+ class Hash
160
+ include HashRecursiveMerge
161
+ include HashRecursiveBlank
162
+
163
+ def diff(other)
164
+ (self.keys + other.keys).uniq.inject({}) do |memo, key|
165
+ unless self[key] == other[key]
166
+ if self[key].kind_of?(Hash) && other[key].kind_of?(Hash)
167
+ memo[key] = self[key].diff(other[key])
168
+ else
169
+ memo[key] = [self[key], other[key]]
170
+ end
171
+ end
172
+ memo
173
+ end
174
+ end
175
+ end
176
+
177
+
178
+ class String
179
+
180
+ # Self test nil String class.
181
+ #
182
+ # @return boolean
183
+ def nil?
184
+ return '' if self == nil
185
+ end
186
+
187
+
188
+ # Justity relative left or right position filled with a espefific char in String.
189
+ #
190
+ # sample:
191
+ #
192
+ # "TESTE".fix(10,'xy') # => xxxxxTESTE
193
+ # "TESTE".fix(-10,'xy') # => TESTExxxxx
194
+ #
195
+ # @param size to justify.
196
+ # @param pattern pattern do justify
197
+ # @return formated string
198
+ def fix(size, pattern=' ')
199
+ if size >= 0
200
+ self[0...size].rjust(size, pattern)
201
+ else
202
+ diff = size.abs - self.size
203
+ self + ''.fix(diff,pattern)
204
+ end
205
+ end
206
+
207
+ # Encrypt a string using a key.
208
+ # sample
209
+ # msg = "teste do encrypt".light_blue
210
+ # passwd = 'tools999'
211
+ # encrypted = msg.encrypt passwd
212
+ # puts (encrypted.decrypt passwd)
213
+ # @return encrypt string
214
+ def encrypt(key)
215
+ Encrypt.dump self, key
216
+ end
217
+
218
+ # Decrypt a string using a key.
219
+ # sample
220
+ # msg = "teste do encrypt".light_blue
221
+ # passwd = 'tools999'
222
+ # encrypted = msg.encrypt passwd
223
+ # puts (encrypted.decrypt passwd)
224
+ # @return decrypt string
225
+ def decrypt(key)
226
+ Encrypt.load self, key
227
+ end
228
+
229
+ # Self test alphanum String class.
230
+ #
231
+ # @return boolean
232
+ def alnum?
233
+ !!match(/^[[:alnum:]]+$/)
234
+ end
235
+
236
+ # Self test alpha String class.
237
+ #
238
+ # @return boolean
239
+ def alpha?
240
+ !!match(/^[[:alpha:]]+$/)
241
+ end
242
+
243
+ end
244
+
245
+
246
+ class Object
247
+ # Self test Boolean class.
248
+ #
249
+ # @return boolean
250
+ def boolean?
251
+ self.is_a?(TrueClass) || self.is_a?(FalseClass)
252
+ end
253
+ # Self test Symbol class.
254
+ #
255
+ # @return boolean
256
+ def symbol?
257
+ self.is_a?(Symbol)
258
+ end
259
+ # Self test String class.
260
+ #
261
+ # @return boolean
262
+ def string?
263
+ self.is_a?(String)
264
+ end
265
+ end
266
+
267
+ class Array
268
+
269
+ # Self pop first element.
270
+ # @return first element
271
+ def extract_first
272
+ first = self[0]
273
+ self.delete_at(0)
274
+ return first
275
+ end
276
+
277
+ # Self extract symbol.
278
+ # @param symbol to retrive
279
+ # @return boolean
280
+ def extract_symbol symbol
281
+ status = false
282
+ if self.include? symbol
283
+ status = true
284
+ self.delete(symbol)
285
+ end
286
+ return status
287
+ end
288
+
289
+ # Self extract color.
290
+ # @return boolean
291
+ def extract_color
292
+ colors = String.colors
293
+ color = :default
294
+ self.each do |argument|
295
+ if argument.symbol?
296
+ if colors.include? argument
297
+ color = argument
298
+ self.delete(argument)
299
+ return color
300
+ end
301
+ else
302
+ if argument.string?
303
+ if argument.start_with? ':'
304
+ color_candidate = argument.gsub(':','').to_sym
305
+ color = color_candidate if colors.include? color_candidate
306
+ self.delete(argument)
307
+ return color
308
+ end
309
+ end
310
+ end
311
+ end
312
+ return color
313
+ end
314
+
315
+
316
+
317
+
318
+ # Self extract option.
319
+ # @param option to extract.
320
+ # @param single boolean to repeat
321
+ # @return variable boolean, arguments - extract_option
322
+ #
323
+ # Sample
324
+ # args = [xxx -x -vvv -c -vcv -v2 -vvvvv -s :json :yellow :red]
325
+ # args.extract_option '-x' => true
326
+ # args.extract_option '-f' => false
327
+ # args.extract_option '-v', false => 8
328
+ # args.extract_symbol :json => true
329
+ # args.extract_symbol :yaml => false
330
+ # args.extract_color => :yellow
331
+ # args.extract_color => red
332
+ # args => [-c -vcv -v2 -s ]
333
+ #
334
+ def extract_option option, single = true
335
+ if single
336
+ result = false
337
+ if self.include? option
338
+ index = self.index(option)
339
+ self.delete_at(index)
340
+ result = true
341
+ end
342
+ else
343
+ result = 0
344
+ self.each do |argument|
345
+ if argument.start_with? option
346
+ #puts "'#{option}' '#{argument}' #{argument.start_with? option} \n"
347
+ search_char = option.sub('-','')
348
+ aux_string = argument.sub('-','')
349
+ count = argument.count(search_char)
350
+ if (count == aux_string.size)
351
+ result += count
352
+ self.delete(argument)
353
+ end
354
+ end
355
+ end
356
+ end
357
+ return result
358
+ end
359
+
360
+
361
+
362
+
363
+
364
+ end
@@ -1,5 +1,89 @@
1
1
  require "tools/version"
2
2
 
3
+ # Basic models
4
+ require 'lib/utils'
5
+ require 'lib/display'
6
+ require 'lib/net'
7
+ require 'lib/files'
8
+
9
+ # Extented models
10
+ require 'lib/config'
11
+ require 'lib/log'
12
+ require 'lib/cache'
13
+
14
+ # require 'lib/console'
15
+
16
+ load __dir__ + '/files/requireds.rb'
17
+
3
18
  module Tools
4
- # Your code goes here...
19
+
20
+ @@check_version = nil
21
+
22
+ def self.set_check_version param
23
+ @@check_version = param
24
+ end
25
+
26
+ def self.get_check_version
27
+ return @@check_version
28
+ end
29
+
30
+ class Configuration
31
+ attr_accessor :console_prompt
32
+ end
33
+
34
+ class << self
35
+ attr_accessor :configuration
36
+
37
+ def configure
38
+ self.configuration ||= Configuration.new
39
+ yield(configuration)
40
+ end
41
+
42
+ end
43
+
44
+
45
+ def self.root
46
+ File.dirname __dir__
47
+ end
48
+
49
+ def self.files
50
+ File.join root, 'lib/files'
51
+ end
52
+
53
+ def self.host
54
+ Socket.gethostname
55
+ end
56
+
57
+ def self.home
58
+ ENV['HOME']
59
+ end
60
+
61
+ def self.user
62
+ ENV['USER']
63
+ end
64
+
65
+ def self.pwd
66
+ ENV['PWD']
67
+ end
68
+
69
+ def self.ldap_pass
70
+ ENV['ldap_pass']
71
+ end
72
+
73
+ def self.ldap_user
74
+ ENV['ldap_user']
75
+ end
76
+
77
+ def self.gem_path
78
+ ENV['GEM_PATH']
79
+ end
80
+
81
+ end
82
+
83
+ Tools.configure do |config|
84
+ config.console_prompt = Tools::VERSION
85
+ ToolsUtil.instance
5
86
  end
87
+
88
+
89
+