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
|
# File 'lib/xlogin/cli.rb', line 62
def run(args)
config = Hash.new
config[:runner] = self.method(:tty)
config[:inventory] = DEFAULT_INVENTORY_FILE
config[:templates] = DEFAULT_TEMPLATE_DIR
parser = OptionParser.new
parser.banner = "#{File.basename($0)} HOST_PATTERN [Options]"
parser.version = Xlogin::VERSION
parser.on('-i PATH', '--inventory', String, 'The PATH to the inventory file (default: $HOME/.xloginrc).')
parser.on('-T PATH', '--template', String, 'The PATH to the template dir (default: $HOME/.xlogin.d).')
parser.on('-L [DIRECTORY]', '--log-dir', String, 'The PATH to the log dir (default: $PWD).') { |v| v || '.' }
parser.on('-t', '--tty', TrueClass, 'Allocate a pseudo-tty.') { |v| config[:runner] = self.method(:tty) }
parser.on('-l', '--list', TrueClass, 'List the inventory.') { |v| config[:runner] = self.method(:list) }
parser.on('-e COMMAND', '--exec', 'Execute commands and quit.') { |v| config[:runner] = self.method(:exec); v }
parser.on('-j NUM', '--jobs', Integer, 'The NUM of jobs to execute in parallel(default: 1).')
parser.on('-E', '--enable', TrueClass, 'Try to gain enable priviledge.')
parser.on('-y', '--assume-yes', TrueClass, 'Automatically answer yes to prompts.')
parser.parse!(args, into: config)
config[:pattern] = args
Xlogin.configure do
set assume_yes: true if config[:assume_yes]
source File.expand_path(config[:inventory], ENV['PWD'])
template File.expand_path(config[:templates], ENV['PWD'])
end
raise "No host found: `#{args.join(', ')}`" if Xlogin.list(*config[:pattern]).empty?
config[:runner].call(config)
rescue => e
$stderr.puts e, '', parser
exit 1
end
|