tty-command 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -0
  3. data/LICENSE.txt +1 -1
  4. data/README.md +44 -44
  5. data/lib/tty-command.rb +1 -1
  6. data/lib/tty/command.rb +18 -18
  7. data/lib/tty/command/child_process.rb +9 -9
  8. data/lib/tty/command/cmd.rb +11 -11
  9. data/lib/tty/command/dry_runner.rb +3 -3
  10. data/lib/tty/command/exit_error.rb +1 -1
  11. data/lib/tty/command/printers/abstract.rb +8 -10
  12. data/lib/tty/command/printers/null.rb +1 -4
  13. data/lib/tty/command/printers/pretty.rb +10 -15
  14. data/lib/tty/command/printers/progress.rb +3 -8
  15. data/lib/tty/command/printers/quiet.rb +3 -7
  16. data/lib/tty/command/process_runner.rb +25 -17
  17. data/lib/tty/command/truncator.rb +3 -3
  18. data/lib/tty/command/version.rb +1 -1
  19. metadata +20 -68
  20. data/Rakefile +0 -10
  21. data/bin/console +0 -6
  22. data/bin/setup +0 -6
  23. data/examples/bash.rb +0 -12
  24. data/examples/basic.rb +0 -9
  25. data/examples/buffer.rb +0 -4
  26. data/examples/env.rb +0 -9
  27. data/examples/logger.rb +0 -10
  28. data/examples/output.rb +0 -10
  29. data/examples/pty.rb +0 -9
  30. data/examples/redirect_stderr.rb +0 -10
  31. data/examples/redirect_stdin.rb +0 -15
  32. data/examples/redirect_stdout.rb +0 -10
  33. data/examples/stdin_input.rb +0 -10
  34. data/examples/threaded.rb +0 -14
  35. data/examples/timeout.rb +0 -11
  36. data/examples/timeout_input.rb +0 -12
  37. data/examples/wait.rb +0 -21
  38. data/spec/spec_helper.rb +0 -78
  39. data/spec/unit/binmode_spec.rb +0 -29
  40. data/spec/unit/cmd_spec.rb +0 -152
  41. data/spec/unit/dry_run_spec.rb +0 -42
  42. data/spec/unit/exit_error_spec.rb +0 -25
  43. data/spec/unit/input_spec.rb +0 -13
  44. data/spec/unit/output_spec.rb +0 -25
  45. data/spec/unit/printer_spec.rb +0 -50
  46. data/spec/unit/printers/custom_spec.rb +0 -48
  47. data/spec/unit/printers/null_spec.rb +0 -36
  48. data/spec/unit/printers/pretty_spec.rb +0 -172
  49. data/spec/unit/printers/progress_spec.rb +0 -45
  50. data/spec/unit/printers/quiet_spec.rb +0 -71
  51. data/spec/unit/pty_spec.rb +0 -62
  52. data/spec/unit/redirect_spec.rb +0 -103
  53. data/spec/unit/result_spec.rb +0 -64
  54. data/spec/unit/ruby_spec.rb +0 -20
  55. data/spec/unit/run_spec.rb +0 -159
  56. data/spec/unit/test_spec.rb +0 -11
  57. data/spec/unit/timeout_spec.rb +0 -36
  58. data/spec/unit/truncator_spec.rb +0 -73
  59. data/tasks/console.rake +0 -11
  60. data/tasks/coverage.rake +0 -11
  61. data/tasks/spec.rake +0 -29
  62. data/tty-command.gemspec +0 -30
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'securerandom'
4
- require 'shellwords'
3
+ require "securerandom"
4
+ require "shellwords"
5
5
 
6
6
  module TTY
7
7
  class Command
@@ -33,14 +33,14 @@ module TTY
33
33
  if env_or_cmd.respond_to?(:to_hash)
34
34
  @env = env_or_cmd
35
35
  unless command = args.shift
36
- raise ArgumentError, 'Cmd requires command argument'
36
+ raise ArgumentError, "Cmd requires command argument"
37
37
  end
38
38
  else
39
39
  command = env_or_cmd
40
40
  end
41
41
 
42
42
  if args.empty? && cmd = command.to_s
43
- raise ArgumentError, 'No command provided' if cmd.empty?
43
+ raise ArgumentError, "No command provided" if cmd.empty?
44
44
  @command = sanitize(cmd)
45
45
  @argv = []
46
46
  else
@@ -55,7 +55,7 @@ module TTY
55
55
  @env ||= {}
56
56
  @options = opts
57
57
 
58
- @uuid = SecureRandom.uuid.split('-')[0]
58
+ @uuid = SecureRandom.uuid.split("-")[0]
59
59
  @only_output_on_error = opts.fetch(:only_output_on_error) { false }
60
60
  freeze
61
61
  end
@@ -63,7 +63,7 @@ module TTY
63
63
  # Extend command options if keys don't already exist
64
64
  #
65
65
  # @api public
66
- def update(**options)
66
+ def update(options)
67
67
  @options.update(options.update(@options))
68
68
  end
69
69
 
@@ -79,12 +79,12 @@ module TTY
79
79
  converted_key = key.is_a?(Symbol) ? key.to_s.upcase : key.to_s
80
80
  escaped_val = val.to_s.gsub(/"/, '\"')
81
81
  %(#{converted_key}="#{escaped_val}")
82
- end.join(' ')
82
+ end.join(" ")
83
83
  end
84
84
 
85
85
  def evars(value, &block)
86
86
  return (value || block) unless environment.any?
87
- %(( export #{environment_string} ; %s )) % [value || block.call]
87
+ "( export #{environment_string} ; #{value || block.call} )"
88
88
  end
89
89
 
90
90
  def umask(value)
@@ -94,12 +94,12 @@ module TTY
94
94
 
95
95
  def chdir(value)
96
96
  return value unless options[:chdir]
97
- %(cd #{options[:chdir]} && %s) % [value]
97
+ %(cd #{Shellwords.escape(options[:chdir])} && #{value})
98
98
  end
99
99
 
100
100
  def user(value)
101
101
  return value unless options[:user]
102
- vars = environment.any? ? "#{environment_string} " : ''
102
+ vars = environment.any? ? "#{environment_string} " : ""
103
103
  %(sudo -u #{options[:user]} #{vars}-- sh -c '%s') % [value]
104
104
  end
105
105
 
@@ -123,7 +123,7 @@ module TTY
123
123
 
124
124
  # @api public
125
125
  def to_s
126
- [command.to_s, *Array(argv)].join(' ')
126
+ [command.to_s, *Array(argv)].join(" ")
127
127
  end
128
128
 
129
129
  # @api public
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'result'
3
+ require_relative "result"
4
4
 
5
5
  module TTY
6
6
  class Command
@@ -17,10 +17,10 @@ module TTY
17
17
  # @api public
18
18
  def run!(*)
19
19
  cmd.to_command
20
- message = "#{@printer.decorate('(dry run)', :blue)} " +
20
+ message = "#{@printer.decorate("(dry run)", :blue)} " +
21
21
  @printer.decorate(cmd.to_command, :yellow, :bold)
22
22
  @printer.write(cmd, message, cmd.uuid)
23
- Result.new(0, '', '')
23
+ Result.new(0, "", "")
24
24
  end
25
25
  end # DryRunner
26
26
  end # Command
@@ -24,7 +24,7 @@ module TTY
24
24
  end
25
25
 
26
26
  def extract_output(value)
27
- (value || '').strip.empty? ? 'Nothing written' : value.strip
27
+ (value || "").strip.empty? ? "Nothing written" : value.strip
28
28
  end
29
29
  end # ExitError
30
30
  end # Command
@@ -1,6 +1,4 @@
1
- # encoding: utf-8
2
-
3
- require 'pastel'
1
+ require "pastel"
4
2
 
5
3
  module TTY
6
4
  class Command
@@ -22,11 +20,11 @@ module TTY
22
20
  def initialize(output, options = {})
23
21
  @output = output
24
22
  @options = options
25
- @enabled = options.fetch(:color) { true }
26
- @color = ::Pastel.new(output: output, enabled: @enabled)
23
+ @enabled = options.fetch(:color, true)
24
+ @color = ::Pastel.new(enabled: @enabled)
27
25
 
28
- @out_data = ''
29
- @err_data = ''
26
+ @out_data = ""
27
+ @err_data = ""
30
28
  end
31
29
 
32
30
  def print_command_start(cmd, *args)
@@ -34,15 +32,15 @@ module TTY
34
32
  end
35
33
 
36
34
  def print_command_out_data(cmd, *args)
37
- write(args.join(' '))
35
+ write(args.join(" "))
38
36
  end
39
37
 
40
38
  def print_command_err_data(cmd, *args)
41
- write(args.join(' '))
39
+ write(args.join(" "))
42
40
  end
43
41
 
44
42
  def print_command_exit(cmd, *args)
45
- write(args.join(' '))
43
+ write(args.join(" "))
46
44
  end
47
45
 
48
46
  def write(cmd, message)
@@ -1,7 +1,4 @@
1
- # encoding: utf-8
2
- # frozen_string_literal: true
3
-
4
- require_relative 'abstract'
1
+ require_relative "abstract"
5
2
 
6
3
  module TTY
7
4
  class Command
@@ -1,34 +1,29 @@
1
- # encoding: utf-8
2
- # frozen_string_literal: true
3
-
4
- require 'pastel'
5
-
6
- require_relative 'abstract'
1
+ require_relative "abstract"
7
2
 
8
3
  module TTY
9
4
  class Command
10
5
  module Printers
11
6
  class Pretty < Abstract
12
- TIME_FORMAT = "%5.3f %s".freeze
7
+ TIME_FORMAT = "%5.3f %s"
13
8
 
14
9
  def initialize(*)
15
10
  super
16
- @uuid = options.fetch(:uuid) { true }
11
+ @uuid = options.fetch(:uuid, true)
17
12
  end
18
13
 
19
14
  def print_command_start(cmd, *args)
20
15
  message = ["Running #{decorate(cmd.to_command, :yellow, :bold)}"]
21
- message << args.map(&:chomp).join(' ') unless args.empty?
16
+ message << args.map(&:chomp).join(" ") unless args.empty?
22
17
  write(cmd, message.join)
23
18
  end
24
19
 
25
20
  def print_command_out_data(cmd, *args)
26
- message = args.map(&:chomp).join(' ')
21
+ message = args.map(&:chomp).join(" ")
27
22
  write(cmd, "\t#{message}", out_data)
28
23
  end
29
24
 
30
25
  def print_command_err_data(cmd, *args)
31
- message = args.map(&:chomp).join(' ')
26
+ message = args.map(&:chomp).join(" ")
32
27
  write(cmd, "\t" + decorate(message, :red), err_data)
33
28
  end
34
29
 
@@ -38,7 +33,7 @@ module TTY
38
33
  output << err_data
39
34
  end
40
35
 
41
- runtime = TIME_FORMAT % [runtime, pluralize(runtime, 'second')]
36
+ runtime = TIME_FORMAT % [runtime, pluralize(runtime, "second")]
42
37
  message = ["Finished in #{runtime}"]
43
38
  message << " with exit status #{status}" if status
44
39
  message << " (#{success_or_failure(status)})"
@@ -66,15 +61,15 @@ module TTY
66
61
  #
67
62
  # @api private
68
63
  def pluralize(count, word)
69
- "#{word}#{'s' unless count.to_f == 1}"
64
+ "#{word}#{'s' unless count.to_i == 1}"
70
65
  end
71
66
 
72
67
  # @api private
73
68
  def success_or_failure(status)
74
69
  if status == 0
75
- decorate('successful', :green, :bold)
70
+ decorate("successful", :green, :bold)
76
71
  else
77
- decorate('failed', :red, :bold)
72
+ decorate("failed", :red, :bold)
78
73
  end
79
74
  end
80
75
  end # Pretty
@@ -1,14 +1,9 @@
1
- # encoding: utf-8
2
- # frozen_string_literal: true
3
-
4
- require 'pastel'
5
- require_relative 'abstract'
1
+ require_relative "abstract"
6
2
 
7
3
  module TTY
8
4
  class Command
9
5
  module Printers
10
6
  class Progress < Abstract
11
-
12
7
  def print_command_exit(cmd, status, runtime, *args)
13
8
  output.print(success_or_failure(status))
14
9
  end
@@ -21,9 +16,9 @@ module TTY
21
16
  # @api private
22
17
  def success_or_failure(status)
23
18
  if status == 0
24
- decorate('.', :green)
19
+ decorate(".", :green)
25
20
  else
26
- decorate('F', :red)
21
+ decorate("F", :red)
27
22
  end
28
23
  end
29
24
  end # Progress
@@ -1,23 +1,19 @@
1
- # encoding: utf-8
2
-
3
- require_relative 'abstract'
1
+ require_relative "abstract"
4
2
 
5
3
  module TTY
6
4
  class Command
7
5
  module Printers
8
6
  class Quiet < Abstract
9
- attr_reader :output, :options
10
-
11
7
  def print_command_start(cmd)
12
8
  # quiet
13
9
  end
14
10
 
15
11
  def print_command_out_data(cmd, *args)
16
- write(cmd, args.join(' '), out_data)
12
+ write(cmd, args.join(" "), out_data)
17
13
  end
18
14
 
19
15
  def print_command_err_data(cmd, *args)
20
- write(cmd, args.join(' '), err_data)
16
+ write(cmd, args.join(" "), err_data)
21
17
  end
22
18
 
23
19
  def print_command_exit(cmd, status, *args)
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'thread'
3
+ require "thread"
4
4
 
5
- require_relative 'child_process'
6
- require_relative 'result'
7
- require_relative 'truncator'
5
+ require_relative "child_process"
6
+ require_relative "result"
7
+ require_relative "truncator"
8
8
 
9
9
  module TTY
10
10
  class Command
@@ -128,20 +128,20 @@ module TTY
128
128
  stdout_data = []
129
129
  stderr_data = Truncator.new
130
130
 
131
- out_buffer = ->(line) {
132
- stdout_data << line
133
- @printer.print_command_out_data(cmd, line)
134
- @block.(line, nil) if @block
131
+ out_handler = ->(data) {
132
+ stdout_data << data
133
+ @printer.print_command_out_data(cmd, data)
134
+ @block.(data, nil) if @block
135
135
  }
136
136
 
137
- err_buffer = ->(line) {
138
- stderr_data << line
139
- @printer.print_command_err_data(cmd, line)
140
- @block.(nil, line) if @block
137
+ err_handler = ->(data) {
138
+ stderr_data << data
139
+ @printer.print_command_err_data(cmd, data)
140
+ @block.(nil, data) if @block
141
141
  }
142
142
 
143
- stdout_thread = read_stream(stdout, out_buffer)
144
- stderr_thread = read_stream(stderr, err_buffer)
143
+ stdout_thread = read_stream(stdout, out_handler)
144
+ stderr_thread = read_stream(stderr, err_handler)
145
145
 
146
146
  stdout_thread.join
147
147
  stderr_thread.join
@@ -154,7 +154,15 @@ module TTY
154
154
  ]
155
155
  end
156
156
 
157
- def read_stream(stream, buffer)
157
+ # Read stream and invoke handler when data becomes available
158
+ #
159
+ # @param [IO] stream
160
+ # the stream to read data from
161
+ # @param [Proc] handler
162
+ # the handler to call when data becomes available
163
+ #
164
+ # @api private
165
+ def read_stream(stream, handler)
158
166
  Thread.new do
159
167
  if Thread.current.respond_to?(:report_on_exception)
160
168
  Thread.current.report_on_exception = false
@@ -168,8 +176,8 @@ module TTY
168
176
 
169
177
  ready[0].each do |reader|
170
178
  begin
171
- line = reader.readpartial(BUFSIZE)
172
- buffer.(line)
179
+ chunk = reader.readpartial(BUFSIZE)
180
+ handler.(chunk)
173
181
 
174
182
  # control total time spent reading
175
183
  runtime = Time.now - Thread.current[:cmd_start]
@@ -17,8 +17,8 @@ module TTY
17
17
  # @api public
18
18
  def initialize(options = {})
19
19
  @max_size = options.fetch(:max_size) { DEFAULT_SIZE }
20
- @prefix = ''
21
- @suffix = ''
20
+ @prefix = ""
21
+ @suffix = ""
22
22
  @skipped = 0
23
23
  end
24
24
 
@@ -92,7 +92,7 @@ module TTY
92
92
  # @api private
93
93
  def append(value, dst)
94
94
  remain = @max_size - dst.bytesize
95
- remaining = ''
95
+ remaining = ""
96
96
  if remain > 0
97
97
  value_bytes = value.to_s.bytesize
98
98
  offset = value_bytes < remain ? value_bytes : remain
@@ -2,6 +2,6 @@
2
2
 
3
3
  module TTY
4
4
  class Command
5
- VERSION = '0.9.0'
5
+ VERSION = "0.10.0"
6
6
  end # Command
7
7
  end # TTY
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tty-command
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Murach
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-09-28 00:00:00.000000000 Z
11
+ date: 2020-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pastel
@@ -16,28 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.7.0
19
+ version: '0.8'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.7.0
27
- - !ruby/object:Gem::Dependency
28
- name: bundler
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: 1.5.0
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: 1.5.0
26
+ version: '0.8'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: rake
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -56,46 +42,31 @@ dependencies:
56
42
  name: rspec
57
43
  requirement: !ruby/object:Gem::Requirement
58
44
  requirements:
59
- - - "~>"
45
+ - - ">="
60
46
  - !ruby/object:Gem::Version
61
47
  version: '3.0'
62
48
  type: :development
63
49
  prerelease: false
64
50
  version_requirements: !ruby/object:Gem::Requirement
65
51
  requirements:
66
- - - "~>"
52
+ - - ">="
67
53
  - !ruby/object:Gem::Version
68
54
  version: '3.0'
69
55
  description: Execute shell commands with pretty output logging and capture their stdout,
70
56
  stderr and exit status. Redirect stdin, stdout and stderr of each command to a file
71
57
  or a string.
72
58
  email:
73
- - me@piotrmurach.com
59
+ - piotr@piotrmurach.com
74
60
  executables: []
75
61
  extensions: []
76
- extra_rdoc_files: []
62
+ extra_rdoc_files:
63
+ - README.md
64
+ - CHANGELOG.md
65
+ - LICENSE.txt
77
66
  files:
78
67
  - CHANGELOG.md
79
68
  - LICENSE.txt
80
69
  - README.md
81
- - Rakefile
82
- - bin/console
83
- - bin/setup
84
- - examples/bash.rb
85
- - examples/basic.rb
86
- - examples/buffer.rb
87
- - examples/env.rb
88
- - examples/logger.rb
89
- - examples/output.rb
90
- - examples/pty.rb
91
- - examples/redirect_stderr.rb
92
- - examples/redirect_stdin.rb
93
- - examples/redirect_stdout.rb
94
- - examples/stdin_input.rb
95
- - examples/threaded.rb
96
- - examples/timeout.rb
97
- - examples/timeout_input.rb
98
- - examples/wait.rb
99
70
  - lib/tty-command.rb
100
71
  - lib/tty/command.rb
101
72
  - lib/tty/command/child_process.rb
@@ -111,35 +82,16 @@ files:
111
82
  - lib/tty/command/result.rb
112
83
  - lib/tty/command/truncator.rb
113
84
  - lib/tty/command/version.rb
114
- - spec/spec_helper.rb
115
- - spec/unit/binmode_spec.rb
116
- - spec/unit/cmd_spec.rb
117
- - spec/unit/dry_run_spec.rb
118
- - spec/unit/exit_error_spec.rb
119
- - spec/unit/input_spec.rb
120
- - spec/unit/output_spec.rb
121
- - spec/unit/printer_spec.rb
122
- - spec/unit/printers/custom_spec.rb
123
- - spec/unit/printers/null_spec.rb
124
- - spec/unit/printers/pretty_spec.rb
125
- - spec/unit/printers/progress_spec.rb
126
- - spec/unit/printers/quiet_spec.rb
127
- - spec/unit/pty_spec.rb
128
- - spec/unit/redirect_spec.rb
129
- - spec/unit/result_spec.rb
130
- - spec/unit/ruby_spec.rb
131
- - spec/unit/run_spec.rb
132
- - spec/unit/test_spec.rb
133
- - spec/unit/timeout_spec.rb
134
- - spec/unit/truncator_spec.rb
135
- - tasks/console.rake
136
- - tasks/coverage.rake
137
- - tasks/spec.rake
138
- - tty-command.gemspec
139
- homepage: https://piotrmurach.github.io/tty
85
+ homepage: https://ttytoolkit.org
140
86
  licenses:
141
87
  - MIT
142
- metadata: {}
88
+ metadata:
89
+ allowed_push_host: https://rubygems.org
90
+ bug_tracker_uri: https://github.com/piotrmurach/tty-command/issues
91
+ changelog_uri: https://github.com/piotrmurach/tty-command/blob/master/CHANGELOG.md
92
+ documentation_uri: https://www.rubydoc.info/gems/tty-command
93
+ homepage_uri: https://ttytoolkit.org
94
+ source_code_uri: https://github.com/piotrmurach/tty-command
143
95
  post_install_message:
144
96
  rdoc_options: []
145
97
  require_paths:
@@ -155,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
155
107
  - !ruby/object:Gem::Version
156
108
  version: '0'
157
109
  requirements: []
158
- rubygems_version: 3.0.3
110
+ rubygems_version: 3.1.2
159
111
  signing_key:
160
112
  specification_version: 4
161
113
  summary: Execute shell commands with pretty output logging and capture their stdout,