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
-
#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
39 40 41 42 43 44 45 |
# File 'lib/git_dump/cmd.rb', line 39 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
16 17 18 |
# File 'lib/git_dump/cmd.rb', line 16 def args @args end |
#chdir ⇒ Object (readonly)
Working dir for running command
22 23 24 |
# File 'lib/git_dump/cmd.rb', line 22 def chdir @chdir end |
#env ⇒ Object (readonly)
Environment variables to set for command
19 20 21 |
# File 'lib/git_dump/cmd.rb', line 19 def env @env end |
#no_stderr ⇒ Object (readonly)
Redirect stderr to /dev/null
31 32 33 |
# File 'lib/git_dump/cmd.rb', line 31 def no_stderr @no_stderr end |
#no_stdin ⇒ Object (readonly)
Pipe /dev/null to stdin
25 26 27 |
# File 'lib/git_dump/cmd.rb', line 25 def no_stdin @no_stdin end |
#no_stdout ⇒ Object (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
#capture ⇒ Object
Capture output
80 81 82 |
# File 'lib/git_dump/cmd.rb', line 80 def capture popen(&:read) end |
#inspect ⇒ Object
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 |
#run ⇒ Object
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_lines ⇒ Object
Captured lines
85 86 87 |
# File 'lib/git_dump/cmd.rb', line 85 def stripped_lines capture.split(/[\n\r]+/) end |
#to_s ⇒ Object
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 |