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.
@@ -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 config_name, config_file, config_type = :json, data = {}
27
- unless File.exists? config_file
28
- case config_type
29
- when :json
30
- config = File.open(config_file, 'w')
31
- config.write JSON.pretty_generate(data)
32
- config.close
33
- #ToolsLog.tools_info "Json config file '#{config_file}' was created!'"
34
- #return true
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
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
- def self.method_missing(method, *args, &block)
52
- #expected call format => STRING_LOGGER_TYPE + '_' + LOGGER_TYPE
53
- # Ex.: tools_info
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 source
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
- config_type = :json
69
- else
70
- if ToolsUtil.valid_yaml? content
71
- config_type = :yaml
72
- else
73
- config_type = :invalid
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 source
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 = open(source)
96
- parsed = YAML.load(file.read)
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 source, command
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.load(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 *args
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.load(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.validate_config
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
@@ -2,138 +2,122 @@ require 'singleton'
2
2
  class ToolsConsole
3
3
  include Singleton
4
4
 
5
- def self.exec_console args
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
- unless cmd.nil?
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
- def initialize(options = {})
10
+ cmd.run cmd
11
+ true
18
12
  end
19
13
 
20
- def self.create_console
21
-
22
- extend Prompt::DSL
14
+ def initialize(options = {}); end
23
15
 
24
- group "Console commands"
16
+ def self.create_console
17
+ extend Prompt::DSL
25
18
 
26
- desc "test"
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
- Prompt.application.prompt = "#{Tools.configuration.console_prompt} console > ".light_green
36
- @history_file = File.join(File.expand_path(File.dirname(__FILE__)).to_s, ".workin-history")
37
- Prompt::Console.start @history_file
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
- # def initialize(options = {})
52
- # commands
53
- # end
37
+ # def initialize(options = {})
38
+ # commands
39
+ # end
54
40
 
55
41
  # def commands
56
42
 
57
- # extend Prompt::DSL
58
-
59
- # group "Commands"
60
-
61
- # desc "config"
62
- # command "config" do ||
63
- # Prompt.application.prompt = " tools > ".light_blue
64
- # end
65
-
66
- # desc "show"
67
- # command "show :param" do |param|
68
- # case param
69
- # when 'config'
70
- # puts "ROOT .: #{Tools.root}"
71
- # puts "USER .: #{Tools.configuration.user}"
72
- # puts "HOME .: #{Tools.configuration.home}"
73
- # puts "PWD .: #{Tools.configuration.pwd}"
74
- # puts "ldap_user .: #{Tools.configuration.ldap_user}"
75
- # puts "ldap_pass .: #{Tools.configuration.ldap_pass}"
76
- # Tools.configuration.info.each do |k,v|
77
- # ap k
78
- # ap v
79
- # end
80
- # else
81
- # ap Tools.get_variable param
82
- # end
83
- # end
84
-
85
- # desc "rsync"
86
- # command "rsync :from :to" do |from, to|
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
- # unless Tools.configuration.info[:directorys_to][from].nil?
91
- # from = Tools.configuration.info[:directorys_to][from]
92
- # from += ask("?? ")
93
- # else
94
- # sourcefiles = File.join("**", from)
95
- # Dir.glob(sourcefiles).each do |source|
96
- # ap source
97
- # end
98
- # end
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
- # # if File.file?(source)
103
- # # result = Rsync.run(source, dest)
104
- # # ap result
105
- # # end
106
-
107
- # end
108
-
109
- # desc "connect"
110
- # command "connect :host" do |host|
111
- # if host.split('@').size == 2
112
- # user = host.split('@')[0]
113
- # host = host.split('@')[1]
114
- # else
115
- # unless Tools.configuration.info[:servers][host].nil?
116
- # user = Tools.configuration.info[:servers][host].split('@')[0]
117
- # host = Tools.configuration.info[:servers][host].split('@')[1]
118
- # else
119
- # ap "No hosts selected! Exiting..."
120
- # end
121
- # end
122
- # ssh = Tools.ssh_connect_knowhost host, user
123
- # Tools.set_variable 'ssh', ssh
124
- # end
125
-
126
-
127
- # desc "cmd"
128
- # command "cmd :variable :command " do |variable, command|
129
- # ssh = Tools.get_variable 'ssh'
130
- # Tools.set_variable variable, (Tools.ssh_cmd ssh, command).split("\n")
131
- # end
132
-
133
-
134
- # Prompt.application.prompt = " tools > ".light_red
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
 
@@ -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 *arguments
16
+ def self.show(*arguments)
18
17
  post = arguments[0]
19
- unless (post.class == String)
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
- unless sameline
29
- post += "\n"
30
- end
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
- # if colorized
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 *arguments
32
+ def self.show_colorize(*arguments)
51
33
  puts arguments.first
52
34
  end
53
-
54
- end
35
+ end