From 78f6cb08d8c32215c04d0e88f08f45846390e4a7 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Tue, 26 Dec 2017 21:43:52 -0200 Subject: [PATCH] Add --status flag to check is a task is up-to-date Closes #81 --- README.md | 3 +++ cmd/task/task.go | 9 +++++++++ status.go | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/README.md b/README.md index 4c2b588a..c3fe340c 100644 --- a/README.md +++ b/README.md @@ -300,6 +300,9 @@ generate-files: You can use `--force` or `-f` if you want to force a task to run even when up-to-date. +Also, `task --status [tasks]...` will exit with non-zero exit code if any of +the tasks is not up-to-date. + ### Variables When doing interpolation of variables, Task will look for the below. diff --git a/cmd/task/task.go b/cmd/task/task.go index 5f3dfbbd..f7a5afe9 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -50,6 +50,7 @@ func main() { versionFlag bool init bool list bool + status bool force bool watch bool verbose bool @@ -60,6 +61,7 @@ func main() { pflag.BoolVar(&versionFlag, "version", false, "show Task version") pflag.BoolVarP(&init, "init", "i", false, "creates a new Taskfile.yml in the current folder") pflag.BoolVarP(&list, "list", "l", false, "lists tasks with description of current Taskfile") + pflag.BoolVar(&status, "status", false, "exits with non-zero exit code if any of the given tasks is not up-to-date") pflag.BoolVarP(&force, "force", "f", false, "forces execution even when the task is up-to-date") pflag.BoolVarP(&watch, "watch", "w", false, "enables watch of the given task") pflag.BoolVarP(&verbose, "verbose", "v", false, "enables verbose mode") @@ -116,6 +118,13 @@ func main() { log.Fatal(err) } + if status { + if err = e.Status(calls...); err != nil { + log.Fatal(err) + } + return + } + if err := e.Run(calls...); err != nil { log.Fatal(err) } diff --git a/status.go b/status.go index 40f3db66..4742bb38 100644 --- a/status.go +++ b/status.go @@ -8,6 +8,24 @@ import ( "github.com/go-task/task/internal/status" ) +// Status returns an error if any the of given tasks is not up-to-date +func (e *Executor) Status(calls ...Call) error { + for _, call := range calls { + t, ok := e.Tasks[call.Task] + if !ok { + return &taskNotFoundError{taskName: call.Task} + } + isUpToDate, err := t.isUpToDate(e.Context) + if err != nil { + return err + } + if !isUpToDate { + return fmt.Errorf(`task: Task "%s" is not up-to-date`, t.Task) + } + } + return nil +} + func (t *Task) isUpToDate(ctx context.Context) (bool, error) { if len(t.Status) > 0 { return t.isUpToDateStatus(ctx)