Skip to content

Commit 9971c07

Browse files
committed
Add tag option
1 parent 49c37a8 commit 9971c07

File tree

6 files changed

+129
-33
lines changed

6 files changed

+129
-33
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Change log
22

3+
## [Unreleased]
4+
5+
### Added
6+
* Add :tag option to add custom tags to command output
7+
38
## [v0.10.1] - 2021-02-14
49

510
### Fixed

README.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ Or install it yourself as:
5353
* [2.3. Logging](#23-logging)
5454
* [2.3.1. Color](#231-color)
5555
* [2.3.2. UUID](#232-uuid)
56-
* [2.3.3. Only output on error](#233-only-output-on-error)
57-
* [2.3.4. Verbose](#234-verbose)
56+
* [2.3.3. Tag](#233-tag)
57+
* [2.3.4. Only output on error](#234-only-output-on-error)
58+
* [2.3.5. Verbose](#235-verbose)
5859
* [2.4. Dry run](#24-dry-run)
5960
* [2.5. Wait](#25-wait)
6061
* [2.6. Test](#26-test)
@@ -221,7 +222,25 @@ cmd.run("echo hello", uuid: false)
221222
# Finished in 0.003 seconds with exit status 0 (successful)
222223
```
223224

224-
#### 2.3.3 Only output on error
225+
#### 2.3.3 Tag
226+
227+
You can add custom tags to command output using the `:tag` option. This is useful for categorizing or identifying a specific instance or run of a command:
228+
229+
```ruby
230+
cmd = TTY::Command.new(tag: "deploy")
231+
cmd.run("echo hello")
232+
233+
# or individually per command run:
234+
cmd = TTY::Command.new
235+
cmd.run("echo hello", tag: "deploy")
236+
237+
# =>
238+
# [deploy] Running echo hello
239+
# hello
240+
# [deploy] Finished in 0.003 seconds with exit status 0 (successful)
241+
```
242+
243+
#### 2.3.4 Only output on error
225244

226245
When using a command that can fail, setting `:only_output_on_error` option to `true` hides the output if the command succeeds:
227246

@@ -254,7 +273,7 @@ will also print the output.
254273

255274
*Setting this option will cause the output to show at once, at the end of the command.*
256275

257-
#### 2.3.4 Verbose
276+
#### 2.3.5 Verbose
258277

259278
By default commands will produce warnings when, for example `pty` option is not supported on a given platform. You can switch off such warnings with `:verbose` option set to `false`.
260279

lib/tty/command.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,16 @@ def initialize(**options)
5555
@output = options.fetch(:output) { $stdout }
5656
@color = options.fetch(:color) { true }
5757
@uuid = options.fetch(:uuid) { true }
58+
@tag = options[:tag]
5859
@printer_name = options.fetch(:printer) { :pretty }
5960
@dry_run = options.fetch(:dry_run) { false }
60-
@printer = use_printer(@printer_name, color: @color, uuid: @uuid)
61+
@printer = use_printer(@printer_name, color: @color, uuid: @uuid, tag: @tag)
6162
@cmd_options = {}
6263
@cmd_options[:verbose] = options.fetch(:verbose, true)
6364
@cmd_options[:pty] = true if options[:pty]
6465
@cmd_options[:binmode] = true if options[:binmode]
6566
@cmd_options[:timeout] = options[:timeout] if options[:timeout]
67+
@cmd_options[:tag] = @tag if @tag
6668
end
6769

6870
# Start external executable in a child process

lib/tty/command/cmd.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ def group(value)
123123
def with_clean_env
124124
end
125125

126+
# The command tag
127+
# @api public
128+
def tag
129+
@options[:tag]
130+
end
131+
126132
# Assemble full command
127133
#
128134
# @api public
@@ -140,7 +146,8 @@ def to_hash
140146
{
141147
command: command,
142148
argv: argv,
143-
uuid: uuid
149+
uuid: uuid,
150+
tag: @options[:tag]
144151
}
145152
end
146153

lib/tty/command/printers/pretty.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Pretty < Abstract
99
def initialize(*)
1010
super
1111
@uuid = options.fetch(:uuid, true)
12+
@tag = options[:tag]
1213
end
1314

1415
def print_command_start(cmd, *args)
@@ -46,10 +47,10 @@ def print_command_exit(cmd, status, runtime, *args)
4647
def write(cmd, message, data = nil)
4748
cmd_set_uuid = cmd.options.fetch(:uuid, true)
4849
uuid_needed = cmd.options[:uuid].nil? ? @uuid : cmd_set_uuid
50+
prefix = cmd.tag || @tag || (uuid_needed ? cmd.uuid : nil)
51+
4952
out = []
50-
if uuid_needed
51-
out << "[#{decorate(cmd.uuid, :green)}] " unless cmd.uuid.nil?
52-
end
53+
out << "[#{decorate(prefix, :green)}] " if prefix
5354
out << "#{message}\n"
5455
target = (cmd.only_output_on_error && !data.nil?) ? data : output
5556
target << out.join

spec/unit/run_spec.rb

Lines changed: 86 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,36 +48,98 @@
4848
])
4949
end
5050

51-
it "runs command successfully with logging without uuid set globally" do
52-
output = StringIO.new
53-
command = TTY::Command.new(output: output, uuid: false)
51+
# TODO:
52+
# Move "with xxx option" context to the end of
53+
# the describe block to follow RSpec style guide.
54+
55+
context "with uuid option" do
56+
it "runs command successfully with logging without uuid set globally" do
57+
output = StringIO.new
58+
command = TTY::Command.new(output: output, uuid: false)
59+
60+
command.run(:echo, "hello")
61+
output.rewind
62+
63+
lines = output.readlines
64+
lines.last.gsub!(/\d+\.\d+/, "x")
65+
expect(lines).to eq([
66+
"Running \e[33;1mecho hello\e[0m\n",
67+
"\thello\n",
68+
"Finished in x seconds with exit status 0 (\e[32;1msuccessful\e[0m)\n"
69+
])
70+
end
5471

55-
command.run(:echo, "hello")
56-
output.rewind
72+
it "runs command successfully with logging without uuid set locally" do
73+
output = StringIO.new
74+
command = TTY::Command.new(output: output)
5775

58-
lines = output.readlines
59-
lines.last.gsub!(/\d+\.\d+/, "x")
60-
expect(lines).to eq([
61-
"Running \e[33;1mecho hello\e[0m\n",
62-
"\thello\n",
63-
"Finished in x seconds with exit status 0 (\e[32;1msuccessful\e[0m)\n"
64-
])
76+
command.run(:echo, "hello", uuid: false)
77+
output.rewind
78+
79+
lines = output.readlines
80+
lines.last.gsub!(/\d+\.\d+/, "x")
81+
expect(lines).to eq([
82+
"Running \e[33;1mecho hello\e[0m\n",
83+
"\thello\n",
84+
"Finished in x seconds with exit status 0 (\e[32;1msuccessful\e[0m)\n"
85+
])
86+
end
6587
end
6688

67-
it "runs command successfully with logging without uuid set locally" do
68-
output = StringIO.new
69-
command = TTY::Command.new(output: output)
89+
context "with tag option" do
90+
it "prints the tag set globally" do
91+
output = StringIO.new
92+
tag = "task"
93+
command = TTY::Command.new(output: output, tag: tag)
94+
95+
command.run(:echo, "hello")
96+
97+
output.rewind
98+
lines = output.readlines
99+
lines.last.gsub!(/\d+\.\d+/, "x")
100+
expect(lines).to eq([
101+
"[\e[32m#{tag}\e[0m] Running \e[33;1mecho hello\e[0m\n",
102+
"[\e[32m#{tag}\e[0m] \thello\n",
103+
"[\e[32m#{tag}\e[0m] Finished in x seconds with exit status 0 " \
104+
"(\e[32;1msuccessful\e[0m)\n"
105+
])
106+
end
70107

71-
command.run(:echo, "hello", uuid: false)
72-
output.rewind
108+
it "prints the tag set locally" do
109+
output = StringIO.new
110+
tag = "task"
111+
command = TTY::Command.new(output: output)
112+
113+
command.run(:echo, "hello", tag: tag)
114+
115+
output.rewind
116+
lines = output.readlines
117+
lines.last.gsub!(/\d+\.\d+/, "x")
118+
expect(lines).to eq([
119+
"[\e[32m#{tag}\e[0m] Running \e[33;1mecho hello\e[0m\n",
120+
"[\e[32m#{tag}\e[0m] \thello\n",
121+
"[\e[32m#{tag}\e[0m] Finished in x seconds with exit status 0 " \
122+
"(\e[32;1msuccessful\e[0m)\n"
123+
])
124+
end
73125

74-
lines = output.readlines
75-
lines.last.gsub!(/\d+\.\d+/, "x")
76-
expect(lines).to eq([
77-
"Running \e[33;1mecho hello\e[0m\n",
78-
"\thello\n",
79-
"Finished in x seconds with exit status 0 (\e[32;1msuccessful\e[0m)\n"
80-
])
126+
it "prints the tag even if uuid is set to false" do
127+
output = StringIO.new
128+
tag = "task"
129+
command = TTY::Command.new(output: output, tag: tag, uuid: false)
130+
131+
command.run(:echo, "hello")
132+
133+
output.rewind
134+
lines = output.readlines
135+
lines.last.gsub!(/\d+\.\d+/, "x")
136+
expect(lines).to eq([
137+
"[\e[32m#{tag}\e[0m] Running \e[33;1mecho hello\e[0m\n",
138+
"[\e[32m#{tag}\e[0m] \thello\n",
139+
"[\e[32m#{tag}\e[0m] Finished in x seconds with exit status 0 " \
140+
"(\e[32;1msuccessful\e[0m)\n"
141+
])
142+
end
81143
end
82144

83145
it "runs command and fails with logging" do

0 commit comments

Comments
 (0)