Module: UPM::Tool::DSL
- Included in:
- UPM::Tool
- Defined in:
- lib/upm/tool_dsl.rb
Instance Method Summary collapse
- #call_command(name, *args) ⇒ Object
- #command(name, shell_command = nil, root: false, paged: false, highlight: nil, &block) ⇒ Object
- #curl(url) ⇒ Object
- #help ⇒ Object
- #identifying_binary(id_bin = nil) ⇒ Object
- #os(*names) ⇒ Object
- #prefix(name) ⇒ Object
- #print_files(*paths, include: nil, exclude: nil) ⇒ Object
-
#run(*args, root: false, paged: false, grep: nil, highlight: nil) ⇒ Object
Helpers.
Instance Method Details
#call_command(name, *args) ⇒ Object
134 135 136 137 138 139 140 |
# File 'lib/upm/tool_dsl.rb', line 134 def call_command(name, *args) if block = (@cmds[name] || @cmds[ALIASES[name]]) block.call args else puts "Command #{name.inspect} not supported by #{@name.inspect}" end end |
#command(name, shell_command = nil, root: false, paged: false, highlight: nil, &block) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/upm/tool_dsl.rb', line 16 def command(name, shell_command=nil, root: false, paged: false, highlight: nil, &block) @cmds ||= {} if block_given? if root and Process.uid != 0 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(" ") : nil run(*shell_command, *args, paged: paged, root: root, highlight: query) end else raise "Error: Must supply a block or shell command" end end |
#curl(url) ⇒ Object
112 113 114 115 116 117 |
# File 'lib/upm/tool_dsl.rb', line 112 def curl(url) IO.popen(["curl", "-Ss", url], &:read) rescue Errno::ENOENT puts "Error: 'curl' isn't installed. You need this!" exit 1 end |
#help ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/upm/tool_dsl.rb', line 142 def help if osname = Tool.nice_os_name puts " Detected OS: #{osname}" end puts "Package manager: #{@name}" puts puts "Available commands:" available = COMMAND_HELP.select do |name, desc| names = name.split("/") names.any? { |name| @cmds[name] } end max_width = available.map(&:first).map(&:size).max available.each do |name, desc| puts " #{name.rjust(max_width)} | #{desc}" end end |
#identifying_binary(id_bin = nil) ⇒ Object
4 5 6 7 8 9 10 |
# File 'lib/upm/tool_dsl.rb', line 4 def (id_bin=nil) if id_bin @id_bin = id_bin else @id_bin || @name end end |
#os(*names) ⇒ Object
47 48 49 |
# File 'lib/upm/tool_dsl.rb', line 47 def os(*names) names.any? ? @os = names : @os end |
#prefix(name) ⇒ Object
12 13 14 |
# File 'lib/upm/tool_dsl.rb', line 12 def prefix(name) @prefix = name end |
#print_files(*paths, include: nil, exclude: nil) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/upm/tool_dsl.rb', line 119 def print_files(*paths, include: nil, exclude: nil) lesspipe do |less| paths.each do |path| less.puts "<8>=== <11>#{path} <8>========".colorize open(path) do |io| enum = io.each_line enum = enum.grep(include) if include enum = enum.reject { |line| line[exclude] } if exclude enum.each { |line| less.puts line } end less.puts end end end |
#run(*args, root: false, paged: false, grep: nil, highlight: nil) ⇒ Object
Helpers
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/upm/tool_dsl.rb', line 53 def run(*args, root: false, paged: false, grep: nil, highlight: nil) if root if Process.uid != 0 if File.which("sudo") args.unshift "sudo" elsif File.which("su") args = ["su", "-c"] + args else raise "Error: You must be root to run this command. (And I couldn't find the 'sudo' *or* 'su' commands.)" end end end if !paged and !grep system(*args) else IO.popen(args, err: [:child, :out]) do |command_io| # if grep # pattern = grep.is_a?(Regexp) ? grep.source : grep.to_s # grep_io = IO.popen(["grep", "--color=always", "-Ei", pattern], "w+") # IO.copy_stream(command_io, grep_io) # grep_io.close_write # command_io = grep_io # end # if paged # lesspipe do |less| # IO.copy_stream(command_io, less) # end # else # IO.copy_stream(command_io, STDOUT) # end highlight_proc = if highlight proc { |line| line.gsub(highlight) { |m| "\e[33;1m#{m}\e[0m" } } else proc { |line| line } end grep_proc = if grep proc { |line| line[grep] } else proc { true } end lesspipe(disabled: !paged) do |less| command_io.each_line do |line| less.puts highlight_proc.(line) if grep_proc.(line) end end end $?.to_i == 0 end end |