raykit 0.0.543 → 0.0.545

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) 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 +374 -374
  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 +310 -310
  11. data/lib/raykit/default_content.rb +8 -8
  12. data/lib/raykit/default_content.txt +219 -219
  13. data/lib/raykit/dir.rb +127 -127
  14. data/lib/raykit/dotnet.rb +207 -174
  15. data/lib/raykit/environment.rb +115 -115
  16. data/lib/raykit/filesystem.rb +34 -34
  17. data/lib/raykit/git/commit.rb +16 -16
  18. data/lib/raykit/git/directory.rb +279 -279
  19. data/lib/raykit/git/files.rb +46 -46
  20. data/lib/raykit/git/repositories.rb +89 -89
  21. data/lib/raykit/git/repository.rb +362 -362
  22. data/lib/raykit/installer.rb +17 -17
  23. data/lib/raykit/log.rb +80 -80
  24. data/lib/raykit/logevent.rb +29 -29
  25. data/lib/raykit/logger.rb +100 -100
  26. data/lib/raykit/logging.rb +57 -57
  27. data/lib/raykit/markdown.rb +21 -21
  28. data/lib/raykit/msbuild.rb +54 -54
  29. data/lib/raykit/nugetpackage.rb +54 -54
  30. data/lib/raykit/nunit.rb +13 -13
  31. data/lib/raykit/project.rb +343 -343
  32. data/lib/raykit/rake.rb +39 -39
  33. data/lib/raykit/runner.rb +42 -42
  34. data/lib/raykit/secrets.rb +38 -38
  35. data/lib/raykit/sourceImport.rb +76 -76
  36. data/lib/raykit/sourceImports.rb +43 -43
  37. data/lib/raykit/string.rb +18 -18
  38. data/lib/raykit/symbols.rb +115 -115
  39. data/lib/raykit/tasks.rb +91 -91
  40. data/lib/raykit/text.rb +32 -32
  41. data/lib/raykit/timer.rb +31 -31
  42. data/lib/raykit/version.rb +97 -95
  43. data/lib/raykit/vstest.rb +24 -24
  44. data/lib/raykit/wix.rb +61 -61
  45. data/lib/raykit/wt.rb +28 -28
  46. data/lib/raykit/zip.rb +73 -73
  47. data/lib/raykit.rb +143 -140
  48. metadata +3 -3
@@ -1,374 +1,374 @@
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::warning
212
- symbol = Raykit::Symbols::checkmark if !@exitstatus.nil? && @exitstatus.zero?
213
- symbol = Raykit::Symbols::error if @exitstatus != 0
214
- cmd = "#{Rainbow(SECRETS.hide(@command)).yellow}"
215
- if show_directory
216
- puts "#{symbol} #{cmd} " + Rainbow("#{elapsed_str}").cyan
217
- puts Rainbow(" #{@directory}").white + " "
218
- else
219
- puts "#{symbol} #{Rainbow(SECRETS.hide(@command)).yellow} " + Rainbow("#{elapsed_str}").cyan
220
- #puts "#{symbol} #{Rainbow(SECRETS.hide(@command)).yellow} (#{elapsed_str})"
221
- end
222
- self
223
- end
224
-
225
- def details_on_failure
226
- if @exitstatus != 0
227
- details
228
- end
229
- self
230
- end
231
-
232
- def details
233
- #puts " exit_code: " + @exitstatus.to_s
234
- summary
235
- if @output.length > 0
236
- @output.lines.each do |line|
237
- puts " " + line
238
- end
239
- end
240
- if @error.length > 0
241
- @error.lines.each do |line|
242
- puts " " + line
243
- end
244
- end
245
- self
246
- end
247
-
248
- def to_markdown
249
- checkmark = "\u2713"
250
- error = "\u0058"
251
- symbol = checkmark.encode("utf-8")
252
- symbol = error.encode("utf-8") if @exitstatus != 0
253
- cmd = "#{SECRETS.hide(@command)}"
254
- md = "#{symbol} #{SECRETS.hide(@command)} (#{elapsed_str})"
255
- md
256
- end
257
-
258
- def save
259
- filename = "#{Environment.get_dev_dir("log")}/Commands/#{SecureRandom.uuid}"
260
- log_dir = File.dirname(filename)
261
- FileUtils.mkdir_p(log_dir) unless Dir.exist?(log_dir)
262
-
263
- File.open(filename, "w") do |f|
264
- f.write(JSON.pretty_generate(to_hash))
265
- end
266
- self
267
- end
268
-
269
- def save_as(filename)
270
- File.open(filename, "w") do |f|
271
- f.write(JSON.pretty_generate(to_hash))
272
- end
273
- self
274
- end
275
-
276
- def log_to_file(filename)
277
- File.delete(filename) if File.exist?(filename)
278
- File.open(filename, "w") { |f|
279
- f.puts output
280
- f.puts error
281
- }
282
- self
283
- end
284
-
285
- def to_hash
286
- hash = {}
287
- hash[:command] = @command
288
- hash[:directory] = @directory
289
- hash[:timeout] = @timeout
290
- hash[:start_time] = @start_time
291
- hash[:elapsed] = @elapsed
292
- hash[:output] = @output.force_encoding("ISO-8859-1").encode("UTF-8")
293
- hash[:error] = @error.force_encoding("ISO-8859-1").encode("UTF-8")
294
- hash[:exitstatus] = @exitstatus
295
- hash[:user] = @user
296
- hash[:machine] = @machine
297
- hash[:context] = "Raykit.Command"
298
- hash
299
- end
300
-
301
- def from_hash(hash)
302
- @command = hash["command"]
303
- @directory = hash["directory"]
304
- @timeout = hash["timeout"]
305
- @start_time = hash["start_time"]
306
- @elapsed = hash["elapsed"]
307
- @output = hash["output"]
308
- @error = hash["error"]
309
- @exitstatus = hash["exitstatus"]
310
- @user = hash["user"] if hash.include?("user")
311
- @machine = hash["machine"] if hash.include?("machine")
312
- end
313
-
314
- def self.parse(json)
315
- cmd = Command.new("")
316
- cmd.from_hash(JSON.parse(json))
317
- cmd
318
- end
319
-
320
- def self.parse_yaml_commands(yaml)
321
- # commands=Array.new()
322
- data = YAML.safe_load(yaml)
323
- commands = get_script_commands(data)
324
- end
325
-
326
- def self.get_script_commands(hash)
327
- commands = []
328
- if hash.key?("script")
329
- hash["script"].each do |cmd|
330
- commands << cmd
331
- end
332
- end
333
- hash.each do |_k, v|
334
- next unless v.is_a?(Hash)
335
-
336
- subcommands = get_script_commands(v)
337
- subcommands.each do |c|
338
- commands << c
339
- end
340
- end
341
- commands
342
- end # get_script_commands
343
-
344
- def to_s
345
- s = ""
346
- checkmark = "\u2713"
347
- # warning="\u26A0"
348
- error = "\u0058"
349
- symbol = Rainbow(checkmark.encode("utf-8")).green
350
- symbol = Rainbow(error.encode("utf-8")).red if @exitstatus != 0
351
- cmd = "#{Rainbow(SECRETS.hide(@command)).yellow}"
352
- #if !@exitstatus.zero?
353
- s += "#{symbol} #{cmd} " + Rainbow("#{elapsed_str}").cyan
354
- s += Rainbow(" #{@directory}").white + " "
355
- #else
356
- # s += "#{symbol} #{Rainbow(SECRETS.hide(@command)).yellow} " + Rainbow("#{elapsed_str}").cyan
357
- #end
358
-
359
- if !@exitstatus.zero?
360
- if @output.length > 0
361
- @output.lines.each do |line|
362
- s += " " + line
363
- end
364
- end
365
- if @error.length > 0
366
- @error.lines.each do |line|
367
- s += " " + line
368
- end
369
- end
370
- end
371
- s
372
- end
373
- end # class Command
374
- 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::warning
212
+ symbol = Raykit::Symbols::checkmark if !@exitstatus.nil? && @exitstatus.zero?
213
+ symbol = Raykit::Symbols::error if @exitstatus != 0
214
+ cmd = "#{Rainbow(SECRETS.hide(@command)).yellow}"
215
+ if show_directory
216
+ puts "#{symbol} #{cmd} " + Rainbow("#{elapsed_str}").cyan
217
+ puts Rainbow(" #{@directory}").white + " "
218
+ else
219
+ puts "#{symbol} #{Rainbow(SECRETS.hide(@command)).yellow} " + Rainbow("#{elapsed_str}").cyan
220
+ #puts "#{symbol} #{Rainbow(SECRETS.hide(@command)).yellow} (#{elapsed_str})"
221
+ end
222
+ self
223
+ end
224
+
225
+ def details_on_failure
226
+ if @exitstatus != 0
227
+ details
228
+ end
229
+ self
230
+ end
231
+
232
+ def details
233
+ #puts " exit_code: " + @exitstatus.to_s
234
+ summary
235
+ if @output.length > 0
236
+ @output.lines.each do |line|
237
+ puts " " + line
238
+ end
239
+ end
240
+ if @error.length > 0
241
+ @error.lines.each do |line|
242
+ puts " " + line
243
+ end
244
+ end
245
+ self
246
+ end
247
+
248
+ def to_markdown
249
+ checkmark = "\u2713"
250
+ error = "\u0058"
251
+ symbol = checkmark.encode("utf-8")
252
+ symbol = error.encode("utf-8") if @exitstatus != 0
253
+ cmd = "#{SECRETS.hide(@command)}"
254
+ md = "#{symbol} #{SECRETS.hide(@command)} (#{elapsed_str})"
255
+ md
256
+ end
257
+
258
+ def save
259
+ filename = "#{Environment.get_dev_dir("log")}/Commands/#{SecureRandom.uuid}"
260
+ log_dir = File.dirname(filename)
261
+ FileUtils.mkdir_p(log_dir) unless Dir.exist?(log_dir)
262
+
263
+ File.open(filename, "w") do |f|
264
+ f.write(JSON.pretty_generate(to_hash))
265
+ end
266
+ self
267
+ end
268
+
269
+ def save_as(filename)
270
+ File.open(filename, "w") do |f|
271
+ f.write(JSON.pretty_generate(to_hash))
272
+ end
273
+ self
274
+ end
275
+
276
+ def log_to_file(filename)
277
+ File.delete(filename) if File.exist?(filename)
278
+ File.open(filename, "w") { |f|
279
+ f.puts output
280
+ f.puts error
281
+ }
282
+ self
283
+ end
284
+
285
+ def to_hash
286
+ hash = {}
287
+ hash[:command] = @command
288
+ hash[:directory] = @directory
289
+ hash[:timeout] = @timeout
290
+ hash[:start_time] = @start_time
291
+ hash[:elapsed] = @elapsed
292
+ hash[:output] = @output.force_encoding("ISO-8859-1").encode("UTF-8")
293
+ hash[:error] = @error.force_encoding("ISO-8859-1").encode("UTF-8")
294
+ hash[:exitstatus] = @exitstatus
295
+ hash[:user] = @user
296
+ hash[:machine] = @machine
297
+ hash[:context] = "Raykit.Command"
298
+ hash
299
+ end
300
+
301
+ def from_hash(hash)
302
+ @command = hash["command"]
303
+ @directory = hash["directory"]
304
+ @timeout = hash["timeout"]
305
+ @start_time = hash["start_time"]
306
+ @elapsed = hash["elapsed"]
307
+ @output = hash["output"]
308
+ @error = hash["error"]
309
+ @exitstatus = hash["exitstatus"]
310
+ @user = hash["user"] if hash.include?("user")
311
+ @machine = hash["machine"] if hash.include?("machine")
312
+ end
313
+
314
+ def self.parse(json)
315
+ cmd = Command.new("")
316
+ cmd.from_hash(JSON.parse(json))
317
+ cmd
318
+ end
319
+
320
+ def self.parse_yaml_commands(yaml)
321
+ # commands=Array.new()
322
+ data = YAML.safe_load(yaml)
323
+ commands = get_script_commands(data)
324
+ end
325
+
326
+ def self.get_script_commands(hash)
327
+ commands = []
328
+ if hash.key?("script")
329
+ hash["script"].each do |cmd|
330
+ commands << cmd
331
+ end
332
+ end
333
+ hash.each do |_k, v|
334
+ next unless v.is_a?(Hash)
335
+
336
+ subcommands = get_script_commands(v)
337
+ subcommands.each do |c|
338
+ commands << c
339
+ end
340
+ end
341
+ commands
342
+ end # get_script_commands
343
+
344
+ def to_s
345
+ s = ""
346
+ checkmark = "\u2713"
347
+ # warning="\u26A0"
348
+ error = "\u0058"
349
+ symbol = Rainbow(checkmark.encode("utf-8")).green
350
+ symbol = Rainbow(error.encode("utf-8")).red if @exitstatus != 0
351
+ cmd = "#{Rainbow(SECRETS.hide(@command)).yellow}"
352
+ #if !@exitstatus.zero?
353
+ s += "#{symbol} #{cmd} " + Rainbow("#{elapsed_str}").cyan
354
+ s += Rainbow(" #{@directory}").white + " "
355
+ #else
356
+ # s += "#{symbol} #{Rainbow(SECRETS.hide(@command)).yellow} " + Rainbow("#{elapsed_str}").cyan
357
+ #end
358
+
359
+ if !@exitstatus.zero?
360
+ if @output.length > 0
361
+ @output.lines.each do |line|
362
+ s += " " + line
363
+ end
364
+ end
365
+ if @error.length > 0
366
+ @error.lines.each do |line|
367
+ s += " " + line
368
+ end
369
+ end
370
+ end
371
+ s
372
+ end
373
+ end # class Command
374
+ end # module Raykit