Class: MGit::System::SystemCommand

Inherits:
Object
  • Object
show all
Includes:
Output
Defined in:
lib/mgit/system.rb

Direct Known Subclasses

GitCommand

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Output

#perror, #pinfo, #psystem, #ptable, #pwarn

Constructor Details

#initialize(cmd, opts) ⇒ SystemCommand

Returns a new instance of SystemCommand.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mgit/system.rb', line 10

def initialize(cmd, opts)
  opts, popen_opts = extract_options(opts)
  @stdout, @stderr, @st = Open3.capture3(cmd, popen_opts)

  psystem(stdout.strip) if opts[:print_stdout]

  return if success?

  psystem(stderr.strip) if opts[:print_stderr]
  fail SystemCommandError.new(cmd, opts[:error]) if opts[:raise]
end

Instance Attribute Details

#stderrObject (readonly)

Returns the value of attribute stderr.



8
9
10
# File 'lib/mgit/system.rb', line 8

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



8
9
10
# File 'lib/mgit/system.rb', line 8

def stdout
  @stdout
end

Instance Method Details

#=~(other) ⇒ Object



26
27
28
# File 'lib/mgit/system.rb', line 26

def =~(other)
  (@stdout =~ other) || (@stderr =~ other)
end

#default_optionsObject



30
31
32
33
34
35
36
37
# File 'lib/mgit/system.rb', line 30

def default_options
  {
    print_stdout: false,
    print_stderr: false,
    raise: false,
    error: 'Command failed.'
  }
end

#extract_options(opts) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mgit/system.rb', line 39

def extract_options(opts)
  popen_opts = opts.dup

  opts = Hash[
    default_options.map do |k, v|
      [k, popen_opts.key?(k) ? popen_opts.delete(k) : v]
    end
  ]

  [opts, popen_opts]
end

#success?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/mgit/system.rb', line 22

def success?
  @st.exitstatus == 0
end