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


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

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



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

def args
  @args
end

#chdirObject (readonly)

Working dir for running command



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

def chdir
  @chdir
end

#envObject (readonly)

Environment variables to set for command



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

def env
  @env
end

#no_stderrObject (readonly)

Redirect stderr to /dev/null



33
34
35
# File 'lib/git_dump/cmd.rb', line 33

def no_stderr
  @no_stderr
end

#no_stdinObject (readonly)

Pipe /dev/null to stdin



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

def no_stdin
  @no_stdin
end

#no_stdoutObject (readonly)

Redirect stdout to /dev/null



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

def no_stdout
  @no_stdout
end

Class Method Details

.git(*args) ⇒ Object

Run git command



13
14
15
# File 'lib/git_dump/cmd.rb', line 13

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

Instance Method Details

#captureObject

Capture output



95
96
97
# File 'lib/git_dump/cmd.rb', line 95

def capture
  popen(&:read)
end

#inspectObject



104
105
106
# File 'lib/git_dump/cmd.rb', line 104

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

#pipe(input) ⇒ Object

Write input to pipe and return output



84
85
86
87
88
89
90
91
92
# File 'lib/git_dump/cmd.rb', line 84

def pipe(input)
  popen(input ? 'r+' : 'r') do |f|
    if input
      f.write input
      f.close_write
    end
    f.read
  end
end

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

Run using popen



72
73
74
75
76
77
78
79
80
81
# File 'lib/git_dump/cmd.rb', line 72

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



64
65
66
67
68
69
# File 'lib/git_dump/cmd.rb', line 64

def run
  success = system(*arguments)
  return true if success

  fail Failure, "`#{self}` failed with #{$CHILD_STATUS.exitstatus}"
end

#stripped_linesObject

Captured lines



100
101
102
# File 'lib/git_dump/cmd.rb', line 100

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

#to_sObject

Construct command string



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

def to_s
  parts = [args.shelljoin]
  if env
    env_str = env.map{ |k, v| "#{k}=#{v}" }.shelljoin
    parts.unshift "export #{env_str};"
  end
  parts.unshift "cd #{chdir};" if chdir
  parts.push '< /dev/null' if no_stdin
  parts.push '> /dev/null' if no_stdout
  parts.push '2> /dev/null' if no_stderr
  parts.join(' ')
end