require_relative 'logger' module Remon module Helper include Logger def cmd(command, error_msg: nil, return_output: true, env: {}, shell: false) if command.is_a? Array command_arr = command command_str = command.join(" ") else command_arr = command.split command_str = command end logger.debug command_str run_command = shell ? command_str : command_arr output = if return_output IO.popen(env, run_command) { |f| f.read } else system(env, run_command, 2 => 1) end exitstatus = $?.exitstatus if exitstatus != 0 error_msg ||= "non zero exit for \"#{command_str}\"" raise Error, error_msg end return output end def safe_cmd(*args, **kwargs) output = cmd(*args, **kwargs) return $?.exitstatus, output rescue => e logger.debug e.message return -1, nil end end end