Module: Tabry::CLI::Util
- Defined in:
- lib/tabry/cli/util.rb,
lib/tabry/cli/util/config.rb
Defined Under Namespace
Classes: Config
Constant Summary collapse
- SHELL_FOR_WINDOWS =
["bash", "-c"]
Class Method Summary collapse
-
.backtick_or_die(*cmdline, **opts) ⇒ Object
TODO: would be nice to get separate STDERR and STDOUT.
- .backtick_with_status(*cmdline, valid_statuses: nil, extra_error_message: nil, **opts) ⇒ Object
- .make_cmdline(cmdline, *args, echo: false, echo_only: false, merge_stderr: false) ⇒ Object
- .open_command ⇒ Object
- .open_web_page(url_or_urls) ⇒ Object
- .system(*cmdline, **opts) ⇒ Object
Class Method Details
.backtick_or_die(*cmdline, **opts) ⇒ Object
TODO: would be nice to get separate STDERR and STDOUT
48 49 50 |
# File 'lib/tabry/cli/util.rb', line 48 def backtick_or_die(*cmdline, **opts) backtick_with_status(*cmdline, valid_statuses: [0], **opts).first end |
.backtick_with_status(*cmdline, valid_statuses: nil, extra_error_message: nil, **opts) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/tabry/cli/util.rb', line 52 def backtick_with_status(*cmdline, valid_statuses: nil, extra_error_message: nil, **opts) cmdline = make_cmdline(*cmdline, **opts) return [nil, nil] unless cmdline require 'rubygems' enoent_error = false begin res = if Gem.win_platform? IO.popen([*SHELL_FOR_WINDOWS, cmdline]) { |io| io.read } else `#{cmdline}` end rescue Errno::ENOENT enoent_error = true end status = $? if valid_statuses && !valid_statuses.include?(status.exitstatus) msg = "COMMAND FAILED with exit code #{status.exitstatus}" msg += " (command does not exist)" if enoent_error Kernel.warn "#{msg}: #{cmdline}" Kernel.warn "Command output:\n#{res}" Kernel.warn if Kernel.exit 1 end [res, status] end |
.make_cmdline(cmdline, *args, echo: false, echo_only: false, merge_stderr: false) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/tabry/cli/util.rb', line 13 def make_cmdline(cmdline, *args, echo: false, echo_only: false, merge_stderr: false) # Allow to pass in an array, or varargs: args = args.first if args.length == 1 && args.first.is_a?(Array) args = args.map do |arg| if arg.is_a?(Array) # array of arguments get escaped separately and joined with strings (see specs) arg.map { |a| Shellwords.escape(a) }.join(" ") else Shellwords.escape(arg) end end cmdline %= args cmdline = "{ #{cmdline} ;} 2>&1" if merge_stderr warn cmdline if echo || echo_only return nil if echo_only cmdline end |
.open_command ⇒ Object
81 82 83 |
# File 'lib/tabry/cli/util.rb', line 81 def open_command RUBY_PLATFORM.include?("linux") ? "xdg-open" : "open" end |
.open_web_page(url_or_urls) ⇒ Object
85 86 87 88 89 |
# File 'lib/tabry/cli/util.rb', line 85 def open_web_page(url_or_urls) unless system("nohup %s %s 2>&1 >/dev/null &", open_command, url_or_urls) warn "WARNING: opening web page failed: #{make_cmdline("%s %s", open_command, url)}" end end |
.system(*cmdline, **opts) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/tabry/cli/util.rb', line 34 def system(*cmdline, **opts) cmdline = make_cmdline(*cmdline, **opts) return unless cmdline require 'rubygems' if Gem.win_platform? Kernel.system *SHELL_FOR_WINDOWS, cmdline else Kernel.system cmdline end end |