xlogin 0.11.1 → 0.11.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f42c5865068748beef0105170a86a417541a2e5795bd2255c8922a8a087580df
4
- data.tar.gz: 58fc9bd6cc07cfb251cf296d002337b4fefcd44f558ffcc6a3ccbc18a2c06dfc
3
+ metadata.gz: '0086c05feba32e88bf808e6192b10e7a960876aa47d2091310a3fed7c1297fc7'
4
+ data.tar.gz: b82299076bf25526406a516c762cdfa5add6c33d768aceced613691213630140
5
5
  SHA512:
6
- metadata.gz: 3a05591f833bf303edf41aca096d3c79bf8367fc4d97efdd9ce32b86016d63fb2b57932c716039c1566ded07b831c88a666a60fadc5001331a22db91e3d40cf9
7
- data.tar.gz: 1f4288e96bdb275dae475772dd3c3e161a0ad48916ed1ce8e90b5e93afec6cc550b09271d8cb3232053cd4997c2dfa6b3e9931988302ce68c25e8143fc60b7fc
6
+ metadata.gz: 15b2680b9c90bee421c20c5af86bc1fa338b06d69f39b3bdbbb0890738214238e4bfe08fb13b40dcfe2adcaae6a3e3b196a09bd5ac491fdcec9b7fa0fd5287a0
7
+ data.tar.gz: 41b40ad9811148bbf294cd00ff18bccaaa347bf51f13b6f8fe5776a2d70556708e17228ab536ca32ab1b16290437e919f505fed6f8b16428155326ffa07bbd6a
data/lib/xlogin/cli.rb CHANGED
@@ -7,19 +7,19 @@ require 'readline'
7
7
  require 'stringio'
8
8
 
9
9
  module Xlogin
10
- class CLI
10
+ class CLI
11
11
 
12
- DEFAULT_INVENTORY_FILE = File.join(ENV['HOME'], '.xloginrc')
13
- DEFAULT_TEMPLATE_DIR = File.join(ENV['HOME'], '.xlogin.d')
12
+ DEFAULT_INVENTORY_FILE = File.join(ENV['HOME'], '.xloginrc')
13
+ DEFAULT_TEMPLATE_DIR = File.join(ENV['HOME'], '.xlogin.d')
14
14
 
15
- def self.run(args = ARGV)
16
- config = getopts(args)
17
- client = Xlogin::CLI.new
15
+ def self.run(args = ARGV)
16
+ config = getopts(args)
17
+ client = Xlogin::CLI.new
18
18
  client.method(config.task.first).call(config)
19
- end
19
+ end
20
20
 
21
- def self.getopts(args)
22
- config = OpenStruct.new(
21
+ def self.getopts(args)
22
+ config = OpenStruct.new(
23
23
  jobs: 1,
24
24
  auth: false,
25
25
  task: [:tty, nil],
@@ -27,21 +27,21 @@ module Xlogin
27
27
  template_dir: DEFAULT_TEMPLATE_DIR,
28
28
  )
29
29
 
30
- parser = OptionParser.new
31
- parser.banner = "#{File.basename($0)} HOST_PATTERN [Options]"
32
- parser.version = Xlogin::VERSION
30
+ parser = OptionParser.new
31
+ parser.banner = "#{File.basename($0)} HOST_PATTERN [Options]"
32
+ parser.version = Xlogin::VERSION
33
33
 
34
- parser.on('-i PATH', '--inventory', String, 'The PATH to the inventory file (default: $HOME/.xloginrc).') { |v| config.inventory = v }
34
+ parser.on('-i PATH', '--inventory', String, 'The PATH to the inventory file (default: $HOME/.xloginrc).') { |v| config.inventory = v }
35
35
  parser.on('-T PATH', '--template', String, 'The PATH to the template dir (default: $HOME/.xlogin.d).') { |v| config.template_dir = v }
36
36
  parser.on('-L [DIRECTORY]', '--log-dir', String, 'The PATH to the log dir (default: $PWD).') { |v| config.logdir = v || '.' }
37
37
 
38
- parser.on('-l', '--list', TrueClass, 'List the inventory.') { |v| config.task = [:list, nil] }
39
- parser.on('-t', '--tty', TrueClass, 'Allocate a pseudo-tty.') { |v| config.task = [:tty, nil] }
40
- parser.on('-e COMMAND', '--exec', 'Execute commands and quit.') { |v| config.task = [:exec, v] }
38
+ parser.on('-l', '--list', TrueClass, 'List the inventory.') { |v| config.task = [:list, nil] }
39
+ parser.on('-t', '--tty', TrueClass, 'Allocate a pseudo-tty.') { |v| config.task = [:tty, nil] }
40
+ parser.on('-e COMMAND', '--exec', 'Execute commands and quit.') { |v| config.task = [:exec, v] }
41
41
 
42
- parser.on('-j NUM', '--jobs', Integer, 'The NUM of jobs to execute in parallel(default: 1).') { |v| config.jobs = v }
43
- parser.on('-y', '--assume-yes', TrueClass, 'Automatically answer yes to prompts.') { |v| config.auth = v }
44
- parser.on('-E', '--enable', TrueClass, 'Try to gain enable priviledge.') { |v| config.enable = v }
42
+ parser.on('-j NUM', '--jobs', Integer, 'The NUM of jobs to execute in parallel(default: 1).') { |v| config.jobs = v }
43
+ parser.on('-y', '--assume-yes', TrueClass, 'Automatically answer yes to prompts.') { |v| config.auth = v }
44
+ parser.on('-E', '--enable', TrueClass, 'Try to gain enable priviledge.') { |v| config.enable = v }
45
45
 
46
46
  parser.parse!(args)
47
47
  Xlogin.configure do
@@ -53,58 +53,58 @@ module Xlogin
53
53
  config.hosts = Xlogin.list(*args)
54
54
  raise "No host found: `#{args.join(', ')}`" if config.hosts.empty?
55
55
 
56
- return config
56
+ return config
57
57
  rescue => e
58
58
  $stderr.puts e, '', parser
59
59
  exit 1
60
- end
60
+ end
61
61
 
62
- def list(config)
63
- $stdout.puts config.hosts.map { |e| e[:name] }.sort.uniq
64
- end
62
+ def list(config)
63
+ $stdout.puts config.hosts.map { |e| e[:name] }.sort.uniq
64
+ end
65
65
 
66
- def tty(config)
67
- Signal.trap(:INT) { exit 0 }
66
+ def tty(config)
67
+ Signal.trap(:INT) { exit 0 }
68
68
 
69
69
  config.hosts.each do |hostinfo|
70
70
  unless config.hosts.size == 1
71
- case resp = Readline.readline(">> #{hostinfo[:name]}(Y/n)? ", false).strip
72
- when /^y(es)?$/i, ''
73
- when /^n(o)?$/i then next
74
- else redo
75
- end
76
- end
77
-
78
- $stdout.puts "Trying #{hostinfo[:name]}...", "Escape character is '^]'."
71
+ case resp = Readline.readline(">> #{hostinfo[:name]}(Y/n)? ", false).strip
72
+ when /^y(es)?$/i, ''
73
+ when /^n(o)?$/i then next
74
+ else redo
75
+ end
76
+ end
77
+
78
+ $stdout.puts "Trying #{hostinfo[:name]}...", "Escape character is '^]'."
79
79
  tty_config = OpenStruct.new(config.to_h.merge(jobs: 1, hosts: [hostinfo]))
80
80
 
81
- session, _ = exec(tty_config)
82
- session.interact!
83
- end
84
- end
81
+ session, _ = exec(tty_config)
82
+ session.interact!
83
+ end
84
+ end
85
85
 
86
- def exec(config)
87
- Signal.trap(:INT) { exit 0 }
86
+ def exec(config)
87
+ Signal.trap(:INT) { exit 0 }
88
88
 
89
- max_width = config.hosts.map { |e| e[:name].length }.max
90
- Parallel.map(config.hosts, in_threads: config.jobs) do |hostinfo|
91
- session = nil
92
- error = nil
89
+ max_width = config.hosts.map { |e| e[:name].length }.max
90
+ Parallel.map(config.hosts, in_threads: config.jobs) do |hostinfo|
91
+ session = nil
92
+ error = nil
93
93
 
94
- begin
95
- buffer = StringIO.new
94
+ begin
95
+ buffer = StringIO.new
96
96
 
97
- loggers = []
98
- loggers << ((config.jobs > 1)? buffer : $stdout)
99
- loggers << File.expand_path(File.join(config.logdir, "#{hostinfo[:name]}.log"), ENV['PWD']) if config.logdir
97
+ loggers = []
98
+ loggers << ((config.jobs > 1)? buffer : $stdout)
99
+ loggers << File.expand_path(File.join(config.logdir, "#{hostinfo[:name]}.log"), ENV['PWD']) if config.logdir
100
100
 
101
- session = Xlogin.get(hostinfo.merge(log: loggers))
102
- session.enable if config.enable && hostinfo[:enable]
101
+ session = Xlogin.get(hostinfo.merge(log: loggers))
102
+ session.enable(hostinfo[:enable]) if config.enable && hostinfo[:enable]
103
103
 
104
104
  command_lines = ['', *config.task.last.to_s.split(';').map(&:strip)]
105
- command_lines.each { |line| session.cmd(line) }
106
- rescue => err
107
- error = err
105
+ command_lines.each { |line| session.cmd(line) }
106
+ rescue => err
107
+ error = err
108
108
  end
109
109
 
110
110
  if config.jobs > 1
@@ -116,9 +116,9 @@ module Xlogin
116
116
  $stderr.print "[Error] #{error}\n" if error
117
117
  end
118
118
 
119
- session
120
- end
121
- end
119
+ session
120
+ end
121
+ end
122
122
 
123
- end
123
+ end
124
124
  end
@@ -10,7 +10,6 @@ module Xlogin
10
10
  module SessionModule
11
11
 
12
12
  attr_accessor :name
13
- attr_accessor :config
14
13
 
15
14
  def initialize(template, uri, **opts)
16
15
  @uri = uri
@@ -52,11 +51,6 @@ module Xlogin
52
51
  @template.name
53
52
  end
54
53
 
55
- def enable(*args)
56
- return super(*args) unless args.empty?
57
- super(@config.enable)
58
- end
59
-
60
54
  def prompt
61
55
  cmd('').to_s.lines.last&.chomp
62
56
  end
@@ -42,7 +42,12 @@ module Xlogin
42
42
  klass = Class.new(Xlogin.const_get(uri.scheme.capitalize))
43
43
  klass.class_exec(@methods) do |methods|
44
44
  methods.each do |name, block|
45
- define_method(name, &block)
45
+ case name.to_s
46
+ when 'enable'
47
+ define_method(name) { |args = nil| instance_exec(args || opts[:enable], &block) }
48
+ else
49
+ define_method(name, &block)
50
+ end
46
51
  end
47
52
  end
48
53
 
@@ -1,3 +1,3 @@
1
1
  module Xlogin
2
- VERSION = "0.11.1"
2
+ VERSION = "0.11.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xlogin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.1
4
+ version: 0.11.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - haccht