From d1d36ef33faa492b7fc1ab44858b4db1352472ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Iv=C3=A1n=20L=C3=B3pez=20Gonz=C3=A1lez?= Date: Thu, 13 Dec 2018 14:23:08 +0000 Subject: [PATCH] WIP --- library/system/src/lib/yast2/execute.rb | 163 +++++++++++++++--------- 1 file changed, 102 insertions(+), 61 deletions(-) diff --git a/library/system/src/lib/yast2/execute.rb b/library/system/src/lib/yast2/execute.rb index 44d10c7d8..1b23fdb24 100644 --- a/library/system/src/lib/yast2/execute.rb +++ b/library/system/src/lib/yast2/execute.rb @@ -37,76 +37,117 @@ class Execute extend Yast::I18n textdomain "base" - # Runs with chroot; a failure becomes a popup. - # Runs a command described by *args*, - # in a `chroot(2)` specified by the installation (WFM.scr_root). - # Shows a {ReportClass#Error popup} if the command fails - # and returns `nil` in such case. - # It also globally switches the default Cheetah logger to - # {http://www.rubydoc.info/github/yast/yast-ruby-bindings/Yast%2FLogger Y2Logger}. - # @param args see http://www.rubydoc.info/github/openSUSE/cheetah/Cheetah.run - def self.on_target(*args) - popup_error { on_target!(*args) } - end + class << self + # Runs with chroot; a failure becomes a popup. + # Runs a command described by *args*, + # in a `chroot(2)` specified by the installation (WFM.scr_root). + # Shows a {ReportClass#Error popup} if the command fails + # and returns `nil` in such case. + # It also globally switches the default Cheetah logger to + # {http://www.rubydoc.info/github/yast/yast-ruby-bindings/Yast%2FLogger Y2Logger}. + # @param args see http://www.rubydoc.info/github/openSUSE/cheetah/Cheetah.run + def on_target(*args) + popup_error { on_target!(*args) } + end + + # Runs with chroot; a failure becomes an exception. + # Runs a command described by *args*, + # in a `chroot(2)` specified by the installation (WFM.scr_root). + # It also globally switches the default Cheetah logger to + # {http://www.rubydoc.info/github/yast/yast-ruby-bindings/Yast%2FLogger Y2Logger}. + # @param args see http://www.rubydoc.info/github/openSUSE/cheetah/Cheetah.run + # @raise Cheetah::ExecutionFailed if the command fails + def on_target!(*args) + root = Yast::WFM.scr_root + + object = Execute.new(args, chroot: root) + + args.none? ? object : object.run + end - # Runs with chroot; a failure becomes an exception. - # Runs a command described by *args*, - # in a `chroot(2)` specified by the installation (WFM.scr_root). - # It also globally switches the default Cheetah logger to - # {http://www.rubydoc.info/github/yast/yast-ruby-bindings/Yast%2FLogger Y2Logger}. - # @param args see http://www.rubydoc.info/github/openSUSE/cheetah/Cheetah.run - # @raise Cheetah::ExecutionFailed if the command fails - def self.on_target!(*args) - root = Yast::WFM.scr_root - - if args.last.is_a? ::Hash - args.last[:chroot] = root - else - args.push(chroot: root) + # Runs without chroot; a failure becomes a popup. + # Runs a command described by *args*, + # *disregarding* a `chroot(2)` specified by the installation (WFM.scr_root). + # Shows a {ReportClass#Error popup} if the command fails + # and returns `nil` in such case. + # It also globally switches the default Cheetah logger to + # {http://www.rubydoc.info/github/yast/yast-ruby-bindings/Yast%2FLogger Y2Logger}. + # @param args see http://www.rubydoc.info/github/openSUSE/cheetah/Cheetah.run + def locally(*args) + popup_error { locally!(*args) } end - Cheetah.run(*args) + # Runs without chroot; a failure becomes an exception. + # Runs a command described by *args*, + # *disregarding* a `chroot(2)` specified by the installation (WFM.scr_root). + # It also globally switches the default Cheetah logger to + # {http://www.rubydoc.info/github/yast/yast-ruby-bindings/Yast%2FLogger Y2Logger}. + # @param args see http://www.rubydoc.info/github/openSUSE/cheetah/Cheetah.run + # @raise Cheetah::ExecutionFailed if the command fails + def locally!(*args) + object = Execute.new(args) + + args.none? ? object : object.run + end + + def stdout(*args) + Execute.new.stdout(*args) + end + + private + + def popup_error(&block) + block.call + rescue Cheetah::ExecutionFailed => e + Yast.import "Report" + + Yast::Report.Error( + _( + "Execution of command \"%{command}\" failed.\n"\ + "Exit code: %{exitcode}\n"\ + "Error output: %{stderr}" + ) % { + command: e.commands.inspect, + exitcode: e.status.exitstatus, + stderr: e.stderr + } + ) + end end - # Runs without chroot; a failure becomes a popup. - # Runs a command described by *args*, - # *disregarding* a `chroot(2)` specified by the installation (WFM.scr_root). - # Shows a {ReportClass#Error popup} if the command fails - # and returns `nil` in such case. - # It also globally switches the default Cheetah logger to - # {http://www.rubydoc.info/github/yast/yast-ruby-bindings/Yast%2FLogger Y2Logger}. - # @param args see http://www.rubydoc.info/github/openSUSE/cheetah/Cheetah.run - def self.locally(*args) - popup_error { locally!(*args) } + def initialize(args = [], options = {}) + @args = args + @options = options end - # Runs without chroot; a failure becomes an exception. - # Runs a command described by *args*, - # *disregarding* a `chroot(2)` specified by the installation (WFM.scr_root). - # It also globally switches the default Cheetah logger to - # {http://www.rubydoc.info/github/yast/yast-ruby-bindings/Yast%2FLogger Y2Logger}. - # @param args see http://www.rubydoc.info/github/openSUSE/cheetah/Cheetah.run - # @raise Cheetah::ExecutionFailed if the command fails - def self.locally!(*args) - Cheetah.run(*args) + def stdout(*args) + self.args = args + + add_options(stdout: :capture) + + run + rescue Cheetah::ExecutionFailed + "" end - private_class_method def self.popup_error(&block) - block.call - rescue Cheetah::ExecutionFailed => e - Yast.import "Report" - - Yast::Report.Error( - _( - "Execution of command \"%{command}\" failed.\n"\ - "Exit code: %{exitcode}\n"\ - "Error output: %{stderr}" - ) % { - command: e.commands.inspect, - exitcode: e.status.exitstatus, - stderr: e.stderr - } - ) + def run + new_args = args + new_args << {} unless args.last.is_a?(Hash) + + new_options = new_args.last.merge(options) + new_args[-1] = new_options + + Cheetah.run(*new_args) + end + + private + + attr_accessor :args + + attr_reader :options + + def add_options(options) + @options.merge!(options) end end end