sshkit 0.0.18 → 0.0.19

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -3,6 +3,18 @@
3
3
  This file is written in reverse chronological order, newer releases will
4
4
  appear at the top.
5
5
 
6
+ ## 0.0.19
7
+
8
+ * Modified the `Pretty` formatter to include the log level in front of
9
+ executed commands.
10
+
11
+ * Modified the `Pretty` formatter not to print stdout and stderr by default,
12
+ the log level must be raised to Logger::DEBUG to see the command outputs.
13
+
14
+ * Modified the `Pretty` formatter to use `Command#to_s` when printing the
15
+ command, this prints the short form (without modifications/wrappers applied
16
+ to the command for users, groups, directories, umasks, etc).
17
+
6
18
  ## 0.0.18
7
19
 
8
20
  * Enable `as()` to take either a string/symbol as previously, but also now
@@ -53,7 +53,7 @@ module SSHKit
53
53
  cmd.started = true
54
54
  ssh.open_channel do |chan|
55
55
  chan.request_pty if config.pty
56
- chan.exec cmd.to_s do |ch, success|
56
+ chan.exec cmd.to_command do |ch, success|
57
57
  chan.on_data do |ch, data|
58
58
  cmd.stdout += data
59
59
  output << cmd
@@ -10,7 +10,7 @@ module SSHKit
10
10
  end
11
11
 
12
12
  def execute(*args)
13
- output << String(command(*args)) + "\n"
13
+ output << command(*args).to_command + "\n"
14
14
  end
15
15
 
16
16
  def capture(command, args=[])
@@ -178,17 +178,15 @@ module SSHKit
178
178
  #"newgrp #{options[:group]} <<EOC \\\"%s\\\" EOC" % %Q{#{yield}}
179
179
  end
180
180
 
181
- def to_s
182
-
181
+ def to_command
183
182
  return command.to_s unless should_map?
184
-
185
183
  within do
186
184
  umask do
187
185
  with do
188
186
  user do
189
187
  in_background do
190
188
  group do
191
- [SSHKit.config.command_map[command.to_sym], *Array(args)].join(' ')
189
+ to_s
192
190
  end
193
191
  end
194
192
  end
@@ -197,6 +195,10 @@ module SSHKit
197
195
  end
198
196
  end
199
197
 
198
+ def to_s
199
+ [SSHKit.config.command_map[command.to_sym], *Array(args)].join(' ')
200
+ end
201
+
200
202
  private
201
203
 
202
204
  def default_options
@@ -12,23 +12,28 @@ module SSHKit
12
12
  return if obj.verbosity < SSHKit.config.output_verbosity
13
13
 
14
14
  unless obj.started?
15
- original_output << "[#{c.green(obj.uuid)}] Running #{c.yellow(c.bold(String(obj)))} on #{c.yellow(obj.host.to_s)}\n"
15
+ original_output << level(obj.verbosity) + uuid(obj) + "Running #{c.yellow(c.bold(String(obj)))} on #{c.blue(obj.host.to_s)}\n"
16
+ if SSHKit.config.output_verbosity = Logger::DEBUG
17
+ original_output << level(Logger::DEBUG) + uuid(obj) + c.white("Command: #{c.blue(obj.to_command)}") + "\n"
18
+ end
16
19
  end
17
20
 
18
- if obj.complete? && !obj.stdout.empty?
19
- obj.stdout.lines.each do |line|
20
- original_output << c.green("\t" + line)
21
+ if SSHKit.config.output_verbosity == Logger::DEBUG
22
+ if obj.complete? && !obj.stdout.empty?
23
+ obj.stdout.lines.each do |line|
24
+ original_output << level(Logger::DEBUG) + uuid(obj) + c.green("\t" + line)
25
+ end
21
26
  end
22
- end
23
27
 
24
- if obj.complete? && !obj.stderr.empty?
25
- obj.stderr.lines.each do |line|
26
- original_output << c.red("\t" + line)
28
+ if obj.complete? && !obj.stderr.empty?
29
+ obj.stderr.lines.each do |line|
30
+ original_output << level(Logger::DEBUG) + uuid(obj) + c.red("\t" + line)
31
+ end
27
32
  end
28
33
  end
29
34
 
30
35
  if obj.finished?
31
- original_output << "[#{c.green(obj.uuid)}] Finished in #{sprintf('%5.3f seconds', obj.runtime)} command #{c.bold { obj.failure? ? c.red('failed') : c.green('successful') }}.\n"
36
+ original_output << level(obj.verbosity) + uuid(obj) + "Finished in #{sprintf('%5.3f seconds', obj.runtime)} command #{c.bold { obj.failure? ? c.red('failed') : c.green('successful') }}.\n"
32
37
  end
33
38
 
34
39
  else
@@ -43,6 +48,24 @@ module SSHKit
43
48
  @c ||= Term::ANSIColor
44
49
  end
45
50
 
51
+ def uuid(obj)
52
+ "[#{c.green(obj.uuid)}] "
53
+ end
54
+
55
+ def level(verbosity)
56
+ # Insane number here accounts for the control codes added
57
+ # by term-ansicolor
58
+ sprintf "%14s ", c.send(level_formatting(verbosity), level_names(verbosity))
59
+ end
60
+
61
+ def level_formatting(level_num)
62
+ %w{black blue yellow red red }[level_num]
63
+ end
64
+
65
+ def level_names(level_num)
66
+ %w{DEBUG INFO WARN ERROR FATAL}[level_num]
67
+ end
68
+
46
69
  end
47
70
 
48
71
  end
@@ -1,3 +1,3 @@
1
1
  module SSHKit
2
- VERSION = "0.0.18"
2
+ VERSION = "0.0.19"
3
3
  end
@@ -10,13 +10,13 @@ module SSHKit
10
10
 
11
11
  def test_maps_a_command
12
12
  c = Command.new('example')
13
- assert_equal '/usr/bin/env example', String(c)
13
+ assert_equal '/usr/bin/env example', c.to_command
14
14
  end
15
15
 
16
16
  def test_not_mapping_a_builtin
17
17
  %w{if test time}.each do |builtin|
18
18
  c = Command.new(builtin)
19
- assert_equal builtin, String(c)
19
+ assert_equal builtin, c.to_command
20
20
  end
21
21
  end
22
22
 
@@ -26,48 +26,48 @@ module SSHKit
26
26
  echo "Example"
27
27
  fi
28
28
  EOHEREDOC
29
- assert_equal "if test ! -d /var/log; then; echo \"Example\"; fi", String(c)
29
+ assert_equal "if test ! -d /var/log; then; echo \"Example\"; fi", c.to_command
30
30
  end
31
31
 
32
32
  def test_including_the_env
33
33
  SSHKit.config = nil
34
34
  c = Command.new(:rails, 'server', env: {rails_env: :production})
35
- assert_equal "( RAILS_ENV=production /usr/bin/env rails server )", String(c)
35
+ assert_equal "( RAILS_ENV=production /usr/bin/env rails server )", c.to_command
36
36
  end
37
37
 
38
38
  def test_including_the_env_with_multiple_keys
39
39
  SSHKit.config = nil
40
40
  c = Command.new(:rails, 'server', env: {rails_env: :production, foo: 'bar'})
41
- assert_equal "( RAILS_ENV=production FOO=bar /usr/bin/env rails server )", String(c)
41
+ assert_equal "( RAILS_ENV=production FOO=bar /usr/bin/env rails server )", c.to_command
42
42
  end
43
43
 
44
44
  def test_including_the_env_doesnt_addressively_escape
45
45
  SSHKit.config = nil
46
46
  c = Command.new(:rails, 'server', env: {path: '/example:$PATH'})
47
- assert_equal "( PATH=/example:$PATH /usr/bin/env rails server )", String(c)
47
+ assert_equal "( PATH=/example:$PATH /usr/bin/env rails server )", c.to_command
48
48
  end
49
49
 
50
50
  def test_global_env
51
51
  SSHKit.config = nil
52
52
  SSHKit.config.default_env = { default: 'env' }
53
53
  c = Command.new(:rails, 'server', env: {})
54
- assert_equal "( DEFAULT=env /usr/bin/env rails server )", String(c)
54
+ assert_equal "( DEFAULT=env /usr/bin/env rails server )", c.to_command
55
55
  end
56
56
 
57
57
  def test_default_env_is_overwritten_with_locally_defined
58
58
  SSHKit.config.default_env = { foo: 'bar', over: 'under' }
59
59
  c = Command.new(:rails, 'server', env: { over: 'write'})
60
- assert_equal "( FOO=bar OVER=write /usr/bin/env rails server )", String(c)
60
+ assert_equal "( FOO=bar OVER=write /usr/bin/env rails server )", c.to_command
61
61
  end
62
62
 
63
63
  def test_working_in_a_given_directory
64
64
  c = Command.new(:ls, '-l', in: "/opt/sites")
65
- assert_equal "cd /opt/sites && /usr/bin/env ls -l", String(c)
65
+ assert_equal "cd /opt/sites && /usr/bin/env ls -l", c.to_command
66
66
  end
67
67
 
68
68
  def test_working_in_a_given_directory_with_env
69
69
  c = Command.new(:ls, '-l', in: "/opt/sites", env: {a: :b})
70
- assert_equal "cd /opt/sites && ( A=b /usr/bin/env ls -l )", String(c)
70
+ assert_equal "cd /opt/sites && ( A=b /usr/bin/env ls -l )", c.to_command
71
71
  end
72
72
 
73
73
  def test_having_a_host_passed
@@ -78,56 +78,56 @@ module SSHKit
78
78
 
79
79
  def test_working_as_a_given_user
80
80
  c = Command.new(:whoami, user: :anotheruser)
81
- assert_equal "sudo su anotheruser -c \"/usr/bin/env whoami\"", String(c)
81
+ assert_equal "sudo su anotheruser -c \"/usr/bin/env whoami\"", c.to_command
82
82
  end
83
83
 
84
84
  def test_working_as_a_given_group
85
85
  c = Command.new(:whoami, group: :devvers)
86
- assert_equal "sg devvers -c \\\"/usr/bin/env whoami\\\"", String(c)
86
+ assert_equal "sg devvers -c \\\"/usr/bin/env whoami\\\"", c.to_command
87
87
  end
88
88
 
89
89
  def test_working_as_a_given_user_and_group
90
90
  c = Command.new(:whoami, user: :anotheruser, group: :devvers)
91
- assert_equal "sudo su anotheruser -c \"sg devvers -c \\\"/usr/bin/env whoami\\\"\"", String(c)
91
+ assert_equal "sudo su anotheruser -c \"sg devvers -c \\\"/usr/bin/env whoami\\\"\"", c.to_command
92
92
  end
93
93
 
94
94
  def test_backgrounding_a_task
95
95
  c = Command.new(:sleep, 15, run_in_background: true)
96
- assert_equal "nohup /usr/bin/env sleep 15 > /dev/null &", String(c)
96
+ assert_equal "nohup /usr/bin/env sleep 15 > /dev/null &", c.to_command
97
97
  end
98
98
 
99
99
  def test_backgrounding_a_task_as_a_given_user
100
100
  c = Command.new(:sleep, 15, run_in_background: true, user: :anotheruser)
101
- assert_equal "sudo su anotheruser -c \"nohup /usr/bin/env sleep 15 > /dev/null &\"", String(c)
101
+ assert_equal "sudo su anotheruser -c \"nohup /usr/bin/env sleep 15 > /dev/null &\"", c.to_command
102
102
  end
103
103
 
104
104
  def test_backgrounding_a_task_as_a_given_user_with_env
105
105
  c = Command.new(:sleep, 15, run_in_background: true, user: :anotheruser, env: {a: :b})
106
- assert_equal "( A=b sudo su anotheruser -c \"nohup /usr/bin/env sleep 15 > /dev/null &\" )", String(c)
106
+ assert_equal "( A=b sudo su anotheruser -c \"nohup /usr/bin/env sleep 15 > /dev/null &\" )", c.to_command
107
107
  end
108
108
 
109
109
  def test_umask
110
110
  SSHKit.config.umask = '007'
111
111
  c = Command.new(:touch, 'somefile')
112
- assert_equal "umask 007 && /usr/bin/env touch somefile", String(c)
112
+ assert_equal "umask 007 && /usr/bin/env touch somefile", c.to_command
113
113
  end
114
114
 
115
115
  def test_umask_with_working_directory
116
116
  SSHKit.config.umask = '007'
117
117
  c = Command.new(:touch, 'somefile', in: '/opt')
118
- assert_equal "cd /opt && umask 007 && /usr/bin/env touch somefile", String(c)
118
+ assert_equal "cd /opt && umask 007 && /usr/bin/env touch somefile", c.to_command
119
119
  end
120
120
 
121
121
  def test_umask_with_working_directory_and_user
122
122
  SSHKit.config.umask = '007'
123
123
  c = Command.new(:touch, 'somefile', in: '/var', user: 'alice')
124
- assert_equal "cd /var && umask 007 && sudo su alice -c \"/usr/bin/env touch somefile\"", String(c)
124
+ assert_equal "cd /var && umask 007 && sudo su alice -c \"/usr/bin/env touch somefile\"", c.to_command
125
125
  end
126
126
 
127
127
  def test_umask_with_env_and_working_directory_and_user
128
128
  SSHKit.config.umask = '007'
129
129
  c = Command.new(:touch, 'somefile', user: 'bob', env: {a: 'b'}, in: '/var')
130
- assert_equal "cd /var && umask 007 && ( A=b sudo su bob -c \"/usr/bin/env touch somefile\" )", String(c)
130
+ assert_equal "cd /var && umask 007 && ( A=b sudo su bob -c \"/usr/bin/env touch somefile\" )", c.to_command
131
131
  end
132
132
 
133
133
  def test_verbosity_defaults_to_logger_info
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sshkit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.18
4
+ version: 0.0.19
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: