Method: UPM::Tool::DSL#command

Defined in:
lib/upm/tool_dsl.rb

#command(name, shell_command = nil, root: false, paged: false, highlight: nil, &block) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/upm/tool_dsl.rb', line 41

def command(name, shell_command=nil, root: false, paged: false, highlight: nil, &block)
  @cmds ||= {}

  if block_given?

    if root and Process.uid != 0
      @cmds[name] = proc { exec("sudo", $PROGRAM_NAME, *ARGV) }
    else
      @cmds[name] = block
    end

  elsif shell_command

    if shell_command.is_a? String
      shell_command = shell_command.split
    elsif not shell_command.is_a? Array
      raise "Error: command argument must be a String or an Array; it was a #{cmd.class}"
    end

    @cmds[name] = proc do |args|
      query = highlight ? args.join("\\s+") : nil
      run(*shell_command, *args, paged: paged, root: root, highlight: query)
    end

  else
    raise "Error: Must supply a block or shell command"
  end
end