286 lines
7.0 KiB
Ruby
Executable File
286 lines
7.0 KiB
Ruby
Executable File
#!/usr/bin/env ruby
|
|
# -*- encoding : utf-8 -*-
|
|
|
|
require 'irb-pager'
|
|
require 'shellwords'
|
|
|
|
class Commands < Hash
|
|
class CommandError < Exception
|
|
end
|
|
class ExpectingCommandError < CommandError
|
|
def initialize *args
|
|
args = ['Command expected'] if args.empty?
|
|
super *args
|
|
end
|
|
end
|
|
class UnknownCommandError < CommandError
|
|
def initialize *args
|
|
args = ['This Command i do not know'] if args.empty?
|
|
super *args
|
|
end
|
|
end
|
|
|
|
attr_accessor :exe, :prefix, :default_cmd
|
|
|
|
def self.arg_unshift arg, args
|
|
arg = arg.is_a?(Array) ? arg : [arg]
|
|
i = 1+args.index {|x|/^[^-]/=~x}
|
|
args[0...i] + arg + args[i..-1]
|
|
end
|
|
|
|
def self.new prefix, exe
|
|
r = super()
|
|
r.exe = exe
|
|
r.prefix = prefix
|
|
r.on {|cmd, *argv| raise UnknownCommandError, 'Unknown Command: %s' % cmd }
|
|
r
|
|
end
|
|
|
|
def on *names, &run
|
|
if names.empty?
|
|
@default_cmd = run
|
|
else
|
|
names.each {|name| self[name.to_s.to_sym] = run }
|
|
end
|
|
end
|
|
|
|
def cmd argv
|
|
if @prefix == @exe
|
|
raise ExpectingCommandError if argv.empty?
|
|
[argv[0].to_sym, *argv[1..-1]]
|
|
else
|
|
@exe =~ /^(?:#{Regexp.escape @prefix}-)?(.*)$/
|
|
[$1.to_sym, *argv]
|
|
end
|
|
end
|
|
|
|
def each all=nil, &block
|
|
if not block_given?
|
|
Enumerator.new self, :each, all
|
|
elsif all
|
|
super(&block)
|
|
else
|
|
super() {|k,v| yield k,v unless /^-/ =~ k.to_s }
|
|
end
|
|
end
|
|
|
|
def run *argv
|
|
c, *argv = self.cmd( argv)
|
|
(self[c] || @default_cmd).call c, *argv
|
|
rescue CommandError
|
|
STDERR.puts $!.message
|
|
exit 1
|
|
end
|
|
alias call run
|
|
|
|
def to_proc
|
|
method(:call).to_proc
|
|
end
|
|
end
|
|
|
|
class RunCave
|
|
class CommandExpected <Exception
|
|
end
|
|
class ResumeFileExpected <Exception
|
|
end
|
|
|
|
attr_accessor :preargs, :cmd, :args
|
|
attr_reader :dummy, :resumable
|
|
attr_writer :resume_file
|
|
def initialize preargs = nil, cmd = nil, args = nil
|
|
@resume_file, @preargs, @cmd, @args = nil, preargs || [], cmd || [], args || []
|
|
@colored = @dummy = @resumable = nil
|
|
end
|
|
|
|
def can_be_dummy
|
|
@dummy = true unless false == @dummy
|
|
self
|
|
end
|
|
def dummy?() @dummy end
|
|
def dummy!() @dummy = true; self end
|
|
def dummy=(v) @dummy = !!v; self end
|
|
|
|
def can_be_resumable
|
|
@resumable = true unless false == @resumable
|
|
self
|
|
end
|
|
def resumable?() @resumable end
|
|
def resumable!() @resumable = true; self end
|
|
def resumable=(v) @resumable = !!v; self end
|
|
|
|
def can_be_colored
|
|
@colored = true unless false == @colored
|
|
self
|
|
end
|
|
def colored?() @colored end
|
|
def colored!() @colored = true; self end
|
|
def colored=(v) @colored = !!v; self end
|
|
|
|
def prepare_resume_file
|
|
resumable? ? ['--resume-file', @resume_file || raise(ResumeFileExpected)] : []
|
|
end
|
|
|
|
def prepare_colored
|
|
colored? ? ['--colour', 'yes'] : []
|
|
end
|
|
|
|
def prepare
|
|
raise CommandExpected, "Set #{self}.cmd = yourcommand." if @cmd.nil? or @cmd.empty?
|
|
[
|
|
:cave, prepare_colored, @preargs, @cmd, prepare_resume_file, *@args
|
|
].flatten.select{|x|x}.map {|x| x.to_s }
|
|
end
|
|
|
|
def run
|
|
a = prepare
|
|
if dummy?
|
|
puts a.shelljoin
|
|
0
|
|
else
|
|
Kernel.system *a
|
|
$? ? $?.exitstatus : 130
|
|
end
|
|
end
|
|
|
|
def this cmd, *args
|
|
@cmd, @args = cmd, args
|
|
self
|
|
end
|
|
alias method_missing this
|
|
|
|
def resume_file f
|
|
if f.nil?
|
|
@resume_file
|
|
else
|
|
@resume_file = f
|
|
self
|
|
end
|
|
end
|
|
|
|
def run_exit
|
|
exit run
|
|
end
|
|
alias call run_exit
|
|
end
|
|
|
|
def pager *args, &exe
|
|
if STDOUT.tty?
|
|
IRB::Pager::pager *args, &exe
|
|
else
|
|
exe.call
|
|
end
|
|
end
|
|
|
|
def pagered exe = nil, &block
|
|
exe ||= block
|
|
lambda {|*args| pager { exe.call *args } }
|
|
end
|
|
|
|
ENV['LESS'] = "-FR #{ENV['LESS']}"
|
|
|
|
argv0 = $0
|
|
cave = RunCave.new
|
|
cmds = Commands.new 'truckle', File.basename(argv0)
|
|
cmds.on {|*args| cave.this(*args).() }
|
|
|
|
def markdown_format t, colored = nil
|
|
t = t.gsub( /^\t+/) {|indent| ' '*indent.length}
|
|
if colored
|
|
t.gsub! /^([^\n]+)\n===+/m, "\n«««««« \033[1;4;33m\\1\033[0m »»»»»»"
|
|
t.gsub! /^([^\n]+)\n---+/m, "\n««« \033[1;33m\\1\033[0m »»»"
|
|
t.gsub! /`([^`]+)`/, "\033[1;44m \\1 \033[0m"
|
|
end
|
|
t = t[1..-1] while t.start_with? "\n"
|
|
t
|
|
end
|
|
|
|
cmds.on :help, '-h', '--help', &pagered { cmd=cmds.prefix; STDOUT.puts markdown_format(<<EOF, cave.colored?) }
|
|
Usage
|
|
=====
|
|
|
|
#{cmd} Command *ARGS
|
|
|
|
Resumable
|
|
=========
|
|
|
|
You do not need to set a resume-file. #{cmd} will determine it automaticaly. First, you can give a first argument for tagging. Tag must be numerical!
|
|
|
|
#{cmd} 321 resolve netcat6
|
|
#{cmd} 321 do
|
|
|
|
If you do not give a tag, #{cmd} will use the actual terminal-device-number. If it isn't possible to determine terminal, the parent-pid will be used.
|
|
|
|
Like cave but different
|
|
=======================
|
|
|
|
Some commands will be displayed by a pager, so you can scroll up and down like
|
|
|
|
truckle resolve WHAT # cave -cy resolve WHAT | less -R
|
|
truckle remove WHAT # cave -cy uninstall WHAT | less -R
|
|
|
|
Some commands are not displayed by a pager, but will execute:
|
|
|
|
truckle install WHAT # cave -cy resolve -x WHAT
|
|
truckle uninstall WHAT # cave -cy uninstall -x WHAT
|
|
|
|
«do» and «resume» are special, to execute the last command:
|
|
|
|
truckle resume # | do # cave resume
|
|
|
|
Commands
|
|
========
|
|
|
|
Most commands are like by cave.
|
|
|
|
- `search` is pagered.
|
|
- `show` is pagered.
|
|
- `resolve` is pagered and resumable. Do _not_ use `-x`! Use `resume`.
|
|
- `install` is resumable and like `cave resolve -x`.
|
|
- `upgrade` is pagered and resumable and like `cave resolve -c world`. Do _not_ use `-x`! Use `resume`.
|
|
- `remove` is pagered, resumable and like `cave uninstall`. Do _not_ use `-x`! Use `resume`.
|
|
- `uninstall` is resumable and like `cave uninstall -x`.
|
|
- `fix-linkage` is pagered and resumable.
|
|
- `do` and `resume` are resumable and will resume or execute the last command.
|
|
|
|
- `--list-commands` will list all possible commands.
|
|
- `--install-commands` will install all commands as truckle-COMMAND.
|
|
EOF
|
|
|
|
cmds.on(:sync) { cave.sync.() }
|
|
cmds.on :search, :show, &pagered {|*args| cave.this(*args).() }
|
|
cmds.on :resolve, &pagered {|*args| cave.resumable!.this(*args).() }
|
|
cmds.on 'fix-linkage', &pagered {|*args|
|
|
cave.resumable!.this *args
|
|
cave.args.push '--', cave.prepare_resume_file
|
|
cave.resumable = false
|
|
cave.()
|
|
}
|
|
cmds.on :remove, &pagered {|cmd, *args| cave.resumable!.uninstall(*args).() }
|
|
cmds.on :upgrade, &pagered {|cmd, *args| cave.resumable!.resolve( '-c', :world, *args).() }
|
|
cmds.on(:install) {|cmd, *argv| cave.resumable!.resolve( '-x', *argv).() }
|
|
cmds.on(:uninstall) {|cmd, *argv| cave.resumable!.uninstall( '-x', *argv).() }
|
|
|
|
cmds.on(:do, :resume) {|cmd, *args| cave.resumable!.resume( *args).() }
|
|
|
|
cmds.on('--list-commands') { puts cmds.map{|k,v|k} }
|
|
cmds.on '--install-commands' do
|
|
dir, exe = File.split( argv0)
|
|
cmds.each do |k,v|
|
|
k = k.to_s
|
|
k = File.join dir, "#{cmds.prefix}-#{k}"
|
|
File.symlink exe, k unless File.exist? k
|
|
end
|
|
end
|
|
|
|
cave.dummy! if %w[1 true yes].include?( ENV['DUMMY'].to_s.downcase)
|
|
cave.can_be_colored if STDOUT.tty?
|
|
resumefilesuffix = if /^\d+$/ =~ ARGV[0]
|
|
"tag-#{ARGV.shift}"
|
|
elsif STDOUT.tty? and STDIN.tty? and STDOUT.stat.rdev == STDIN.stat.rdev
|
|
"dev-#{STDOUT.stat.rdev}"
|
|
else
|
|
"ppd-#{Process.ppid}"
|
|
end
|
|
cave.resume_file = "/tmp/truckle-resume-#{resumefilesuffix}"
|
|
cmds.run *ARGV
|