mirror of
https://github.com/go-task/task.git
synced 2025-08-08 22:36:57 +02:00
@@ -7,7 +7,7 @@ import (
|
||||
|
||||
type Group struct{}
|
||||
|
||||
func (Group) WrapWriter(w io.Writer) io.WriteCloser {
|
||||
func (Group) WrapWriter(w io.Writer, _ string) io.WriteCloser {
|
||||
return &groupWriter{writer: w}
|
||||
}
|
||||
|
||||
|
@@ -6,7 +6,7 @@ import (
|
||||
|
||||
type Interleaved struct{}
|
||||
|
||||
func (Interleaved) WrapWriter(w io.Writer) io.WriteCloser {
|
||||
func (Interleaved) WrapWriter(w io.Writer, _ string) io.WriteCloser {
|
||||
return nopWriterCloser{w: w}
|
||||
}
|
||||
|
||||
|
@@ -5,5 +5,5 @@ import (
|
||||
)
|
||||
|
||||
type Output interface {
|
||||
WrapWriter(io.Writer) io.WriteCloser
|
||||
WrapWriter(w io.Writer, prefix string) io.WriteCloser
|
||||
}
|
||||
|
65
internal/output/prefixed.go
Normal file
65
internal/output/prefixed.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package output
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Prefixed struct{}
|
||||
|
||||
func (Prefixed) WrapWriter(w io.Writer, prefix string) io.WriteCloser {
|
||||
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 {
|
||||
line, err := pw.buff.ReadString('\n')
|
||||
if err == nil {
|
||||
if err = pw.writeLine(line); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if err == 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)
|
||||
} else {
|
||||
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
|
||||
}
|
@@ -17,4 +17,5 @@ type Task struct {
|
||||
Env Vars
|
||||
Silent bool
|
||||
Method string
|
||||
Prefix string
|
||||
}
|
||||
|
6
task.go
6
task.go
@@ -130,6 +130,8 @@ func (e *Executor) Setup() error {
|
||||
e.Output = output.Interleaved{}
|
||||
case "group":
|
||||
e.Output = output.Group{}
|
||||
case "prefixed":
|
||||
e.Output = output.Prefixed{}
|
||||
default:
|
||||
return fmt.Errorf(`task: output option "%s" not recognized`, e.Taskfile.Output)
|
||||
}
|
||||
@@ -204,8 +206,8 @@ func (e *Executor) runCommand(ctx context.Context, t *taskfile.Task, call taskfi
|
||||
e.Logger.Errf(cmd.Cmd)
|
||||
}
|
||||
|
||||
stdOut := e.Output.WrapWriter(e.Stdout)
|
||||
stdErr := e.Output.WrapWriter(e.Stderr)
|
||||
stdOut := e.Output.WrapWriter(e.Stdout, t.Prefix)
|
||||
stdErr := e.Output.WrapWriter(e.Stderr, t.Prefix)
|
||||
defer stdOut.Close()
|
||||
defer stdErr.Close()
|
||||
|
||||
|
@@ -39,6 +39,7 @@ func (e *Executor) CompiledTask(call taskfile.Call) (*taskfile.Task, error) {
|
||||
Env: r.ReplaceVars(origTask.Env),
|
||||
Silent: origTask.Silent,
|
||||
Method: r.Replace(origTask.Method),
|
||||
Prefix: r.Replace(origTask.Prefix),
|
||||
}
|
||||
new.Dir, err = homedir.Expand(new.Dir)
|
||||
if err != nil {
|
||||
@@ -47,6 +48,9 @@ func (e *Executor) CompiledTask(call taskfile.Call) (*taskfile.Task, error) {
|
||||
if e.Dir != "" && !filepath.IsAbs(new.Dir) {
|
||||
new.Dir = filepath.Join(e.Dir, new.Dir)
|
||||
}
|
||||
if new.Prefix == "" {
|
||||
new.Prefix = new.Task
|
||||
}
|
||||
for k, v := range new.Env {
|
||||
static, err := e.Compiler.HandleDynamicVar(v)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user