1
0
mirror of https://github.com/go-task/task.git synced 2025-02-09 13:47:06 +02:00

allow custom Stdin, Stdout and Stderr while running as a lib

This commit is contained in:
Andrey Nering 2017-07-01 15:05:51 -03:00
parent 03fd5c84ec
commit 9ba44f3e6e
6 changed files with 46 additions and 17 deletions

View File

@ -3,6 +3,7 @@ package main
import (
"fmt"
"log"
"os"
"github.com/go-task/task"
@ -61,6 +62,10 @@ hello:
e := task.Executor{
Force: force,
Watch: watch,
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
if err := e.ReadTaskfile(); err != nil {
log.Fatal(err)

View File

@ -2,7 +2,6 @@ package task
import (
"fmt"
"os"
"sort"
"text/tabwriter"
)
@ -12,10 +11,10 @@ func (e *Executor) printExistingTasksHelp() {
if len(tasks) == 0 {
return
}
fmt.Println("Available tasks for this project:")
e.println("Available tasks for this project:")
// Format in tab-separated columns with a tab stop of 8.
w := tabwriter.NewWriter(os.Stdout, 0, 8, 0, '\t', 0)
w := tabwriter.NewWriter(e.Stdout, 0, 8, 0, '\t', 0)
for _, task := range tasks {
fmt.Fprintln(w, fmt.Sprintf("- %s:\t%s", task, e.Tasks[task].Desc))
}

13
log.go Normal file
View File

@ -0,0 +1,13 @@
package task
import (
"fmt"
)
func (e *Executor) println(args ...interface{}) {
fmt.Fprintln(e.Stdout, args...)
}
func (e *Executor) printfln(format string, args ...interface{}) {
fmt.Fprintf(e.Stdout, format+"\n", args...)
}

26
task.go
View File

@ -4,7 +4,7 @@ import (
"bytes"
"context"
"fmt"
"log"
"io"
"os"
"path/filepath"
"strings"
@ -26,6 +26,10 @@ type Executor struct {
Force bool
Watch bool
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
watchingFiles map[string]struct{}
}
@ -52,6 +56,16 @@ func (e *Executor) Run(args ...string) error {
return ErrCyclicDependencyDetected
}
if e.Stdin == nil {
e.Stdin = os.Stdin
}
if e.Stdout == nil {
e.Stdout = os.Stdout
}
if e.Stderr == nil {
e.Stderr = os.Stderr
}
// check if given tasks exist
for _, a := range args {
if _, ok := e.Tasks[a]; !ok {
@ -93,7 +107,7 @@ func (e *Executor) RunTask(ctx context.Context, name string) error {
return err
}
if upToDate {
log.Printf(`task: Task "%s" is up to date`, name)
e.printfln(`task: Task "%s" is up to date`, name)
return nil
}
}
@ -231,13 +245,13 @@ func (e *Executor) runCommand(ctx context.Context, task string, i int) error {
Command: c,
Dir: dir,
Env: envs,
Stdin: os.Stdin,
Stderr: os.Stderr,
Stdin: e.Stdin,
Stderr: e.Stderr,
}
if t.Set == "" {
log.Println(c)
opts.Stdout = os.Stdout
e.println(c)
opts.Stdout = e.Stdout
if err = execext.RunCommand(opts); err != nil {
return err
}

View File

@ -36,7 +36,7 @@ func (e *Executor) handleDynamicVariableContent(value string) (string, error) {
Command: strings.TrimPrefix(value, "$"),
Dir: e.Dir,
Stdout: buff,
Stderr: os.Stderr,
Stderr: e.Stderr,
}
if err := execext.RunCommand(opts); err != nil {
return "", err

View File

@ -2,8 +2,6 @@ package task
import (
"context"
"fmt"
"log"
"strings"
"time"
@ -13,12 +11,12 @@ import (
// watchTasks start watching the given tasks
func (e *Executor) watchTasks(args ...string) error {
log.Printf("task: Started watching for tasks: %s", strings.Join(args, ", "))
e.printfln("task: Started watching for tasks: %s", strings.Join(args, ", "))
// run tasks on init
for _, a := range args {
if err := e.RunTask(context.Background(), a); err != nil {
fmt.Println(err)
e.println(err)
break
}
}
@ -32,7 +30,7 @@ func (e *Executor) watchTasks(args ...string) error {
go func() {
for {
if err := e.registerWatchedFiles(watcher, args); err != nil {
log.Printf("Error watching files: %v", err)
e.printfln("Error watching files: %v", err)
}
time.Sleep(time.Second * 2)
}
@ -44,12 +42,12 @@ loop:
case <-watcher.Events:
for _, a := range args {
if err := e.RunTask(context.Background(), a); err != nil {
fmt.Println(err)
e.println(err)
continue loop
}
}
case err := <-watcher.Errors:
fmt.Println(err)
e.println(err)
continue loop
}
}