Class: GitDump::Cmd

Inherits:
Object
  • Object
show all
Defined in:
lib/git_dump/cmd.rb

Overview

Running commands using system and popen

Defined Under Namespace

Classes: Failure

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Cmd

Construct command, last argument can be a hash of options with keys:

:env - hash of environment varibles
:chdir - working dir for running command
:no_stdin - pipe /dev/null to stdin
:no_stdout - redirect stdout to /dev/null
:no_stderr - redirect stderr to /dev/null


39
40
41
42
43
44
45
# File 'lib/git_dump/cmd.rb', line 39

def initialize(*args)
  args = args.dup
  options = args.pop if args.last.is_a?(Hash)
  @args = args.map(&:to_s)

  parse_options(options) if options
end

Instance Attribute Details

#argsObject (readonly)

Command arguments



16
17
18
# File 'lib/git_dump/cmd.rb', line 16

def args
  @args
end

#chdirObject (readonly)

Working dir for running command



22
23
24
# File 'lib/git_dump/cmd.rb', line 22

def chdir
  @chdir
end

#envObject (readonly)

Environment variables to set for command



19
20
21
# File 'lib/git_dump/cmd.rb', line 19

def env
  @env
end

#no_stderrObject (readonly)

Redirect stderr to /dev/null



31
32
33
# File 'lib/git_dump/cmd.rb', line 31

def no_stderr
  @no_stderr
end

#no_stdinObject (readonly)

Pipe /dev/null to stdin



25
26
27
# File 'lib/git_dump/cmd.rb', line 25

def no_stdin
  @no_stdin
end

#no_stdoutObject (readonly)

Redirect stdout to /dev/null



28
29
30
# File 'lib/git_dump/cmd.rb', line 28

def no_stdout
  @no_stdout
end

Class Method Details

.git(*args) ⇒ Object

Run git command



11
12
13
# File 'lib/git_dump/cmd.rb', line 11

def self.git(*args)
  new(:git, *args)
end

Instance Method Details

#captureObject

Capture output



80
81
82
# File 'lib/git_dump/cmd.rb', line 80

def capture
  popen(&:read)
end

#inspectObject



89
90
91
# File 'lib/git_dump/cmd.rb', line 89

def inspect
  "#<#{self.class} #{self}>"
end

#popen(mode = 'r', &block) ⇒ Object

Run using popen



69
70
71
72
73
74
75
76
77
# File 'lib/git_dump/cmd.rb', line 69

def popen(mode = 'r', &block)
  if block
    result = IO.popen(arguments, mode, &block)
    return result if $CHILD_STATUS.success?
    fail Failure, "`#{self}` failed with #{$CHILD_STATUS.exitstatus}"
  else
    IO.popen(arguments, mode)
  end
end

#runObject

Run using system



62
63
64
65
66
# File 'lib/git_dump/cmd.rb', line 62

def run
  success = system(*arguments)
  return true if success
  fail Failure, "`#{self}` failed with #{$CHILD_STATUS.exitstatus}"
end

#stripped_linesObject

Captured lines



85
86
87
# File 'lib/git_dump/cmd.rb', line 85

def stripped_lines
  capture.split(/[\n\r]+/)
end

#to_sObject

Construct command string



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/git_dump/cmd.rb', line 48

def to_s
  cmd = args.shelljoin
  if env
    env_str = env.map{ |k, v| "#{k}=#{v}" }.shelljoin
    cmd = "export #{env_str}; #{cmd}"
  end
  cmd = "cd #{chdir}; #{cmd}" if chdir
  cmd << ' < /dev/null' if no_stdin
  cmd << ' > /dev/null' if no_stdout
  cmd << ' 2> /dev/null' if no_stderr
  cmd
end