Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Dec 13, 2018
1 parent 6a7d4bb commit d1d36ef
Showing 1 changed file with 102 additions and 61 deletions.
163 changes: 102 additions & 61 deletions library/system/src/lib/yast2/execute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d1d36ef

Please sign in to comment.