module Bixby::Script::Util

Public Instance Methods

get_json_input() click to toggle source

Reads JSON data from STDIN

@return [Object] data found on STDIN (can be Hash, Array, String, etc)

# File lib/bixby-client/script/util.rb, line 11
def get_json_input
  input = read_stdin()
  input.strip! if input
  (input.nil? or input.empty?) ? {} : MultiJson.load(input)
end
logged_sudo(*args) click to toggle source

Like sudo, except log command and results

@param [Array] args

@return [Mixlib::ShellOut]

# File lib/bixby-client/script/util.rb, line 78
def logged_sudo(*args)
  cmd = sudo(*args)
  logger && logger.debug {
    s = cmd.command
    s += "\nSTATUS: #{cmd.exitstatus}" if !cmd.success?
    s += "\nSTDOUT:\n#{cmd.stdout}" if !cmd.stdout.strip.empty?
    s += "\nSTDERR:\n#{cmd.stderr}" if !cmd.stderr.strip.empty?
    s
  }
  cmd
end
logged_systemu(*args) click to toggle source
# File lib/bixby-client/script/util.rb, line 44
def logged_systemu(*args)
  cmd = systemu(*args)
  logger && logger.debug {
    s = cmd.command
    s += "\nSTATUS: #{cmd.exitstatus}" if !cmd.success?
    s += "\nSTDOUT:\n#{cmd.stdout}" if !cmd.stdout.strip.empty?
    s += "\nSTDERR:\n#{cmd.stderr}" if !cmd.stderr.strip.empty?
    s
  }
  cmd
end
read_stdin() click to toggle source

Read all available data on STDIN without blocking (i.e., if no data is available, none will be returned)

@return [String] data

# File lib/bixby-client/script/util.rb, line 21
def read_stdin
  buff = []
  while true do
    begin
      buff << STDIN.read_nonblock(64000)
    rescue
      break
    end
  end
  return buff.join('')
end
sudo(*args) click to toggle source

Simple wrapper around systemu which prepends sudo to the command

@param [Array] args

@return [Mixlib::ShellOut]

# File lib/bixby-client/script/util.rb, line 61
def sudo(*args)
  if args.last.kind_of? Hash then
    opts = args.last
    if opts[:env] && opts[:env]["PATH"] then
      path = opts[:env]["PATH"]
      args[0] = "env PATH=#{path} #{args.first}"
    end
  end
  args[0] = "sudo #{args.first}"
  systemu(*args)
end
systemu(*args) click to toggle source

Simple wrapper around Mixlib::ShellOut

@param [Array] args

@return [Mixlib::ShellOut]

# File lib/bixby-client/script/util.rb, line 38
def systemu(*args)
  cmd = Mixlib::ShellOut.new(*args)
  cmd.run_command
  cmd
end