mirror of
https://github.com/go-task/task.git
synced 2024-12-14 10:52:43 +02:00
74f5cf8f29
Fixes #647 This allows CI systems that support grouping (such as with [GitHub Actions's `::group::` command](https://docs.github.com/en/actions/learn-github-actions/workflow-commands-for-github-actions#grouping-log-lines) and [Azure Devops](https://docs.microsoft.com/en-us/azure/devops/pipelines/scripts/logging-commands?view=azure-devops&tabs=bash#formatting-commands)) to collapse all of the logs for a single task, to improve readability of logs ## Example The following Taskfile ``` # Taskfile.yml version: 3 output: group: begin: "::group::{{ .TASK }}" end: "::endgroup::" tasks: default: cmds: - "echo 'Hello, World!'" ``` Results in the following output ```bash $ task task: [default] echo 'Hello, World!' ::group::default Hello, World! ::endgroup:: ``` See [this GitHub Actions job](https://github.com/janslow/task/runs/4811059609?check_suite_focus=true) for a full example <img width="771" alt="image" src="https://user-images.githubusercontent.com/1253367/149429832-6cb0c1b5-0758-442e-9375-c4daa65771bc.png"> <img width="394" alt="image" src="https://user-images.githubusercontent.com/1253367/149429851-1d5d2ab5-9095-4795-9b57-f91750720d40.png">
66 lines
1.2 KiB
Go
66 lines
1.2 KiB
Go
package output
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
)
|
|
|
|
type Prefixed struct{}
|
|
|
|
func (Prefixed) WrapWriter(w io.Writer, prefix string, _ Templater) io.Writer {
|
|
return &prefixWriter{writer: w, prefix: prefix}
|
|
}
|
|
|
|
type prefixWriter struct {
|
|
writer io.Writer
|
|
prefix string
|
|
buff bytes.Buffer
|
|
}
|
|
|
|
func (pw *prefixWriter) Write(p []byte) (int, error) {
|
|
n, err := pw.buff.Write(p)
|
|
if err != nil {
|
|
return n, err
|
|
}
|
|
|
|
return n, pw.writeOutputLines(false)
|
|
}
|
|
|
|
func (pw *prefixWriter) Close() error {
|
|
return pw.writeOutputLines(true)
|
|
}
|
|
|
|
func (pw *prefixWriter) writeOutputLines(force bool) error {
|
|
for {
|
|
switch line, err := pw.buff.ReadString('\n'); err {
|
|
case nil:
|
|
if err = pw.writeLine(line); err != nil {
|
|
return err
|
|
}
|
|
case io.EOF:
|
|
// if this line was not a complete line, re-add to the buffer
|
|
if !force && !strings.HasSuffix(line, "\n") {
|
|
_, err = pw.buff.WriteString(line)
|
|
return err
|
|
}
|
|
|
|
return pw.writeLine(line)
|
|
default:
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
func (pw *prefixWriter) writeLine(line string) error {
|
|
if line == "" {
|
|
return nil
|
|
}
|
|
if !strings.HasSuffix(line, "\n") {
|
|
line += "\n"
|
|
}
|
|
_, err := fmt.Fprintf(pw.writer, "[%s] %s", pw.prefix, line)
|
|
return err
|
|
}
|