Class: IO
- Inherits:
-
Object
- Object
- IO
- Defined in:
- lib/branch_io_cli/core_ext/io.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#sh(*args) ⇒ Object
Report the command.
Class Method Details
.command_from_args(*args) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/branch_io_cli/core_ext/io.rb', line 51 def IO.command_from_args(*args) raise ArgumentError, "sh requires at least one argument" unless args.count > 0 # Ignore any trailing options in the output args.pop if args.last.kind_of?(Hash) command = "" # Optional initial environment Hash if args.first.kind_of?(Hash) command = args.shift.map { |k, v| "#{k}=#{v.shellescape}" }.join(" ") + " " end # Support [ "/usr/local/bin/foo", "foo" ], "-x", ... if args.first.kind_of?(Array) command += args.shift.first.shellescape + " " + args.shelljoin command.chomp! " " elsif args.count == 1 && args.first.kind_of?(String) command += args.first else command += args.shelljoin end command end |
Instance Method Details
#sh(*args) ⇒ Object
Report the command. Execute the command, capture stdout and stderr and report line by line. Report the exit status at the end in case of error. Returns a Process::Status object.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/branch_io_cli/core_ext/io.rb', line 11 def sh(*args) write "$ #{IO.command_from_args(*args)}\n\n" Open3.popen2e(*args) do |stdin, output, thread| # output is stdout and stderr merged while (line = output.gets) puts line end status = thread.value if status == 0 write "Success.\n\n" else write "#{status}\n\n" end return status end end |