Class: GitDump::Cmd
- Inherits:
-
Object
- Object
- GitDump::Cmd
- Defined in:
- lib/git_dump/cmd.rb
Overview
Running commands using system and popen
Defined Under Namespace
Classes: Failure
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Command arguments.
-
#chdir ⇒ Object
readonly
Working dir for running command.
-
#env ⇒ Object
readonly
Environment variables to set for command.
-
#no_stderr ⇒ Object
readonly
Redirect stderr to /dev/null.
-
#no_stdin ⇒ Object
readonly
Pipe /dev/null to stdin.
-
#no_stdout ⇒ Object
readonly
Redirect stdout to /dev/null.
Class Method Summary collapse
-
.git(*args) ⇒ Object
Run git command.
Instance Method Summary collapse
-
#capture ⇒ Object
Capture output.
-
#initialize(*args) ⇒ Cmd
constructor
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.
- #inspect ⇒ Object
-
#pipe(input) ⇒ Object
Write input to pipe and return output.
-
#popen(mode = 'r', &block) ⇒ Object
Run using popen.
-
#run ⇒ Object
Run using system.
-
#stripped_lines ⇒ Object
Captured lines.
-
#to_s ⇒ Object
Construct command string.
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 = args.pop if args.last.is_a?(Hash) @args = args.map(&:to_s) () if end |
Instance Attribute Details
#args ⇒ Object (readonly)
Command arguments
18 19 20 |
# File 'lib/git_dump/cmd.rb', line 18 def args @args end |
#chdir ⇒ Object (readonly)
Working dir for running command
24 25 26 |
# File 'lib/git_dump/cmd.rb', line 24 def chdir @chdir end |
#env ⇒ Object (readonly)
Environment variables to set for command
21 22 23 |
# File 'lib/git_dump/cmd.rb', line 21 def env @env end |
#no_stderr ⇒ Object (readonly)
Redirect stderr to /dev/null
33 34 35 |
# File 'lib/git_dump/cmd.rb', line 33 def no_stderr @no_stderr end |
#no_stdin ⇒ Object (readonly)
Pipe /dev/null to stdin
27 28 29 |
# File 'lib/git_dump/cmd.rb', line 27 def no_stdin @no_stdin end |
#no_stdout ⇒ Object (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
#capture ⇒ Object
Capture output
95 96 97 |
# File 'lib/git_dump/cmd.rb', line 95 def capture popen(&:read) end |
#inspect ⇒ Object
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 |
#run ⇒ Object
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_lines ⇒ Object
Captured lines
100 101 102 |
# File 'lib/git_dump/cmd.rb', line 100 def stripped_lines capture.split(/[\n\r]+/) end |
#to_s ⇒ Object
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 |