-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jeff Ortel <[email protected]>
- Loading branch information
Showing
4 changed files
with
150 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package command | ||
|
||
import "strings" | ||
|
||
const ( | ||
// Disabled reports: NOTHING. | ||
Disabled = -2 | ||
// Error reports: error. | ||
Error = -1 | ||
// Default reports: error, started, succeeded. | ||
Default = 0 | ||
// LiveOutput reports: error, started, succeeded, output (live). | ||
LiveOutput = 1 | ||
) | ||
|
||
// | ||
// Reporter activity reporter. | ||
type Reporter struct { | ||
Verbosity int | ||
} | ||
|
||
// | ||
// Run reports command started in task Report.Activity. | ||
func (r *Reporter) Run(path string, options Options) { | ||
switch r.Verbosity { | ||
case Disabled: | ||
case Error: | ||
case Default, | ||
LiveOutput: | ||
addon.Activity( | ||
"[CMD] Running: %s %s", | ||
path, | ||
strings.Join(options, " ")) | ||
} | ||
} | ||
|
||
// | ||
// Succeeded reports command succeeded in task Report.Activity. | ||
func (r *Reporter) Succeeded(path string) { | ||
switch r.Verbosity { | ||
case Disabled: | ||
case Error: | ||
case Default, | ||
LiveOutput: | ||
addon.Activity("[CMD] %s succeeded.", path) | ||
} | ||
} | ||
|
||
// | ||
// Error reports command failed in task Report.Activity. | ||
func (r *Reporter) Error(path string, err error, output []byte) { | ||
switch r.Verbosity { | ||
case Disabled: | ||
case Error, | ||
Default: | ||
addon.Activity( | ||
"[CMD] %s failed: %s.\n%s", | ||
path, | ||
err.Error(), | ||
output) | ||
case LiveOutput: | ||
addon.Activity( | ||
"[CMD] %s failed: %s.", | ||
path, | ||
err.Error()) | ||
} | ||
} | ||
|
||
// | ||
// Output reports command output in task Report.Activity. | ||
func (r *Reporter) Output(output string) { | ||
switch r.Verbosity { | ||
case Disabled: | ||
case Error: | ||
case Default: | ||
case LiveOutput: | ||
addon.Activity("> %s", output) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package command | ||
|
||
import "strings" | ||
|
||
// | ||
// Writer records command output. | ||
type Writer struct { | ||
reporter Reporter | ||
buffer []byte | ||
index int | ||
} | ||
|
||
// | ||
// Write command output. | ||
// When verbosity > 0, output is reported in task Report.Activity. | ||
func (w *Writer) Write(p []byte) (n int, err error) { | ||
w.buffer = append(w.buffer, p...) | ||
n = len(p) | ||
batch := string(w.buffer[w.index:]) | ||
end := strings.LastIndex(batch, "\n") | ||
if end != -1 { | ||
output := batch[:end] | ||
w.reporter.Output(output) | ||
w.index += len(output) | ||
w.index++ | ||
} | ||
return | ||
} | ||
|
||
// | ||
// Close the writer. | ||
// When verbosity > 0, output is reported in task Report.Activity. | ||
func (w *Writer) Close() { | ||
batch := string(w.buffer[w.index:]) | ||
if len(batch) > 0 { | ||
w.reporter.Output(batch) | ||
w.index += len(batch) | ||
w.index++ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters