Module: Bixby::Script::Util

Included in:
Bixby::ScriptUtil
Defined in:
lib/bixby-client/script/util.rb

Instance Method Summary collapse

Instance Method Details

#get_json_inputObject

Reads JSON data from STDIN

Returns:

  • (Object)

    data found on STDIN (can be Hash, Array, String, etc)



11
12
13
14
15
# 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) ⇒ Mixlib::ShellOut

Like #sudo, except log command and results

Parameters:

  • args (Array)

Returns:



78
79
80
81
82
83
84
85
86
87
88
# 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) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# 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_stdinString

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

Returns:

  • (String)

    data



21
22
23
24
25
26
27
28
29
30
31
# 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) ⇒ Mixlib::ShellOut

Simple wrapper around #systemu which prepends sudo to the command

Parameters:

  • args (Array)

Returns:



61
62
63
64
65
66
67
68
69
70
71
# 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) ⇒ Mixlib::ShellOut

Simple wrapper around Mixlib::ShellOut

Parameters:

  • args (Array)

Returns:



38
39
40
41
42
# File 'lib/bixby-client/script/util.rb', line 38

def systemu(*args)
  cmd = Mixlib::ShellOut.new(*args)
  cmd.run_command
  cmd
end