tools 0.0.7 → 0.3.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/aux2 CHANGED
@@ -1,2 +1,15 @@
1
- test file..
2
- linha +1
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+ require 'rubygems'
4
+ require 'tools'
5
+
6
+ args = ["1","2","3"]
7
+
8
+
9
+ ap args.extract_first.nil?
10
+ ap args.extract_first
11
+ ap args.extract_first
12
+ ap args.extract_first
13
+ ap args.extract_first.nil?
14
+
15
+
@@ -1,28 +1,38 @@
1
1
  require 'awesome_print'
2
2
  require 'base64'
3
+ require 'byebug'
3
4
  require 'colorize'
4
5
  require 'dnsruby'
5
6
  require 'encrypt'
6
7
  require 'getoptlong'
8
+ require 'highline'
7
9
  require 'highline/import'
8
10
  require 'i18n'
9
11
  require 'ipaddress'
10
12
  require 'ipcalc'
11
13
  require 'json'
14
+ require 'json_patterns'
12
15
  require 'logger'
13
16
  require 'net/http'
14
17
  require 'net/https'
18
+ require 'net/ping'
15
19
  require 'net/smtp'
16
20
  require 'net/ssh'
17
21
  require 'netaddr'
22
+ require 'persistent-cache'
23
+ require 'progress_bar'
18
24
  require 'prompt'
19
25
  require 'public_suffix'
20
- require 'rest_client'
26
+ require 'rest-client'
21
27
  require 'ruby-progressbar'
22
28
  require 'rubygems'
29
+ require 'similar_text'
23
30
  require 'socket'
31
+ require 'thor'
32
+ require 'thor'
33
+ require 'tty-editor'
24
34
  require 'tty-prompt'
25
35
  require 'xmlsimple'
26
- require 'thor'
27
36
  require 'yaml'
37
+
28
38
  include Dnsruby
@@ -8,39 +8,128 @@ class ToolsCache
8
8
 
9
9
  # Create a cache file in work area
10
10
  #
11
- # @param arguments
11
+ # @param cache_name
12
+ # @param cache_file
13
+ # @param ttl
12
14
  # @return
13
- def self.create_cache_file cache_name, cache_dir
14
-
15
- unless File.exists? cache_dir
16
- ToolsLog.tools_error "Invalid directory in new config file in '#{cache_dir}'.", :light_red
17
- ToolsLog.tools_exit
18
- end
19
- unless cache_dir.end_with? '/'
20
- cache_dir += '/'
21
- end
22
-
23
- cache_file = cache_dir + cache_name + '.cache'
24
- if File.exists? cache_file
25
- ToolsLog.tools_error "The cache file in '#{cache_file}' already exists. Leaving operation...", :light_yellow
26
- else
27
- ToolsFiles.create_file cache_dir, cache_name, "cache_#{cache_name}"
28
- end
29
-
15
+ def self.create_cache_file cache_name, cache_file, ttl=nil
16
+ cache = Persistent::Cache.new(cache_file, ttl)
17
+ ToolsUtil.set_variable "#{cache_name}_cache", cache
30
18
  end
31
19
 
20
+
21
+ # Add a record in a cache work area
22
+ #
23
+ # Sample
24
+ #
25
+ # ToolsCache.create_cache_file 'cmdapi', '/home/francisco/.newcmdapi/cmdapi-persistent.cache'
26
+ # cache = ToolsUtil.get_variable 'cmdapi_cache'
27
+ # cache.clear
28
+ # ToolsCache.cmdapi_set :ldap, {:rogerio => {:name => 'rogerio'}}
29
+ # ToolsCache.cmdapi_set :ldap, {:wagner => {:name => 'wagner'}}
30
+ # ToolsCache.cmdapi_set :cloud, [100,200]
31
+ # ToolsCache.cmdapi_set :cloud, 300
32
+ # ToolsCache.cmdapi_set :cloud, [300,500,'teste']
33
+ # ToolsCache.cmdapi_set :cloud, "teste2"
34
+ # ToolsCache.cmdapi_set :cloud, {:teste => 1}
35
+ # ToolsCache.cmdapi_set :nat, "texto para nat"
36
+ # ToolsCache.cmdapi_set :nat, "texto para nat_ depois da primeira"
37
+ # ToolsCache.cmdapi_unset :nat
38
+ # ToolsCache.cmdapi_set :nat, "texto para nat_ depois da primeira"
39
+ #
40
+ # @param method
41
+ # @param args
42
+ # @param block
43
+ # @return
32
44
  def self.method_missing(method, *args, &block)
33
- #expected call format => STRING_LOGGER_TYPE + '_' + LOGGER_TYPE
34
- # Ex.: tools_info
45
+
35
46
  cache_name = method.to_s.split('_').first
36
47
  cache_method = method.to_s.split('_').last
37
- cache_file = ToolsUtil.get_variable "cache_#{cache_name}"
48
+ cache = ToolsUtil.get_variable "#{cache_name}_cache"
49
+
50
+ case cache_method
51
+
52
+ when 'set'
53
+ key = args.extract_first
54
+ values = args.extract_first
55
+ unless cache.key?(key)
56
+ cache[key] = values
57
+ else
58
+ aux = cache[key]
59
+ case aux.class.to_s
60
+ when 'Hash'
61
+ begin
62
+ aux.merge! values
63
+ cache[key] = aux
64
+ rescue Exception => e
65
+ log_file = ToolsUtil.get_variable "#{logger_name}_log_file"
66
+ ToolsDisplay.show "\tError in ToolsCache: #{e.exception}", :light_yellow
67
+ exit
68
+ end
69
+ when 'Array'
70
+ aux = cache[key]
71
+ case values.class.to_s
72
+ when 'Array'
73
+ values.each do |value|
74
+ aux.insert(-1,value)
75
+ end
76
+ cache[key] = aux
77
+ else
78
+ aux.insert(-1,values)
79
+ cache[key] = aux
80
+ end
81
+ end
82
+
83
+ end
84
+
85
+ # when 'set'
86
+ # key = args.extract_first
87
+ # value = args.extract_first
88
+ # cache.set(key, value, Time.now)
89
+
90
+ when 'setext'
91
+ key = args.extract_first
92
+ values = args.extract_first
93
+ unless cache.key?(key)
94
+ ToolsCache.cmdapi_set key, values
95
+ else
96
+ aux = cache[key]
97
+ values.keys.each do |value|
98
+ aux[value] = values[value]
99
+ end
100
+ ToolsCache.cmdapi_set key, aux
101
+ end
38
102
 
39
- ap cache_name
40
- ap cache_method
41
- ap cache_file
42
- ap args
43
- ap block
103
+ when 'unsetext'
104
+ key = args.extract_first
105
+ key_internal = args.extract_first
106
+ if cache.key?(key)
107
+ aux = cache[key]
108
+ if aux.key? key_internal
109
+ aux
110
+ end
111
+ end
112
+
113
+ when 'unset'
114
+ key = args.extract_first
115
+ cache[key] = nil
116
+
117
+ when 'get'
118
+ key = args.extract_first
119
+ return cache[key]
120
+
121
+ when 'list'
122
+ result = {}
123
+ cache.each do |key|
124
+ result[key] = cache[key]
125
+ end
126
+ return result
127
+
128
+ when 'clear'
129
+ cache.each do |key|
130
+ cache[key] = nil
131
+ end
132
+ end
44
133
 
45
134
  end
46
135
 
@@ -18,21 +18,26 @@ class ToolsConfig
18
18
  # config = ToolsConfig.xyko_load
19
19
  # ToolsConfig.xyko_add {}
20
20
  #
21
- # @param logname
22
- # @param logfile
21
+ # @param config_name
22
+ # @param config_file
23
+ # @param config_type
24
+ # @param data
23
25
  # @return
24
26
  def self.create_config_file config_name, config_file, config_type = :json, data = {}
25
27
  unless File.exists? config_file
26
28
  case config_type
27
29
  when :json
28
30
  config = File.open(config_file, 'w')
29
- #data = {}
30
- ap data
31
31
  config.write JSON.pretty_generate(data)
32
32
  config.close
33
33
  ToolsLog.tools_info "Json config file '#{config_file}' was created!'"
34
34
  return true
35
35
  when :yaml
36
+ config = File.open(config_file, 'w')
37
+ config.write data.to_yaml
38
+ config.close
39
+ ToolsLog.tools_info "Json config file '#{config_file}' was created!'"
40
+ return true
36
41
  end
37
42
  else
38
43
  ToolsLog.tools_warn "The file #{config_file} really exist. leaving operation...."
@@ -49,62 +54,9 @@ class ToolsConfig
49
54
  config_method = method.to_s.split('_').last
50
55
  config_file = ToolsUtil.get_variable "#{config_name}_config_file"
51
56
  config_type = ToolsUtil.get_variable "#{config_name}_config_type"
52
-
53
-
54
57
  end
55
58
 
56
59
 
57
-
58
-
59
- # Create a config file in work area
60
- #
61
- # @param arguments
62
- # @return
63
- def self.create_config_file_old *arguments
64
- directory = arguments.extract_first
65
- file_name = arguments.extract_first
66
-
67
- if arguments.extract_symbol :json
68
- config_type = :json
69
- else
70
- if arguments.extract_symbol :yaml
71
- config_type = :yaml
72
- else
73
- config_type = :default
74
- end
75
- end
76
- config_type = :json if config_type.eql? :default
77
- ToolsLog.tools_info "Setting config_type to '#{config_type}!'"
78
- ToolsUtil.set_variable 'config_file_type', config_type
79
-
80
- unless File.exists? directory
81
- ToolsLog.tools_error "Invalid directory in new config file in '#{directory}'.", :light_red
82
- ToolsLog.tools_exit
83
- end
84
- unless directory.end_with? '/'
85
- directory += '/'
86
- end
87
- config_file_location = directory + file_name
88
- ToolsUtil.set_variable 'config_file_location', config_file_location
89
- ToolsLog.tools_info "Setting config_file_location to '#{config_file_location}!'"
90
-
91
- unless File.exists? config_file_location
92
- case config_type
93
- when :json
94
- config_file = File.open(config_file_location, 'w')
95
- config = {}
96
- config_file.write JSON.pretty_generate(config)
97
- config_file.close
98
- ToolsLog.tools_info "Json config file created!'"
99
- return true
100
- when :yaml
101
- end
102
- else
103
- ToolsLog.tools_warn "The file #{config_file_location} really exist. leaving operation...."
104
- end
105
-
106
- end
107
-
108
60
  # Test and register a config file in work area
109
61
  # @param source
110
62
  # @return boolean
@@ -139,6 +91,10 @@ class ToolsConfig
139
91
  ToolsUtil.set_variable 'config_data', parsed
140
92
  return parsed
141
93
  when :yaml
94
+ file = open(source)
95
+ parsed = YAML.load(file.read)
96
+ ToolsUtil.set_variable 'config_data', parsed
97
+ return parsed
142
98
  end
143
99
  end
144
100
 
@@ -147,9 +103,9 @@ class ToolsConfig
147
103
  # @return content
148
104
  def self.insert_in_config source, command
149
105
  test_config_type source
106
+ file = open(source)
150
107
  case ToolsUtil.get_variable 'config_file_type'
151
108
  when :json
152
- file = open(source)
153
109
  json = file.read
154
110
  parsed = JSON.parse(json)
155
111
  parsed.rmerge!(command)
@@ -158,9 +114,43 @@ class ToolsConfig
158
114
  file.write JSON.pretty_generate(parsed)
159
115
  file.close
160
116
  when :yaml
117
+ yaml = file.read
118
+ parsed = YAML.load(yaml)
119
+ parsed.rmerge!(command)
120
+ file.close
121
+ file = open(source, 'w')
122
+ file.write parsed.to_yaml
123
+ file.close
161
124
  end
162
125
  end
163
126
 
127
+ # Change data in config file in work area
128
+ # @param args Sequence keys to locate the value in hash
129
+ def self.change_value_in_config *args
130
+ source = args.extract_first
131
+ value = args.extract_first
132
+ test_config_type source
133
+ case ToolsUtil.get_variable 'config_file_type'
134
+ when :json
135
+ file = open(source)
136
+ json = file.read
137
+ parsed = JSON.parse(json)
138
+ parsed.nested_set(args, value)
139
+ file.close
140
+ file = open(source, 'w')
141
+ file.write JSON.pretty_generate(parsed)
142
+ file.close
143
+ when :yaml
144
+ file = open(source)
145
+ yaml = file.read
146
+ parsed = YAML.load(yaml)
147
+ parsed.nested_set(args, value)
148
+ file.close
149
+ file = open(source, 'w')
150
+ file.write parsed.to_yaml
151
+ file.close
152
+ end
153
+ end
164
154
 
165
155
 
166
156
  def self.validate_config
@@ -17,10 +17,10 @@ class ToolsDisplay
17
17
  return post.class.to_s
18
18
  end
19
19
 
20
- color = arguments.extract_color
21
- sameline = arguments.extract_symbol :sameline
22
- nocolor = arguments.extract_symbol :nocolor
23
- colorized = arguments.extract_symbol :colorized
20
+ color = arguments.extract_color
21
+ sameline = arguments.extract_symbol :sameline
22
+ nocolor = arguments.extract_symbol :nocolor
23
+ colorized = arguments.extract_symbol :colorized
24
24
 
25
25
  unless sameline
26
26
  post += "\n"
@@ -34,8 +34,10 @@ class ToolsDisplay
34
34
  printf "#{post}"
35
35
  end
36
36
  end
37
-
38
37
  end
39
38
 
39
+ def self.show_colorize *arguments
40
+ puts arguments.first
41
+ end
40
42
 
41
43
  end
@@ -49,18 +49,26 @@ class ToolsFiles
49
49
  #
50
50
  # Sample
51
51
  #
52
- # ToolsFiles.load_file xyko, file_to_load
53
- # xyko = (ToolsUtil.get_variable 'xyko_file') => ~/2018/xykotools/tools/home/xyko_file.txt
54
- #
55
- # @param file_name
56
- # @param file_name_path
52
+ # ToolsFiles.load_file file_to_load
53
+ # @param file_to_load Object
57
54
  # @return
58
- def self.load_file file_key, file_to_load
55
+ def self.load_file file_to_load
59
56
  if File.exists? file_to_load
60
- file = File.open( complete_file , 'r')
57
+ file = File.open( file_to_load , 'r')
61
58
  return file
62
59
  end
63
60
  end
64
61
 
62
+ def self.open_file file, default_editor=nil
63
+ begin
64
+ if default_editor.nil?
65
+ TTY::Editor.open( file, command: :vi)
66
+ else
67
+ TTY::Editor.open( file, command: default_editor)
68
+ end
69
+ rescue Exception => e
70
+ return e
71
+ end
72
+ end
65
73
 
66
74
  end
@@ -20,8 +20,8 @@ class ToolsLog
20
20
  # ToolsLog.xyko_error 'teste do methodo3'
21
21
  # ToolsLog.xyko_debug 'teste do methodo4'
22
22
  #
23
- # @param logname
24
- # @param logfile
23
+ # @param log_name LogName to create
24
+ # @param log_file LogFile path
25
25
  # @return
26
26
  def self.create_log_file log_name, log_file
27
27
  if File.exists? log_file
@@ -54,13 +54,21 @@ class ToolsLog
54
54
  color = :green
55
55
  when 'error'
56
56
  color = :light_red
57
+ when 'ucolor'
58
+ color = nil
59
+ logger_method = 'info'
57
60
  when 'exit'
58
61
  log_file = ToolsUtil.get_variable "#{logger_name}_log_file"
59
62
  ToolsDisplay.show "\tError in ToolsUtil. See details in '#{log_file}'", :light_yellow
60
63
  exit
61
64
  end
62
65
  end
63
- logger.method(logger_method).call args.first.colorize(color)
66
+ unless color.nil?
67
+ text_to_print = args.first.colorize(color)
68
+ else
69
+ text_to_print = args.first
70
+ end
71
+ logger.method(logger_method).call text_to_print
64
72
  end
65
73
 
66
74
  end