Class: DeltaTest::CLI::CommandBase
- Inherits:
-
Object
- Object
- DeltaTest::CLI::CommandBase
- Defined in:
- lib/delta_test/cli/command_base.rb
Direct Known Subclasses
ExecCommand, HelpCommand, SpecsCommand, StatsCleanCommand, StatsSaveCommand, StatsShowCommand, VersionCommand
Constant Summary collapse
- DEFAULT_OPTIONS =
{ 'verbose' => false, 'force' => false, 'no-sync' => false, }.freeze
Instance Method Summary collapse
-
#bundler_enabled? ⇒ Boolean
Check bundler existance.
-
#create_error_file ⇒ Object
Create an error file.
-
#current_process_status_success? ⇒ Boolean
Check exit status of the current process.
-
#error_file ⇒ Pathname
Path for an error file.
-
#error_recorded? ⇒ Boolean
Check if any error is recorded.
-
#exec_with_data(args, ary, status = nil) ⇒ Object
Exec command with data passed as stdin.
-
#exit_with_message(status, *args) ⇒ Object
Print message and exit with a status.
-
#hook_create_error_file ⇒ Object
Hook on exit and record errors.
-
#initialize(args) ⇒ CommandBase
constructor
A new instance of CommandBase.
- #invoke ⇒ Object
- #invoke! ⇒ Object
-
#parse_options!(args) ⇒ Hash<String, Boolean|String>
Parse option arguments.
-
#record_error ⇒ Object
Wrapper of hook_create_error_file.
Constructor Details
#initialize(args) ⇒ CommandBase
Returns a new instance of CommandBase.
22 23 24 25 26 27 |
# File 'lib/delta_test/cli/command_base.rb', line 22 def initialize(args) @args = args.dup @options = (@args) DeltaTest.verbose = !!@options['verbose'] end |
Instance Method Details
#bundler_enabled? ⇒ Boolean
Check bundler existance
111 112 113 |
# File 'lib/delta_test/cli/command_base.rb', line 111 def bundler_enabled? Object.const_defined?(:Bundler) || !!Utils.find_file_upward('Gemfile') end |
#create_error_file ⇒ Object
Create an error file
164 165 166 167 |
# File 'lib/delta_test/cli/command_base.rb', line 164 def create_error_file FileUtils.mkdir_p(File.dirname(error_file)) File.new(error_file, 'w') end |
#current_process_status_success? ⇒ Boolean
Check exit status of the current process
139 140 141 |
# File 'lib/delta_test/cli/command_base.rb', line 139 def current_process_status_success? $!.nil? || $!.is_a?(SystemExit) && $!.success? end |
#error_file ⇒ Pathname
Path for an error file
157 158 159 |
# File 'lib/delta_test/cli/command_base.rb', line 157 def error_file @error_file ||= DeltaTest.config.tmp_table_file.parent.join('error.txt') end |
#error_recorded? ⇒ Boolean
Check if any error is recorded
148 149 150 |
# File 'lib/delta_test/cli/command_base.rb', line 148 def error_recorded? File.exists?(error_file) end |
#exec_with_data(args, ary, status = nil) ⇒ Object
Exec command with data passed as stdin
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/delta_test/cli/command_base.rb', line 90 def exec_with_data(args, ary, status = nil) $stdout.sync = true Open3.popen3(args) do |i, o, e, w| i.write(ary.join("\n")) if ary i.close threads = [] threads << Thread.new { o.each { |l| puts l } } threads << Thread.new { e.each { |l| $stderr.puts l } } ThreadsWait.all_waits(*threads) exit(status.nil? ? w.value.exitstatus : status) end end |
#exit_with_message(status, *args) ⇒ Object
Print message and exit with a status
73 74 75 76 77 78 79 80 81 |
# File 'lib/delta_test/cli/command_base.rb', line 73 def (status, *args) if status.zero? puts(*args) else $stderr.puts(*args) end exit status end |
#hook_create_error_file ⇒ Object
Hook on exit and record errors
128 129 130 131 132 |
# File 'lib/delta_test/cli/command_base.rb', line 128 def hook_create_error_file at_exit do create_error_file unless current_process_status_success? end end |
#invoke ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/delta_test/cli/command_base.rb', line 33 def invoke begin invoke! rescue => e if DeltaTest.verbose? raise e else (1, '[%s] %s' % [e.class.name, e.]) end end end |
#invoke! ⇒ Object
29 30 31 |
# File 'lib/delta_test/cli/command_base.rb', line 29 def invoke! raise 'Not implemented' end |
#parse_options!(args) ⇒ Hash<String, Boolean|String>
Parse option arguments
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/delta_test/cli/command_base.rb', line 50 def (args) = {} args.reject! do |arg| case arg when /^-([a-z0-9])$/i, /^--([a-z0-9][a-z0-9-]*)$/i [$1] = true when /^--([a-z0-9][a-z0-9-]*)=(.+)$/i [$1] = $2 else break end end DEFAULT_OPTIONS.merge() end |
#record_error ⇒ Object
Wrapper of hook_create_error_file
120 121 122 123 |
# File 'lib/delta_test/cli/command_base.rb', line 120 def record_error hook_create_error_file yield if block_given? end |