mirror of
https://github.com/go-task/task.git
synced 2025-08-10 22:42:19 +02:00
Add "output" option to Taskfile to control how stuff are printed to stdout/stderr
First step for #104
This commit is contained in:
26
internal/output/group.go
Normal file
26
internal/output/group.go
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package output
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Group struct{}
|
||||||
|
|
||||||
|
func (Group) WrapWriter(w io.Writer) io.WriteCloser {
|
||||||
|
return &groupWriter{writer: w}
|
||||||
|
}
|
||||||
|
|
||||||
|
type groupWriter struct {
|
||||||
|
writer io.Writer
|
||||||
|
buff bytes.Buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (gw *groupWriter) Write(p []byte) (int, error) {
|
||||||
|
return gw.buff.Write(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (gw *groupWriter) Close() error {
|
||||||
|
_, err := io.Copy(gw.writer, &gw.buff)
|
||||||
|
return err
|
||||||
|
}
|
23
internal/output/interleaved.go
Normal file
23
internal/output/interleaved.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package output
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Interleaved struct{}
|
||||||
|
|
||||||
|
func (Interleaved) WrapWriter(w io.Writer) io.WriteCloser {
|
||||||
|
return nopWriterCloser{w: w}
|
||||||
|
}
|
||||||
|
|
||||||
|
type nopWriterCloser struct {
|
||||||
|
w io.Writer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wc nopWriterCloser) Write(p []byte) (int, error) {
|
||||||
|
return wc.w.Write(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wc nopWriterCloser) Close() error {
|
||||||
|
return nil
|
||||||
|
}
|
9
internal/output/output.go
Normal file
9
internal/output/output.go
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package output
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Output interface {
|
||||||
|
WrapWriter(io.Writer) io.WriteCloser
|
||||||
|
}
|
20
task.go
20
task.go
@@ -12,6 +12,7 @@ import (
|
|||||||
compilerv2 "github.com/go-task/task/internal/compiler/v2"
|
compilerv2 "github.com/go-task/task/internal/compiler/v2"
|
||||||
"github.com/go-task/task/internal/execext"
|
"github.com/go-task/task/internal/execext"
|
||||||
"github.com/go-task/task/internal/logger"
|
"github.com/go-task/task/internal/logger"
|
||||||
|
"github.com/go-task/task/internal/output"
|
||||||
"github.com/go-task/task/internal/taskfile"
|
"github.com/go-task/task/internal/taskfile"
|
||||||
"github.com/go-task/task/internal/taskfile/version"
|
"github.com/go-task/task/internal/taskfile/version"
|
||||||
|
|
||||||
@@ -44,6 +45,7 @@ type Executor struct {
|
|||||||
|
|
||||||
Logger *logger.Logger
|
Logger *logger.Logger
|
||||||
Compiler compiler.Compiler
|
Compiler compiler.Compiler
|
||||||
|
Output output.Output
|
||||||
|
|
||||||
taskvars taskfile.Vars
|
taskvars taskfile.Vars
|
||||||
|
|
||||||
@@ -119,9 +121,18 @@ func (e *Executor) Setup() error {
|
|||||||
case version.IsV22(v):
|
case version.IsV22(v):
|
||||||
return fmt.Errorf(`task: Taskfile versions greater than v2.1 not implemented in the version of Task`)
|
return fmt.Errorf(`task: Taskfile versions greater than v2.1 not implemented in the version of Task`)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !version.IsV21(v) && e.Taskfile.Output != "" {
|
if !version.IsV21(v) && e.Taskfile.Output != "" {
|
||||||
return fmt.Errorf(`task: Taskfile option "output" is only available starting on Taskfile version v2.1`)
|
return fmt.Errorf(`task: Taskfile option "output" is only available starting on Taskfile version v2.1`)
|
||||||
}
|
}
|
||||||
|
switch e.Taskfile.Output {
|
||||||
|
case "", "interleaved":
|
||||||
|
e.Output = output.Interleaved{}
|
||||||
|
case "group":
|
||||||
|
e.Output = output.Group{}
|
||||||
|
default:
|
||||||
|
return fmt.Errorf(`task: output option "%s" not recognized`, e.Taskfile.Output)
|
||||||
|
}
|
||||||
|
|
||||||
e.taskCallCount = make(map[string]*int32, len(e.Taskfile.Tasks))
|
e.taskCallCount = make(map[string]*int32, len(e.Taskfile.Tasks))
|
||||||
for k := range e.Taskfile.Tasks {
|
for k := range e.Taskfile.Tasks {
|
||||||
@@ -193,14 +204,19 @@ func (e *Executor) runCommand(ctx context.Context, t *taskfile.Task, call taskfi
|
|||||||
e.Logger.Errf(cmd.Cmd)
|
e.Logger.Errf(cmd.Cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stdOut := e.Output.WrapWriter(e.Stdout)
|
||||||
|
stdErr := e.Output.WrapWriter(e.Stderr)
|
||||||
|
defer stdOut.Close()
|
||||||
|
defer stdErr.Close()
|
||||||
|
|
||||||
return execext.RunCommand(&execext.RunCommandOptions{
|
return execext.RunCommand(&execext.RunCommandOptions{
|
||||||
Context: ctx,
|
Context: ctx,
|
||||||
Command: cmd.Cmd,
|
Command: cmd.Cmd,
|
||||||
Dir: t.Dir,
|
Dir: t.Dir,
|
||||||
Env: getEnviron(t),
|
Env: getEnviron(t),
|
||||||
Stdin: e.Stdin,
|
Stdin: e.Stdin,
|
||||||
Stdout: e.Stdout,
|
Stdout: stdOut,
|
||||||
Stderr: e.Stderr,
|
Stderr: stdErr,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user