Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/tsk/tsk.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Options struct {
init bool
listTasks bool
output string
prefix bool
pure bool
taskFile string
tasks []string
Expand All @@ -46,6 +47,7 @@ func main() {
flag.BoolVar(&opts.init, "init", false, "create a tasks.toml file in $PWD")
flag.BoolVarP(&opts.listTasks, "list", "l", false, "list tasks")
flag.StringVarP(&opts.output, "output", "o", "text", fmt.Sprintf("output format (applies only to --list) (one of: %s)", output.String()))
flag.BoolVar(&opts.prefix, "prefix", false, "prefix task output with the task name")
flag.BoolVarP(&opts.pure, "pure", "", false, "don't inherit the parent env")
flag.StringVarP(&opts.taskFile, "file", "f", "", "taskfile to use")
flag.BoolVar(&opts.which, "which", false, "print the path to the found tasks.toml, or an error")
Expand Down Expand Up @@ -91,6 +93,7 @@ func main() {
Stdin: os.Stdin,
Stderr: os.Stderr,
Config: cfg,
Prefix: opts.prefix,
}

if opts.listTasks {
Expand Down
43 changes: 37 additions & 6 deletions internal/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ type Task struct {
}

type Executor struct {
Stdout io.Writer
Stdin io.Reader
Stderr io.Writer
Config *Config
Stdout io.Writer
Stdin io.Reader
Stderr io.Writer
Config *Config
Prefix bool
colorIndex int
taskColors map[string]string
}

// sets the top-level env
Expand Down Expand Up @@ -90,6 +93,26 @@ func (t *Task) CompileEnv(env []string) ([]string, error) {
return env, nil
}

func (exec *Executor) colorForTask(task string) string {
if exec.taskColors == nil {
exec.taskColors = make(map[string]string)
}
if color, ok := exec.taskColors[task]; ok {
return color
}
color := prefixColors[exec.colorIndex%len(prefixColors)]
exec.colorIndex++
exec.taskColors[task] = color
return color
}

func (exec *Executor) prefixedWriter(w io.Writer, prefix string, task string) io.Writer {
if exec.Prefix {
return newPrefixWriter(w, prefix, exec.colorForTask(task))
}
return w
}

func (exec *Executor) RunTasks(config *Config, tasks *[]string) error {
// top-level env
env, err := config.CompileEnv()
Expand Down Expand Up @@ -133,10 +156,18 @@ func (exec *Executor) RunTasks(config *Config, tasks *[]string) error {
return err
}

taskExec := &Executor{
Stdout: exec.prefixedWriter(exec.Stdout, "::"+task+":: ", task),
Stderr: exec.prefixedWriter(exec.Stderr, "::"+task+":: ", task),
Stdin: exec.Stdin,
Config: exec.Config,
Prefix: exec.Prefix,
}

// if a task contains cmds, run them
if len(taskConfig.Cmds) > 0 {
for _, cmd := range taskConfig.Cmds {
err := exec.runCommand(cmd, taskConfig.Dir, env)
err := taskExec.runCommand(cmd, taskConfig.Dir, env)
if err != nil {
fmt.Println(err.Error())
// if the cmd exited with an error, bail immediately
Expand All @@ -146,7 +177,7 @@ func (exec *Executor) RunTasks(config *Config, tasks *[]string) error {
} else {
// if there are no cmds then we intend to run a script with the name name as the task
script := fmt.Sprintf("%s/%s", exec.Config.ScriptDir, task)
err := exec.runCommand(script, taskConfig.Dir, env)
err := taskExec.runCommand(script, taskConfig.Dir, env)
if err != nil {
fmt.Println(err.Error())
// if the cmd exited with an error, bail immediately
Expand Down
46 changes: 46 additions & 0 deletions internal/task/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,58 @@ package task
import (
"bytes"
"fmt"
"io"
"sort"
"text/template"

"github.com/joho/godotenv"
)

// ANSI color codes for prefix coloring. These are chosen for visual distinction.
var prefixColors = []string{
"\033[36m", // cyan
"\033[33m", // yellow
"\033[35m", // magenta
"\033[32m", // green
"\033[34m", // blue
"\033[31m", // red
"\033[96m", // bright cyan
"\033[93m", // bright yellow
"\033[95m", // bright magenta
"\033[92m", // bright green
"\033[94m", // bright blue
"\033[91m", // bright red
}

const colorReset = "\033[0m"

type prefixWriter struct {
writer io.Writer
prefix string
buf []byte
}

func newPrefixWriter(w io.Writer, prefix string, color string) *prefixWriter {
coloredPrefix := color + prefix + colorReset
return &prefixWriter{writer: w, prefix: coloredPrefix}
}

func (p *prefixWriter) Write(b []byte) (int, error) {
p.buf = append(p.buf, b...)
for {
idx := bytes.IndexByte(p.buf, '\n')
if idx < 0 {
break
}
line := p.buf[:idx+1]
if _, err := fmt.Fprintf(p.writer, "%s%s", p.prefix, line); err != nil {
return 0, err
}
p.buf = p.buf[idx+1:]
}
return len(b), nil
}

type Vals struct {
CLI_ARGS string
}
Expand Down
Loading