Class: DeltaTest::CLI::CommandBase

Inherits:
Object
  • Object
show all
Defined in:
lib/delta_test/cli/command_base.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  'verbose' => false,
  'force'   => false,
  'no-sync' => false,
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ CommandBase

Returns a new instance of CommandBase.



21
22
23
24
25
26
# File 'lib/delta_test/cli/command_base.rb', line 21

def initialize(args)
  @args    = args.dup
  @options = parse_options!(@args)

  DeltaTest.verbose = !!@options['verbose']
end

Instance Method Details

#bundler_enabled?Boolean

Check bundler existance

Returns:

  • (Boolean)


110
111
112
# File 'lib/delta_test/cli/command_base.rb', line 110

def bundler_enabled?
  Object.const_defined?(:Bundler) || !!Utils.find_file_upward('Gemfile')
end

#exec_with_data(args, ary, status = nil) ⇒ Object

Exec command with data passed as stdin



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/delta_test/cli/command_base.rb', line 89

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



72
73
74
75
76
77
78
79
80
# File 'lib/delta_test/cli/command_base.rb', line 72

def exit_with_message(status, *args)
  if status.zero?
    puts(*args)
  else
    $stderr.puts(*args)
  end

  exit status
end

#invokeObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/delta_test/cli/command_base.rb', line 32

def invoke
  begin
    invoke!
  rescue => e
    if DeltaTest.verbose?
      raise e
    else
      exit_with_message(1, '[%s] %s' % [e.class.name, e.message])
    end
  end
end

#invoke!Object



28
29
30
# File 'lib/delta_test/cli/command_base.rb', line 28

def invoke!
  raise 'Not implemented'
end

#parse_options!(args) ⇒ Hash<String, Boolean|String>

Parse option arguments

Returns:

  • (Hash<String, Boolean|String>)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/delta_test/cli/command_base.rb', line 49

def parse_options!(args)
  options = {}

  args.reject! do |arg|
    case arg
    when /^-([a-z0-9])$/i, /^--([a-z0-9][a-z0-9-]*)$/i
      options[$1] = true
    when /^--([a-z0-9][a-z0-9-]*)=(.+)$/i
      options[$1] = $2
    else
      break
    end
  end

  DEFAULT_OPTIONS.merge(options)
end