tools 0.4.5 → 0.4.6
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.
- checksums.yaml +4 -4
- data/.rubocop.yml +11 -0
- data/Gemfile +1 -1
- data/Rakefile +9 -15
- data/aux2 +1 -5
- data/bin/tools +29 -37
- data/lib/files/requireds.rb +0 -2
- data/lib/lib/array.rb +45 -65
- data/lib/lib/cache.rb +11 -18
- data/lib/lib/config.rb +47 -76
- data/lib/lib/console.rb +92 -108
- data/lib/lib/display.rb +10 -29
- data/lib/lib/files.rb +26 -44
- data/lib/lib/hash.rb +21 -22
- data/lib/lib/log.rb +28 -32
- data/lib/lib/net.rb +58 -80
- data/lib/lib/object.rb +8 -7
- data/lib/lib/prompt.rb +24 -26
- data/lib/lib/string.rb +15 -15
- data/lib/lib/utils.rb +90 -66
- data/lib/tools.rb +3 -8
- data/lib/tools/version.rb +2 -2
- data/test/mini_array.rb +7 -12
- data/test/mini_cache.rb +9 -12
- data/test/mini_config.rb +21 -12
- data/test/mini_console.rb +6 -8
- data/test/mini_display.rb +6 -9
- data/test/mini_file.rb +20 -24
- data/test/mini_hash.rb +27 -38
- data/test/mini_log.rb +2 -4
- data/test/mini_net.rb +167 -185
- data/test/mini_object.rb +16 -21
- data/test/mini_prompt.rb +28 -16
- data/test/mini_string.rb +11 -14
- data/test/mini_utils.rb +13 -16
- data/test/run +9 -9
- data/tools.gemspec +12 -14
- metadata +35 -20
data/lib/lib/config.rb
CHANGED
@@ -2,10 +2,7 @@ require 'singleton'
|
|
2
2
|
class ToolsConfig
|
3
3
|
include Singleton
|
4
4
|
|
5
|
-
def initialize(options = {})
|
6
|
-
end
|
7
|
-
|
8
|
-
|
5
|
+
def initialize(options = {}); end
|
9
6
|
|
10
7
|
# Create a Config file in work area
|
11
8
|
#
|
@@ -23,77 +20,60 @@ class ToolsConfig
|
|
23
20
|
# @param config_type
|
24
21
|
# @param data
|
25
22
|
# @return
|
26
|
-
def self.create_config_file
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
config.close
|
39
|
-
#ToolsLog.tools_info "Json config file '#{config_file}' was created!'"
|
40
|
-
#return true
|
41
|
-
end
|
42
|
-
else
|
43
|
-
#ToolsLog.tools_warn "The file #{config_file} really exist. leaving operation...."
|
44
|
-
return false
|
23
|
+
def self.create_config_file(config_name, config_file, config_type = :json, data = {})
|
24
|
+
return false if File.exist? config_file
|
25
|
+
|
26
|
+
case config_type
|
27
|
+
when :json
|
28
|
+
config = File.open(config_file, 'w')
|
29
|
+
config.write JSON.pretty_generate(data)
|
30
|
+
config.close
|
31
|
+
when :yaml
|
32
|
+
config = File.open(config_file, 'w')
|
33
|
+
config.write data.to_yaml
|
34
|
+
config.close
|
45
35
|
end
|
46
36
|
ToolsUtil.set_variable "#{config_name}_config_file", config_file
|
47
37
|
ToolsUtil.set_variable "#{config_name}_config_type", config_type
|
48
38
|
end
|
49
39
|
|
50
|
-
|
51
|
-
|
52
|
-
#
|
53
|
-
#
|
54
|
-
config_name = method.to_s.split('_').first
|
55
|
-
config_method = method.to_s.split('_').last
|
56
|
-
config_file = ToolsUtil.get_variable "#{config_name}_config_file"
|
57
|
-
config_type = ToolsUtil.get_variable "#{config_name}_config_type"
|
40
|
+
def self.method_missing(method, *_args)
|
41
|
+
config_name = method.to_s.split('_').first
|
42
|
+
ToolsUtil.get_variable "#{config_name}_config_file"
|
43
|
+
ToolsUtil.get_variable "#{config_name}_config_type"
|
58
44
|
end
|
59
45
|
|
60
|
-
|
61
46
|
# Test and register a config file in work area
|
62
47
|
# @param source
|
63
48
|
# @return boolean
|
64
|
-
def self.test_config_type
|
65
|
-
file = open(source)
|
49
|
+
def self.test_config_type(source)
|
50
|
+
file = File.open(source)
|
66
51
|
content = file.read
|
67
|
-
if ToolsUtil.valid_json? content
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
ToolsLog.tools_error "Invalid format in config file in '#{source}'.", :light_red
|
75
|
-
ToolsLog.tools_exit
|
76
|
-
end
|
77
|
-
end
|
52
|
+
config_type = if ToolsUtil.valid_json? content
|
53
|
+
:json
|
54
|
+
elsif ToolsUtil.valid_yaml? content
|
55
|
+
:yaml
|
56
|
+
else
|
57
|
+
:invalid
|
58
|
+
end
|
78
59
|
ToolsUtil.set_variable 'config_file_type', config_type
|
79
60
|
end
|
80
61
|
|
81
|
-
|
82
62
|
# Load a content from a config file in work area
|
83
63
|
# @param source
|
84
64
|
# @return content
|
85
|
-
def self.load_config
|
65
|
+
def self.load_config(source)
|
86
66
|
test_config_type source
|
87
67
|
case ToolsUtil.get_variable 'config_file_type'
|
88
68
|
when :json
|
89
|
-
file = open(source)
|
69
|
+
file = File.open(source)
|
90
70
|
json = file.read
|
91
71
|
parsed = JSON.parse(json)
|
92
72
|
ToolsUtil.set_variable 'config_data', parsed
|
93
73
|
return parsed
|
94
74
|
when :yaml
|
95
|
-
file
|
96
|
-
parsed = YAML.
|
75
|
+
file = File.open(source)
|
76
|
+
parsed = YAML.safe_load(file.read, [Symbol])
|
97
77
|
ToolsUtil.set_variable 'config_data', parsed
|
98
78
|
return parsed
|
99
79
|
end
|
@@ -102,24 +82,24 @@ class ToolsConfig
|
|
102
82
|
# Merge data in config file in work area
|
103
83
|
# @param source
|
104
84
|
# @return content
|
105
|
-
def self.insert_in_config
|
85
|
+
def self.insert_in_config(source, command)
|
106
86
|
test_config_type source
|
107
|
-
file = open(source)
|
87
|
+
file = File.open(source)
|
108
88
|
case ToolsUtil.get_variable 'config_file_type'
|
109
89
|
when :json
|
110
90
|
json = file.read
|
111
91
|
parsed = JSON.parse(json)
|
112
92
|
parsed.rmerge!(command)
|
113
93
|
file.close
|
114
|
-
file = open(source, 'w')
|
94
|
+
file = File.open(source, 'w')
|
115
95
|
file.write JSON.pretty_generate(parsed)
|
116
96
|
file.close
|
117
97
|
when :yaml
|
118
98
|
yaml = file.read
|
119
|
-
parsed = YAML.
|
99
|
+
parsed = YAML.safe_load(yaml, [Symbol])
|
120
100
|
parsed.rmerge!(command)
|
121
101
|
file.close
|
122
|
-
file = open(source, 'w')
|
102
|
+
file = File.open(source, 'w')
|
123
103
|
file.write parsed.to_yaml
|
124
104
|
file.close
|
125
105
|
end
|
@@ -127,48 +107,39 @@ class ToolsConfig
|
|
127
107
|
|
128
108
|
# Change data in config file in work area
|
129
109
|
# @param args Sequence keys to locate the value in hash
|
130
|
-
def self.change_value_in_config
|
110
|
+
def self.change_value_in_config(*args)
|
131
111
|
source = args.extract_first
|
132
112
|
value = args.extract_first
|
133
113
|
test_config_type source
|
134
114
|
case ToolsUtil.get_variable 'config_file_type'
|
135
115
|
when :json
|
136
|
-
file = open(source)
|
116
|
+
file = File.open(source)
|
137
117
|
json = file.read
|
138
118
|
parsed = JSON.parse(json)
|
139
119
|
parsed.nested_set(args, value)
|
140
120
|
file.close
|
141
|
-
file = open(source, 'w')
|
121
|
+
file = File.open(source, 'w')
|
142
122
|
file.write JSON.pretty_generate(parsed)
|
143
123
|
file.close
|
144
124
|
when :yaml
|
145
|
-
file = open(source)
|
125
|
+
file = File.open(source)
|
146
126
|
yaml = file.read
|
147
|
-
parsed = YAML.
|
127
|
+
parsed = YAML.safe_load(yaml, [Symbol])
|
148
128
|
parsed.nested_set(args, value)
|
149
129
|
file.close
|
150
|
-
file = open(source, 'w')
|
130
|
+
file = File.open(source, 'w')
|
151
131
|
file.write parsed.to_yaml
|
152
132
|
file.close
|
153
133
|
end
|
154
134
|
end
|
155
135
|
|
136
|
+
def self.validate_config; end
|
156
137
|
|
138
|
+
def self.check_config; end
|
157
139
|
|
158
|
-
def self.
|
159
|
-
end
|
160
|
-
|
161
|
-
def self.check_config
|
162
|
-
end
|
163
|
-
|
164
|
-
def self.purge_backup_config
|
165
|
-
end
|
166
|
-
|
167
|
-
def self.config_backup
|
168
|
-
end
|
169
|
-
|
170
|
-
def self.daily_backup_config
|
171
|
-
end
|
140
|
+
def self.purge_backup_config; end
|
172
141
|
|
142
|
+
def self.config_backup; end
|
173
143
|
|
174
|
-
end
|
144
|
+
def self.daily_backup_config; end
|
145
|
+
end
|
data/lib/lib/console.rb
CHANGED
@@ -2,138 +2,122 @@ require 'singleton'
|
|
2
2
|
class ToolsConsole
|
3
3
|
include Singleton
|
4
4
|
|
5
|
-
def self.exec_console
|
5
|
+
def self.exec_console(args)
|
6
6
|
command_name = args.extract_first
|
7
|
-
cmd = Prompt.application.commands.select {|c| c.name.eql? command_name}.first
|
8
|
-
|
9
|
-
cmd.run cmd
|
10
|
-
return true
|
11
|
-
else
|
12
|
-
ToolsDisplay.show "Invalid command '#{command_name}'.! Aborting...", :light_yellow
|
13
|
-
return false
|
14
|
-
end
|
15
|
-
end
|
7
|
+
cmd = Prompt.application.commands.select { |c| c.name.eql? command_name }.first
|
8
|
+
return false if cmd.nil?
|
16
9
|
|
17
|
-
|
10
|
+
cmd.run cmd
|
11
|
+
true
|
18
12
|
end
|
19
13
|
|
20
|
-
def
|
21
|
-
|
22
|
-
extend Prompt::DSL
|
14
|
+
def initialize(options = {}); end
|
23
15
|
|
24
|
-
|
16
|
+
def self.create_console
|
17
|
+
extend Prompt::DSL
|
25
18
|
|
26
|
-
|
27
|
-
command "test" do
|
28
|
-
puts 'Im a test.!'.yellow
|
29
|
-
end
|
19
|
+
group 'Console commands'
|
30
20
|
|
21
|
+
desc 'test'
|
22
|
+
command 'test' do
|
23
|
+
# puts 'Im a test.!'.yellow
|
24
|
+
true
|
25
|
+
end
|
31
26
|
end
|
32
27
|
|
33
|
-
|
34
28
|
def self.run_console
|
35
|
-
|
36
|
-
|
37
|
-
|
29
|
+
Prompt.application.prompt = "#{Tools.configuration.console_prompt} console > ".light_green
|
30
|
+
@history_file = File.join(__dir__.to_s, '.workin-history')
|
31
|
+
Prompt::Console.start @history_file
|
38
32
|
end
|
39
|
-
|
40
33
|
end
|
41
34
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
35
|
# class Console
|
50
36
|
|
51
|
-
#
|
52
|
-
#
|
53
|
-
#
|
37
|
+
# def initialize(options = {})
|
38
|
+
# commands
|
39
|
+
# end
|
54
40
|
|
55
41
|
# def commands
|
56
42
|
|
57
|
-
#
|
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
|
-
#
|
43
|
+
# extend Prompt::DSL
|
44
|
+
|
45
|
+
# group "Commands"
|
46
|
+
|
47
|
+
# desc "config"
|
48
|
+
# command "config" do ||
|
49
|
+
# Prompt.application.prompt = " tools > ".light_blue
|
50
|
+
# end
|
51
|
+
|
52
|
+
# desc "show"
|
53
|
+
# command "show :param" do |param|
|
54
|
+
# case param
|
55
|
+
# when 'config'
|
56
|
+
# puts "ROOT .: #{Tools.root}"
|
57
|
+
# puts "USER .: #{Tools.configuration.user}"
|
58
|
+
# puts "HOME .: #{Tools.configuration.home}"
|
59
|
+
# puts "PWD .: #{Tools.configuration.pwd}"
|
60
|
+
# puts "ldap_user .: #{Tools.configuration.ldap_user}"
|
61
|
+
# puts "ldap_pass .: #{Tools.configuration.ldap_pass}"
|
62
|
+
# Tools.configuration.info.each do |k,v|
|
63
|
+
# ap k
|
64
|
+
# ap v
|
65
|
+
# end
|
66
|
+
# else
|
67
|
+
# ap Tools.get_variable param
|
68
|
+
# end
|
69
|
+
# end
|
70
|
+
|
71
|
+
# desc "rsync"
|
72
|
+
# command "rsync :from :to" do |from, to|
|
87
73
|
|
88
74
|
# ap Tools.configuration.info[:directorys_to][from].nil?
|
89
75
|
|
90
|
-
#
|
91
|
-
#
|
92
|
-
#
|
93
|
-
#
|
94
|
-
#
|
95
|
-
#
|
96
|
-
#
|
97
|
-
#
|
98
|
-
#
|
76
|
+
# unless Tools.configuration.info[:directorys_to][from].nil?
|
77
|
+
# from = Tools.configuration.info[:directorys_to][from]
|
78
|
+
# from += ask("?? ")
|
79
|
+
# else
|
80
|
+
# sourcefiles = File.join("**", from)
|
81
|
+
# Dir.glob(sourcefiles).each do |source|
|
82
|
+
# ap source
|
83
|
+
# end
|
84
|
+
# end
|
99
85
|
|
100
86
|
# ap from
|
101
87
|
|
102
|
-
#
|
103
|
-
#
|
104
|
-
#
|
105
|
-
#
|
106
|
-
|
107
|
-
#
|
108
|
-
|
109
|
-
#
|
110
|
-
#
|
111
|
-
#
|
112
|
-
#
|
113
|
-
#
|
114
|
-
#
|
115
|
-
#
|
116
|
-
#
|
117
|
-
#
|
118
|
-
#
|
119
|
-
#
|
120
|
-
#
|
121
|
-
#
|
122
|
-
#
|
123
|
-
#
|
124
|
-
#
|
125
|
-
|
126
|
-
|
127
|
-
#
|
128
|
-
#
|
129
|
-
#
|
130
|
-
#
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
#
|
135
|
-
# history_file = ".tools-history"
|
136
|
-
# Prompt::Console.start history_file
|
88
|
+
# # if File.file?(source)
|
89
|
+
# # result = Rsync.run(source, dest)
|
90
|
+
# # ap result
|
91
|
+
# # end
|
92
|
+
|
93
|
+
# end
|
94
|
+
|
95
|
+
# desc "connect"
|
96
|
+
# command "connect :host" do |host|
|
97
|
+
# if host.split('@').size == 2
|
98
|
+
# user = host.split('@')[0]
|
99
|
+
# host = host.split('@')[1]
|
100
|
+
# else
|
101
|
+
# unless Tools.configuration.info[:servers][host].nil?
|
102
|
+
# user = Tools.configuration.info[:servers][host].split('@')[0]
|
103
|
+
# host = Tools.configuration.info[:servers][host].split('@')[1]
|
104
|
+
# else
|
105
|
+
# ap "No hosts selected! Exiting..."
|
106
|
+
# end
|
107
|
+
# end
|
108
|
+
# ssh = Tools.ssh_connect_knowhost host, user
|
109
|
+
# Tools.set_variable 'ssh', ssh
|
110
|
+
# end
|
111
|
+
|
112
|
+
# desc "cmd"
|
113
|
+
# command "cmd :variable :command " do |variable, command|
|
114
|
+
# ssh = Tools.get_variable 'ssh'
|
115
|
+
# Tools.set_variable variable, (Tools.ssh_cmd ssh, command).split("\n")
|
116
|
+
# end
|
117
|
+
|
118
|
+
# Prompt.application.prompt = " tools > ".light_red
|
119
|
+
# history_file = ".tools-history"
|
120
|
+
# Prompt::Console.start history_file
|
137
121
|
|
138
122
|
# end
|
139
123
|
|
data/lib/lib/display.rb
CHANGED
@@ -2,8 +2,7 @@ require 'singleton'
|
|
2
2
|
class ToolsDisplay
|
3
3
|
include Singleton
|
4
4
|
|
5
|
-
def initialize(options = {})
|
6
|
-
end
|
5
|
+
def initialize(options = {}); end
|
7
6
|
|
8
7
|
# Tools to awesome prints
|
9
8
|
#
|
@@ -14,41 +13,23 @@ class ToolsDisplay
|
|
14
13
|
#
|
15
14
|
# @param arguments
|
16
15
|
# @return [String] printed
|
17
|
-
def self.show
|
16
|
+
def self.show(*arguments)
|
18
17
|
post = arguments[0]
|
19
|
-
|
20
|
-
return post.class.to_s
|
21
|
-
end
|
18
|
+
return post.class.to_s unless post.is_a? String
|
22
19
|
|
23
20
|
color = arguments.extract_color
|
24
21
|
sameline = arguments.extract_symbol :sameline
|
25
|
-
#nocolor = arguments.extract_symbol :nocolor
|
26
22
|
colorized = arguments.extract_symbol :colorized
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
if colorized
|
33
|
-
printf "#{post}"
|
34
|
-
else
|
35
|
-
printf "#{post}".colorize(color)
|
36
|
-
end
|
37
|
-
|
38
|
-
# unless nocolor
|
39
|
-
# printf "#{post}".colorize(color)
|
23
|
+
post += "\n" unless sameline
|
24
|
+
colorized ? printf(post.to_s) : printf(post.to_s.colorize(color))
|
25
|
+
# if colorized
|
26
|
+
# printf post.to_s
|
40
27
|
# else
|
41
|
-
#
|
42
|
-
# ap post
|
43
|
-
# else
|
44
|
-
# printf "#{post}"
|
45
|
-
# end
|
28
|
+
# printf post.to_s.colorize(color)
|
46
29
|
# end
|
47
|
-
|
48
30
|
end
|
49
31
|
|
50
|
-
def self.show_colorize
|
32
|
+
def self.show_colorize(*arguments)
|
51
33
|
puts arguments.first
|
52
34
|
end
|
53
|
-
|
54
|
-
end
|
35
|
+
end
|