raykit 0.0.504 → 0.0.505

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +21 -21
  3. data/README.md +25 -25
  4. data/bin/raykit +6 -6
  5. data/lib/raykit/auto_setup.rb +69 -69
  6. data/lib/raykit/command.rb +373 -373
  7. data/lib/raykit/conan/buildinfo.rb +69 -69
  8. data/lib/raykit/conanpackage.rb +49 -49
  9. data/lib/raykit/configuration.rb +53 -53
  10. data/lib/raykit/console.rb +318 -314
  11. data/lib/raykit/default_content.rb +227 -227
  12. data/lib/raykit/dir.rb +49 -49
  13. data/lib/raykit/dotnet.rb +174 -174
  14. data/lib/raykit/environment.rb +114 -114
  15. data/lib/raykit/filesystem.rb +34 -34
  16. data/lib/raykit/git/commit.rb +16 -16
  17. data/lib/raykit/git/directory.rb +216 -216
  18. data/lib/raykit/git/files.rb +46 -46
  19. data/lib/raykit/git/repositories.rb +89 -89
  20. data/lib/raykit/git/repository.rb +376 -376
  21. data/lib/raykit/installer.rb +17 -17
  22. data/lib/raykit/log.rb +80 -80
  23. data/lib/raykit/logevent.rb +49 -49
  24. data/lib/raykit/logger.rb +98 -0
  25. data/lib/raykit/logging.rb +57 -57
  26. data/lib/raykit/markdown.rb +21 -21
  27. data/lib/raykit/msbuild.rb +54 -54
  28. data/lib/raykit/nugetpackage.rb +54 -54
  29. data/lib/raykit/nunit.rb +13 -13
  30. data/lib/raykit/project.rb +343 -343
  31. data/lib/raykit/rake.rb +39 -39
  32. data/lib/raykit/runner.rb +42 -42
  33. data/lib/raykit/secrets.rb +38 -38
  34. data/lib/raykit/sourceImport.rb +76 -76
  35. data/lib/raykit/sourceImports.rb +43 -43
  36. data/lib/raykit/string.rb +18 -18
  37. data/lib/raykit/symbols.rb +115 -16
  38. data/lib/raykit/tasks.rb +99 -99
  39. data/lib/raykit/text.rb +32 -32
  40. data/lib/raykit/timer.rb +31 -31
  41. data/lib/raykit/version.rb +89 -103
  42. data/lib/raykit/vstest.rb +24 -24
  43. data/lib/raykit/wt.rb +28 -28
  44. data/lib/raykit/zip.rb +73 -73
  45. data/lib/raykit.rb +126 -125
  46. metadata +4 -3
@@ -1,373 +1,373 @@
1
- require "open3"
2
- require "timeout"
3
- require "json"
4
- require "yaml"
5
- require "logger"
6
- require "securerandom"
7
-
8
- module Raykit
9
- # Functionality to support executing and logging system commands
10
- class Command
11
- @@commands = []
12
- # The timeout in seconds, defaults to 0 if not specified
13
- attr_accessor :timeout
14
- # The working directory, defaults the current directory if not specified
15
- attr_accessor :directory
16
- # The start time
17
- attr_accessor :start_time
18
- # The execution time in seconds
19
- attr_accessor :elapsed
20
- attr_accessor :command, :output, :error, :exitstatus, :user, :machine, :logging_enabled, :success_log_level
21
-
22
- def init_defaults
23
- @timeout = 0
24
- @directory = Dir.pwd
25
- @output = +""
26
- @error = +""
27
- @exitstatus = 0
28
- @user = Environment.user
29
- @machine = Environment.machine
30
- @logging_enabled = true
31
- end
32
-
33
- def initialize(command)
34
- #def initialize(command, timeout = 0, success_log_level = Logger::DEBUG, logging_enabled = true)
35
- timeout = 0
36
- success_log_level = nil
37
- logging_enabled = false
38
- @@commands = []
39
- init_defaults
40
- @success_log_level = success_log_level
41
- @logging_enabled = logging_enabled
42
- @command = command
43
- @timeout = timeout
44
- #t = Time.now
45
- @elapsed = 0
46
- #run if @command.length.positive?
47
- self
48
- end
49
-
50
- def set_timeout(timeout)
51
- @timeout = timeout
52
- self
53
- end
54
-
55
- def run
56
- # puts '---running---'
57
- @start_time = Time.now
58
- @elapsed = 0
59
- timer = Timer.new
60
- if @timeout.zero?
61
- @output, @error, process_status = Open3.capture3(@command)
62
- @exitstatus = process_status.exitstatus
63
- else
64
- # =================================================
65
- #puts "@timeout is #{@timeout}"
66
- Open3.popen3(@command, chdir: @directory) do |_stdin, stdout, stderr, thread|
67
- tick = 1
68
- pid = thread.pid
69
- start = Time.now
70
- while ((Time.now - start) < @timeout) && thread.alive?
71
- Kernel.select([stdout, stderr], nil, nil, tick)
72
- begin
73
- @output << stdout.read_nonblock(BUFFER_SIZE)
74
- #@error << stderr.read_nonblock(BUFFER_SIZE)
75
- rescue IO::WaitReadable
76
- rescue EOFError
77
- #puts "rescue block entered"
78
- @exitstatus = thread.value.exitstatus
79
- until stdout.eof?
80
- @output << stdout.read_nonblock(BUFFER_SIZE)
81
- end
82
- until stderr.eof?
83
- @error << stderr.read_nonblock(BUFFER_SIZE)
84
- end
85
- break
86
- end
87
-
88
- #begin
89
- # @output << stdout.read_nonblock(BUFFER_SIZE)
90
- # @error << stderr.read_nonblock(BUFFER_SIZE)
91
- #rescue IO::WaitReadable
92
- #rescue EOFError
93
- # @exitstatus = thread.value.exitstatus
94
- # until stdout.eof?
95
- # @output << stdout.read_nonblock(BUFFER_SIZE)
96
- # end
97
- # until stderr.eof?
98
- # @error << stderr.read_nonblock(BUFFER_SIZE)
99
- # end
100
- # break
101
- #end
102
- end
103
- sleep 1
104
- if thread.alive?
105
- if Gem.win_platform?
106
- `taskkill /f /pid #{pid}`
107
- else
108
- Process.kill("TERM", pid)
109
- end
110
- @exitstatus = 5
111
- @error = +"timed out"
112
- else
113
- @exitstatus = thread.value.exitstatus
114
- end
115
- end
116
-
117
- # =================================================
118
- end
119
- @elapsed = timer.elapsed
120
- if @logging_enabled
121
- log
122
- if @exitstatus != 0
123
- to_log_event.to_seq
124
- else
125
- # puts '---logging---'
126
- unless @success_log_level.nil?
127
- e = to_log_event
128
- e["Level"] = "Verbose" if @success_log_level == "Verbose"
129
- e["Level"] = "Debug" if @success_log_level == Logger::DEBUG
130
- e["Level"] = "Information" if @success_log_level == Logger::INFO
131
- e["Level"] = "Warning" if @elapsed > 60 * 2
132
- e.to_seq
133
- end
134
- end
135
- end
136
- self
137
- end
138
-
139
- def to_log_event
140
- secrets = Secrets.new
141
- msg = secrets.hide(@command)
142
- level = "Verbose"
143
- level = "Warning" if @exitstatus != 0
144
- output = @output
145
- error = @error
146
- output = @output[-1000..-1] if @output.length > 1200
147
- error = @error[-1000..-1] if @error.length > 1200
148
- Raykit::LogEvent.new(level, msg, {
149
- "SourceContext" => "Raykit::Command",
150
- "Category" => "Command",
151
- "Timeout" => @timeout,
152
- "Directory" => @directory,
153
- "Output" => output,
154
- "Error" => error,
155
- "ExitStatus" => @exitstatus,
156
- "Elapsed" => elapsed_str,
157
- "ElapsedSeconds" => @elapsed,
158
- })
159
- end
160
-
161
- def log
162
- # --- Rolling File JSON -----
163
- log = Logger.new("#{Raykit::Environment.log_dir}/Raykit.Commands.txt", "daily")
164
- log.formatter = proc do |_severity, _datetime, _progname, msg|
165
- "#{msg}\n"
166
- end
167
- secrets = Secrets.new
168
- msg = secrets.hide(@command) # "?"# self.summary(false)
169
- level = "Verbose"
170
- level = "Warning" if @exitstatus != 0
171
- event = Raykit::LogEvent.new(level, msg, {
172
- "Timeout" => @timeout,
173
- "Directory" => @directory,
174
- "Output" => @output,
175
- "Error" => @error,
176
- "ExitStatus" => @exitstatus,
177
- "Elapsed" => elapsed_str,
178
- })
179
- log.info event.to_json
180
- # ---------------------------
181
- begin
182
- json = JSON.generate(to_hash)
183
- log_filename = "#{Environment.get_dev_dir("log")}/Raykit.Command/#{SecureRandom.uuid}.json"
184
- log_dir = File.dirname(log_filename)
185
- FileUtils.mkdir_p(log_dir) unless Dir.exist?(log_dir)
186
- File.open(log_filename, "w") { |f| f.write(json) }
187
- if !@exitstatus.nil? && @exitstatus.zero?
188
- LOG.log("Raykit.Command", Logger::Severity::INFO, json)
189
- else
190
- LOG.log("Raykit.Command", Logger::Severity::ERROR, json)
191
- end
192
- rescue JSON::GeneratorError => e
193
- puts to_hash.to_s
194
- puts e.to_s
195
- json = JSON.generate(to_hash)
196
- end
197
- end
198
-
199
- def elapsed_str
200
- if elapsed < 1.0
201
- end
202
- "#{format("%.0f", elapsed)}s"
203
- end
204
-
205
- def summary(show_directory = false)
206
- #checkmark = "\u2713"
207
- # warning="\u26A0"
208
- #error = "\u0058"
209
- #symbol = Rainbow(checkmark.encode("utf-8")).green
210
- #symbol = Rainbow(error.encode("utf-8")).red if @exitstatus != 0
211
- symbol = Raykit::Symbols::checkmark if @exitstatus.zero?
212
- symbol = Raykit::Symbols::error if @exitstatus != 0
213
- cmd = "#{Rainbow(SECRETS.hide(@command)).yellow}"
214
- if show_directory
215
- puts "#{symbol} #{cmd} " + Rainbow("#{elapsed_str}").cyan
216
- puts Rainbow(" #{@directory}").white + " "
217
- else
218
- puts "#{symbol} #{Rainbow(SECRETS.hide(@command)).yellow} " + Rainbow("#{elapsed_str}").cyan
219
- #puts "#{symbol} #{Rainbow(SECRETS.hide(@command)).yellow} (#{elapsed_str})"
220
- end
221
- self
222
- end
223
-
224
- def details_on_failure
225
- if @exitstatus != 0
226
- details
227
- end
228
- self
229
- end
230
-
231
- def details
232
- #puts " exit_code: " + @exitstatus.to_s
233
- summary
234
- if @output.length > 0
235
- @output.lines.each do |line|
236
- puts " " + line
237
- end
238
- end
239
- if @error.length > 0
240
- @error.lines.each do |line|
241
- puts " " + line
242
- end
243
- end
244
- self
245
- end
246
-
247
- def to_markdown
248
- checkmark = "\u2713"
249
- error = "\u0058"
250
- symbol = checkmark.encode("utf-8")
251
- symbol = error.encode("utf-8") if @exitstatus != 0
252
- cmd = "#{SECRETS.hide(@command)}"
253
- md = "#{symbol} #{SECRETS.hide(@command)} (#{elapsed_str})"
254
- md
255
- end
256
-
257
- def save
258
- filename = "#{Environment.get_dev_dir("log")}/Commands/#{SecureRandom.uuid}"
259
- log_dir = File.dirname(filename)
260
- FileUtils.mkdir_p(log_dir) unless Dir.exist?(log_dir)
261
-
262
- File.open(filename, "w") do |f|
263
- f.write(JSON.pretty_generate(to_hash))
264
- end
265
- self
266
- end
267
-
268
- def save_as(filename)
269
- File.open(filename, "w") do |f|
270
- f.write(JSON.pretty_generate(to_hash))
271
- end
272
- self
273
- end
274
-
275
- def log_to_file(filename)
276
- File.delete(filename) if File.exist?(filename)
277
- File.open(filename, "w") { |f|
278
- f.puts output
279
- f.puts error
280
- }
281
- self
282
- end
283
-
284
- def to_hash
285
- hash = {}
286
- hash[:command] = @command
287
- hash[:directory] = @directory
288
- hash[:timeout] = @timeout
289
- hash[:start_time] = @start_time
290
- hash[:elapsed] = @elapsed
291
- hash[:output] = @output.force_encoding("ISO-8859-1").encode("UTF-8")
292
- hash[:error] = @error.force_encoding("ISO-8859-1").encode("UTF-8")
293
- hash[:exitstatus] = @exitstatus
294
- hash[:user] = @user
295
- hash[:machine] = @machine
296
- hash[:context] = "Raykit.Command"
297
- hash
298
- end
299
-
300
- def from_hash(hash)
301
- @command = hash["command"]
302
- @directory = hash["directory"]
303
- @timeout = hash["timeout"]
304
- @start_time = hash["start_time"]
305
- @elapsed = hash["elapsed"]
306
- @output = hash["output"]
307
- @error = hash["error"]
308
- @exitstatus = hash["exitstatus"]
309
- @user = hash["user"] if hash.include?("user")
310
- @machine = hash["machine"] if hash.include?("machine")
311
- end
312
-
313
- def self.parse(json)
314
- cmd = Command.new("")
315
- cmd.from_hash(JSON.parse(json))
316
- cmd
317
- end
318
-
319
- def self.parse_yaml_commands(yaml)
320
- # commands=Array.new()
321
- data = YAML.safe_load(yaml)
322
- commands = get_script_commands(data)
323
- end
324
-
325
- def self.get_script_commands(hash)
326
- commands = []
327
- if hash.key?("script")
328
- hash["script"].each do |cmd|
329
- commands << cmd
330
- end
331
- end
332
- hash.each do |_k, v|
333
- next unless v.is_a?(Hash)
334
-
335
- subcommands = get_script_commands(v)
336
- subcommands.each do |c|
337
- commands << c
338
- end
339
- end
340
- commands
341
- end # get_script_commands
342
-
343
- def to_s
344
- s = ""
345
- checkmark = "\u2713"
346
- # warning="\u26A0"
347
- error = "\u0058"
348
- symbol = Rainbow(checkmark.encode("utf-8")).green
349
- symbol = Rainbow(error.encode("utf-8")).red if @exitstatus != 0
350
- cmd = "#{Rainbow(SECRETS.hide(@command)).yellow}"
351
- #if !@exitstatus.zero?
352
- s += "#{symbol} #{cmd} " + Rainbow("#{elapsed_str}").cyan
353
- s += Rainbow(" #{@directory}").white + " "
354
- #else
355
- # s += "#{symbol} #{Rainbow(SECRETS.hide(@command)).yellow} " + Rainbow("#{elapsed_str}").cyan
356
- #end
357
-
358
- if !@exitstatus.zero?
359
- if @output.length > 0
360
- @output.lines.each do |line|
361
- s += " " + line
362
- end
363
- end
364
- if @error.length > 0
365
- @error.lines.each do |line|
366
- s += " " + line
367
- end
368
- end
369
- end
370
- s
371
- end
372
- end # class Command
373
- end # module Raykit
1
+ require "open3"
2
+ require "timeout"
3
+ require "json"
4
+ require "yaml"
5
+ require "logger"
6
+ require "securerandom"
7
+
8
+ module Raykit
9
+ # Functionality to support executing and logging system commands
10
+ class Command
11
+ @@commands = []
12
+ # The timeout in seconds, defaults to 0 if not specified
13
+ attr_accessor :timeout
14
+ # The working directory, defaults the current directory if not specified
15
+ attr_accessor :directory
16
+ # The start time
17
+ attr_accessor :start_time
18
+ # The execution time in seconds
19
+ attr_accessor :elapsed
20
+ attr_accessor :command, :output, :error, :exitstatus, :user, :machine, :logging_enabled, :success_log_level
21
+
22
+ def init_defaults
23
+ @timeout = 0
24
+ @directory = Dir.pwd
25
+ @output = +""
26
+ @error = +""
27
+ @exitstatus = 0
28
+ @user = Environment.user
29
+ @machine = Environment.machine
30
+ @logging_enabled = true
31
+ end
32
+
33
+ def initialize(command)
34
+ #def initialize(command, timeout = 0, success_log_level = Logger::DEBUG, logging_enabled = true)
35
+ timeout = 0
36
+ success_log_level = nil
37
+ logging_enabled = false
38
+ @@commands = []
39
+ init_defaults
40
+ @success_log_level = success_log_level
41
+ @logging_enabled = logging_enabled
42
+ @command = command
43
+ @timeout = timeout
44
+ #t = Time.now
45
+ @elapsed = 0
46
+ #run if @command.length.positive?
47
+ self
48
+ end
49
+
50
+ def set_timeout(timeout)
51
+ @timeout = timeout
52
+ self
53
+ end
54
+
55
+ def run
56
+ # puts '---running---'
57
+ @start_time = Time.now
58
+ @elapsed = 0
59
+ timer = Timer.new
60
+ if @timeout.zero?
61
+ @output, @error, process_status = Open3.capture3(@command)
62
+ @exitstatus = process_status.exitstatus
63
+ else
64
+ # =================================================
65
+ #puts "@timeout is #{@timeout}"
66
+ Open3.popen3(@command, chdir: @directory) do |_stdin, stdout, stderr, thread|
67
+ tick = 1
68
+ pid = thread.pid
69
+ start = Time.now
70
+ while ((Time.now - start) < @timeout) && thread.alive?
71
+ Kernel.select([stdout, stderr], nil, nil, tick)
72
+ begin
73
+ @output << stdout.read_nonblock(BUFFER_SIZE)
74
+ #@error << stderr.read_nonblock(BUFFER_SIZE)
75
+ rescue IO::WaitReadable
76
+ rescue EOFError
77
+ #puts "rescue block entered"
78
+ @exitstatus = thread.value.exitstatus
79
+ until stdout.eof?
80
+ @output << stdout.read_nonblock(BUFFER_SIZE)
81
+ end
82
+ until stderr.eof?
83
+ @error << stderr.read_nonblock(BUFFER_SIZE)
84
+ end
85
+ break
86
+ end
87
+
88
+ #begin
89
+ # @output << stdout.read_nonblock(BUFFER_SIZE)
90
+ # @error << stderr.read_nonblock(BUFFER_SIZE)
91
+ #rescue IO::WaitReadable
92
+ #rescue EOFError
93
+ # @exitstatus = thread.value.exitstatus
94
+ # until stdout.eof?
95
+ # @output << stdout.read_nonblock(BUFFER_SIZE)
96
+ # end
97
+ # until stderr.eof?
98
+ # @error << stderr.read_nonblock(BUFFER_SIZE)
99
+ # end
100
+ # break
101
+ #end
102
+ end
103
+ sleep 1
104
+ if thread.alive?
105
+ if Gem.win_platform?
106
+ `taskkill /f /pid #{pid}`
107
+ else
108
+ Process.kill("TERM", pid)
109
+ end
110
+ @exitstatus = 5
111
+ @error = +"timed out"
112
+ else
113
+ @exitstatus = thread.value.exitstatus
114
+ end
115
+ end
116
+
117
+ # =================================================
118
+ end
119
+ @elapsed = timer.elapsed
120
+ if @logging_enabled
121
+ log
122
+ if @exitstatus != 0
123
+ to_log_event.to_seq
124
+ else
125
+ # puts '---logging---'
126
+ unless @success_log_level.nil?
127
+ e = to_log_event
128
+ e["Level"] = "Verbose" if @success_log_level == "Verbose"
129
+ e["Level"] = "Debug" if @success_log_level == Logger::DEBUG
130
+ e["Level"] = "Information" if @success_log_level == Logger::INFO
131
+ e["Level"] = "Warning" if @elapsed > 60 * 2
132
+ e.to_seq
133
+ end
134
+ end
135
+ end
136
+ self
137
+ end
138
+
139
+ def to_log_event
140
+ secrets = Secrets.new
141
+ msg = secrets.hide(@command)
142
+ level = "Verbose"
143
+ level = "Warning" if @exitstatus != 0
144
+ output = @output
145
+ error = @error
146
+ output = @output[-1000..-1] if @output.length > 1200
147
+ error = @error[-1000..-1] if @error.length > 1200
148
+ Raykit::LogEvent.new(level, msg, {
149
+ "SourceContext" => "Raykit::Command",
150
+ "Category" => "Command",
151
+ "Timeout" => @timeout,
152
+ "Directory" => @directory,
153
+ "Output" => output,
154
+ "Error" => error,
155
+ "ExitStatus" => @exitstatus,
156
+ "Elapsed" => elapsed_str,
157
+ "ElapsedSeconds" => @elapsed,
158
+ })
159
+ end
160
+
161
+ def log
162
+ # --- Rolling File JSON -----
163
+ log = Logger.new("#{Raykit::Environment.log_dir}/Raykit.Commands.txt", "daily")
164
+ log.formatter = proc do |_severity, _datetime, _progname, msg|
165
+ "#{msg}\n"
166
+ end
167
+ secrets = Secrets.new
168
+ msg = secrets.hide(@command) # "?"# self.summary(false)
169
+ level = "Verbose"
170
+ level = "Warning" if @exitstatus != 0
171
+ event = Raykit::LogEvent.new(level, msg, {
172
+ "Timeout" => @timeout,
173
+ "Directory" => @directory,
174
+ "Output" => @output,
175
+ "Error" => @error,
176
+ "ExitStatus" => @exitstatus,
177
+ "Elapsed" => elapsed_str,
178
+ })
179
+ log.info event.to_json
180
+ # ---------------------------
181
+ begin
182
+ json = JSON.generate(to_hash)
183
+ log_filename = "#{Environment.get_dev_dir("log")}/Raykit.Command/#{SecureRandom.uuid}.json"
184
+ log_dir = File.dirname(log_filename)
185
+ FileUtils.mkdir_p(log_dir) unless Dir.exist?(log_dir)
186
+ File.open(log_filename, "w") { |f| f.write(json) }
187
+ if !@exitstatus.nil? && @exitstatus.zero?
188
+ LOG.log("Raykit.Command", Logger::Severity::INFO, json)
189
+ else
190
+ LOG.log("Raykit.Command", Logger::Severity::ERROR, json)
191
+ end
192
+ rescue JSON::GeneratorError => e
193
+ puts to_hash.to_s
194
+ puts e.to_s
195
+ json = JSON.generate(to_hash)
196
+ end
197
+ end
198
+
199
+ def elapsed_str
200
+ if elapsed < 1.0
201
+ end
202
+ "#{format("%.0f", elapsed)}s"
203
+ end
204
+
205
+ def summary(show_directory = false)
206
+ #checkmark = "\u2713"
207
+ # warning="\u26A0"
208
+ #error = "\u0058"
209
+ #symbol = Rainbow(checkmark.encode("utf-8")).green
210
+ #symbol = Rainbow(error.encode("utf-8")).red if @exitstatus != 0
211
+ symbol = Raykit::Symbols::checkmark if @exitstatus.zero?
212
+ symbol = Raykit::Symbols::error if @exitstatus != 0
213
+ cmd = "#{Rainbow(SECRETS.hide(@command)).yellow}"
214
+ if show_directory
215
+ puts "#{symbol} #{cmd} " + Rainbow("#{elapsed_str}").cyan
216
+ puts Rainbow(" #{@directory}").white + " "
217
+ else
218
+ puts "#{symbol} #{Rainbow(SECRETS.hide(@command)).yellow} " + Rainbow("#{elapsed_str}").cyan
219
+ #puts "#{symbol} #{Rainbow(SECRETS.hide(@command)).yellow} (#{elapsed_str})"
220
+ end
221
+ self
222
+ end
223
+
224
+ def details_on_failure
225
+ if @exitstatus != 0
226
+ details
227
+ end
228
+ self
229
+ end
230
+
231
+ def details
232
+ #puts " exit_code: " + @exitstatus.to_s
233
+ summary
234
+ if @output.length > 0
235
+ @output.lines.each do |line|
236
+ puts " " + line
237
+ end
238
+ end
239
+ if @error.length > 0
240
+ @error.lines.each do |line|
241
+ puts " " + line
242
+ end
243
+ end
244
+ self
245
+ end
246
+
247
+ def to_markdown
248
+ checkmark = "\u2713"
249
+ error = "\u0058"
250
+ symbol = checkmark.encode("utf-8")
251
+ symbol = error.encode("utf-8") if @exitstatus != 0
252
+ cmd = "#{SECRETS.hide(@command)}"
253
+ md = "#{symbol} #{SECRETS.hide(@command)} (#{elapsed_str})"
254
+ md
255
+ end
256
+
257
+ def save
258
+ filename = "#{Environment.get_dev_dir("log")}/Commands/#{SecureRandom.uuid}"
259
+ log_dir = File.dirname(filename)
260
+ FileUtils.mkdir_p(log_dir) unless Dir.exist?(log_dir)
261
+
262
+ File.open(filename, "w") do |f|
263
+ f.write(JSON.pretty_generate(to_hash))
264
+ end
265
+ self
266
+ end
267
+
268
+ def save_as(filename)
269
+ File.open(filename, "w") do |f|
270
+ f.write(JSON.pretty_generate(to_hash))
271
+ end
272
+ self
273
+ end
274
+
275
+ def log_to_file(filename)
276
+ File.delete(filename) if File.exist?(filename)
277
+ File.open(filename, "w") { |f|
278
+ f.puts output
279
+ f.puts error
280
+ }
281
+ self
282
+ end
283
+
284
+ def to_hash
285
+ hash = {}
286
+ hash[:command] = @command
287
+ hash[:directory] = @directory
288
+ hash[:timeout] = @timeout
289
+ hash[:start_time] = @start_time
290
+ hash[:elapsed] = @elapsed
291
+ hash[:output] = @output.force_encoding("ISO-8859-1").encode("UTF-8")
292
+ hash[:error] = @error.force_encoding("ISO-8859-1").encode("UTF-8")
293
+ hash[:exitstatus] = @exitstatus
294
+ hash[:user] = @user
295
+ hash[:machine] = @machine
296
+ hash[:context] = "Raykit.Command"
297
+ hash
298
+ end
299
+
300
+ def from_hash(hash)
301
+ @command = hash["command"]
302
+ @directory = hash["directory"]
303
+ @timeout = hash["timeout"]
304
+ @start_time = hash["start_time"]
305
+ @elapsed = hash["elapsed"]
306
+ @output = hash["output"]
307
+ @error = hash["error"]
308
+ @exitstatus = hash["exitstatus"]
309
+ @user = hash["user"] if hash.include?("user")
310
+ @machine = hash["machine"] if hash.include?("machine")
311
+ end
312
+
313
+ def self.parse(json)
314
+ cmd = Command.new("")
315
+ cmd.from_hash(JSON.parse(json))
316
+ cmd
317
+ end
318
+
319
+ def self.parse_yaml_commands(yaml)
320
+ # commands=Array.new()
321
+ data = YAML.safe_load(yaml)
322
+ commands = get_script_commands(data)
323
+ end
324
+
325
+ def self.get_script_commands(hash)
326
+ commands = []
327
+ if hash.key?("script")
328
+ hash["script"].each do |cmd|
329
+ commands << cmd
330
+ end
331
+ end
332
+ hash.each do |_k, v|
333
+ next unless v.is_a?(Hash)
334
+
335
+ subcommands = get_script_commands(v)
336
+ subcommands.each do |c|
337
+ commands << c
338
+ end
339
+ end
340
+ commands
341
+ end # get_script_commands
342
+
343
+ def to_s
344
+ s = ""
345
+ checkmark = "\u2713"
346
+ # warning="\u26A0"
347
+ error = "\u0058"
348
+ symbol = Rainbow(checkmark.encode("utf-8")).green
349
+ symbol = Rainbow(error.encode("utf-8")).red if @exitstatus != 0
350
+ cmd = "#{Rainbow(SECRETS.hide(@command)).yellow}"
351
+ #if !@exitstatus.zero?
352
+ s += "#{symbol} #{cmd} " + Rainbow("#{elapsed_str}").cyan
353
+ s += Rainbow(" #{@directory}").white + " "
354
+ #else
355
+ # s += "#{symbol} #{Rainbow(SECRETS.hide(@command)).yellow} " + Rainbow("#{elapsed_str}").cyan
356
+ #end
357
+
358
+ if !@exitstatus.zero?
359
+ if @output.length > 0
360
+ @output.lines.each do |line|
361
+ s += " " + line
362
+ end
363
+ end
364
+ if @error.length > 0
365
+ @error.lines.each do |line|
366
+ s += " " + line
367
+ end
368
+ end
369
+ end
370
+ s
371
+ end
372
+ end # class Command
373
+ end # module Raykit