From e4c1cc3e7765d25c4f7cedd0544405872ba37241 Mon Sep 17 00:00:00 2001 From: Rene Zbinden Date: Mon, 6 Mar 2017 22:25:50 +0100 Subject: [PATCH 1/3] add help task Signed-off-by: Andrey Nering --- README.md | 31 +++++++++++++++++++++++++++++++ task.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/README.md b/README.md index db63cf6a..dce10a8f 100644 --- a/README.md +++ b/README.md @@ -268,6 +268,37 @@ printos: - echo 'Is SH? {{IsSH}}' ``` +### Help + +Running `task help` lists all tasks with a description. The following taskfile: + +```yml +build: + desc: Build the go binary. + cmds: + - go build -v -i main.go + +test: + desc: Run all the go tests. + cmds: + - go test -race ./... + +js: + cmds: + - npm run buildjs + +css: + cmds: + - npm run buildcss +``` + +would print the following output: + +```bash +build Build the go binary. +test Run all the go tests. +``` + ## Alternatives Similar software: diff --git a/task.go b/task.go index f0df8923..74282988 100644 --- a/task.go +++ b/task.go @@ -4,8 +4,10 @@ import ( "fmt" "log" "os" + "sort" "strings" "sync" + "text/tabwriter" "github.com/go-task/task/execext" @@ -30,6 +32,7 @@ var ( type Task struct { Cmds []string Deps []string + Desc string Sources []string Generates []string Dir string @@ -73,6 +76,11 @@ func RunTask(name string) error { t, ok := Tasks[name] if !ok { + tasks := tasksWithDesc() + if len(tasks) > 0 { + help(tasks) + return nil + } return &taskNotFoundError{name} } @@ -202,3 +210,24 @@ func (t *Task) runCommand(i int) error { } return nil } + +func help(tasks []string) { + w := new(tabwriter.Writer) + // Format in tab-separated columns with a tab stop of 8. + w.Init(os.Stdout, 0, 8, 0, '\t', 0) + for _, task := range tasks { + fmt.Fprintln(w, fmt.Sprintf("%s\t%s", task, Tasks[task].Desc)) + } + w.Flush() +} + +func tasksWithDesc() []string { + tasks := []string{} + for name, task := range Tasks { + if len(task.Desc) > 0 { + tasks = append(tasks, name) + } + } + sort.Strings(tasks) + return tasks +} From 2054a1bc34dd17af9e237af84b11311bfe0df7f3 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Sun, 19 Mar 2017 15:18:18 -0300 Subject: [PATCH 2/3] Minor improvements for tasks description --- help.go | 33 +++++++++++++++++++++++++++++++++ task.go | 38 ++++++++++---------------------------- 2 files changed, 43 insertions(+), 28 deletions(-) create mode 100644 help.go diff --git a/help.go b/help.go new file mode 100644 index 00000000..3f085dbc --- /dev/null +++ b/help.go @@ -0,0 +1,33 @@ +package task + +import ( + "fmt" + "os" + "sort" + "text/tabwriter" +) + +func printExistingTasksHelp() { + tasks := tasksWithDesc() + if len(tasks) == 0 { + return + } + fmt.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) + for _, task := range tasks { + fmt.Fprintln(w, fmt.Sprintf("- %s:\t%s", task, Tasks[task].Desc)) + } + w.Flush() +} + +func tasksWithDesc() (tasks []string) { + for name, task := range Tasks { + if task.Desc != "" { + tasks = append(tasks, name) + } + } + sort.Strings(tasks) + return +} diff --git a/task.go b/task.go index 74282988..984a5531 100644 --- a/task.go +++ b/task.go @@ -4,10 +4,8 @@ import ( "fmt" "log" "os" - "sort" "strings" "sync" - "text/tabwriter" "github.com/go-task/task/execext" @@ -57,6 +55,16 @@ func Run() { log.Fatal(err) } + // check if given tasks exist + for _, a := range args { + if _, ok := Tasks[a]; !ok { + var err error = &taskNotFoundError{taskName: a} + fmt.Println(err) + printExistingTasksHelp() + return + } + } + for _, a := range args { if err = RunTask(a); err != nil { log.Fatal(err) @@ -76,11 +84,6 @@ func RunTask(name string) error { t, ok := Tasks[name] if !ok { - tasks := tasksWithDesc() - if len(tasks) > 0 { - help(tasks) - return nil - } return &taskNotFoundError{name} } @@ -210,24 +213,3 @@ func (t *Task) runCommand(i int) error { } return nil } - -func help(tasks []string) { - w := new(tabwriter.Writer) - // Format in tab-separated columns with a tab stop of 8. - w.Init(os.Stdout, 0, 8, 0, '\t', 0) - for _, task := range tasks { - fmt.Fprintln(w, fmt.Sprintf("%s\t%s", task, Tasks[task].Desc)) - } - w.Flush() -} - -func tasksWithDesc() []string { - tasks := []string{} - for name, task := range Tasks { - if len(task.Desc) > 0 { - tasks = append(tasks, name) - } - } - sort.Strings(tasks) - return tasks -} From 1cabfc636fca6bac1ceb41fd6c2a4c5551579044 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Sun, 19 Mar 2017 15:18:59 -0300 Subject: [PATCH 3/3] Add descs to Taskfile --- Taskfile.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Taskfile.yml b/Taskfile.yml index 70c45244..816f138a 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -1,20 +1,24 @@ # compiles current source code and make "task" executable available on # $GOPATH/bin/task{.exe} install: + desc: Installs Task cmds: - go install -v ./... lint: + desc: Runs golint cmds: - golint . - golint ./cmd/task test: + desc: Runs test suite deps: [install] cmds: - go test -v # https://github.com/goreleaser/goreleaser release: + desc: Release Task cmds: - goreleaser