-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
✨ Command verbosity. #49
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,119 @@ | ||
package command | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// | ||
// Verbosity. | ||
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 | ||
) | ||
|
||
// | ||
// ReportFilter filter reported output. | ||
type ReportFilter func(in string) (out string) | ||
|
||
// | ||
// Reporter activity reporter. | ||
type Reporter struct { | ||
Filter ReportFilter | ||
Verbosity int | ||
index 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) { | ||
if len(output) == 0 { | ||
return | ||
} | ||
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. | ||
// Returns the number of bytes reported. | ||
func (r *Reporter) Output(buffer []byte, delimited bool) (reported int) { | ||
if r.Filter == nil { | ||
r.Filter = func(in string) (out string) { | ||
out = in | ||
return | ||
} | ||
} | ||
switch r.Verbosity { | ||
case Disabled: | ||
case Error: | ||
case Default: | ||
case LiveOutput: | ||
if r.index >= len(buffer) { | ||
return | ||
} | ||
batch := string(buffer[r.index:]) | ||
if delimited { | ||
end := strings.LastIndex(batch, "\n") | ||
if end != -1 { | ||
batch = batch[:end] | ||
output := r.Filter(batch) | ||
addon.Activity("> %s", output) | ||
reported = len(output) | ||
r.index += len(batch) | ||
r.index++ | ||
} | ||
} else { | ||
output := r.Filter(batch) | ||
addon.Activity("> %s", output) | ||
reported = len(batch) | ||
r.index = len(buffer) | ||
} | ||
} | ||
return | ||
} |
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,99 @@ | ||
package command | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
const ( | ||
// Backoff rate increment. | ||
Backoff = time.Millisecond * 100 | ||
// MaxBackoff max backoff. | ||
MaxBackoff = 10 * Backoff | ||
// MinBackoff minimum backoff. | ||
MinBackoff = Backoff | ||
) | ||
|
||
// | ||
// OutputFilter filter output. | ||
type OutputFilter func(in []byte) (out []byte) | ||
|
||
// | ||
// Writer records command output. | ||
type Writer struct { | ||
Filter OutputFilter | ||
reporter *Reporter | ||
buffer []byte | ||
backoff time.Duration | ||
end chan any | ||
ended chan any | ||
} | ||
|
||
// | ||
// Write command output. | ||
func (w *Writer) Write(p []byte) (n int, err error) { | ||
if w.Filter == nil { | ||
w.Filter = func(in []byte) (out []byte) { | ||
out = in | ||
return | ||
} | ||
} | ||
n = len(p) | ||
p = w.Filter(p) | ||
w.buffer = append(w.buffer, p...) | ||
if w.ended == nil { | ||
w.end = make(chan any) | ||
w.ended = make(chan any) | ||
go w.report() | ||
} | ||
return | ||
} | ||
|
||
// | ||
// End of writing. | ||
func (w *Writer) End() { | ||
if w.end == nil { | ||
return | ||
} | ||
close(w.end) | ||
<-w.ended | ||
close(w.ended) | ||
w.end = nil | ||
} | ||
|
||
// | ||
// report in task Report.Activity. | ||
// Rate limited. | ||
func (w *Writer) report() { | ||
w.backoff = MinBackoff | ||
ended := false | ||
for { | ||
select { | ||
case <-w.end: | ||
ended = true | ||
case <-time.After(w.backoff): | ||
} | ||
n := w.reporter.Output(w.buffer, true) | ||
w.adjustBackoff(n) | ||
if ended { | ||
break | ||
} | ||
} | ||
w.reporter.Output(w.buffer, false) | ||
w.ended <- true | ||
} | ||
|
||
// | ||
// adjustBackoff adjust the backoff as needed. | ||
// incremented when output reported. | ||
// decremented when no outstanding output reported. | ||
func (w *Writer) adjustBackoff(reported int) { | ||
if reported > 0 { | ||
if w.backoff < MaxBackoff { | ||
w.backoff += Backoff | ||
} | ||
} else { | ||
if w.backoff > MinBackoff { | ||
w.backoff -= Backoff | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be
-1
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.