From d0b37df615e60d0551bf3979ae89ab7503dfa711 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Wed, 19 Jul 2017 20:20:24 -0300 Subject: [PATCH] add silent mode to disable echoing of commands --- README.md | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ cmd/task/task.go | 5 ++++- command.go | 16 +++++++++++--- task.go | 6 +++++- variables.go | 8 ++++--- 5 files changed, 82 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index b5f8e31d..85b8ecf4 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ It aims to be simpler and easier to use then [GNU Make][make]. - [Dynamic variables](#dynamic-variables) - [Go's template engine](#gos-template-engine) - [Help](#help) + - [Silent mode](#silent-mode) - [Watch tasks](#watch-tasks-experimental) - [Alternative task runners](#alternative-task-runners) @@ -421,6 +422,60 @@ would print the following output: * test: Run all the go tests. ``` +## Silent mode + +Silent mode disables echoing of commands before Task runs it. +For the following Taskfile: + +```yml +echo: + cmds: + - echo "Print something" +``` + +Normally this will be print: + +```sh +echo "Print something" +Print something +``` + +With silent mode on, the below will be print instead: + +```sh +Print something +``` + +There's three ways to enable silent mode: + +* At command level: + +```yml +echo: + cmds: + - cmd: echo "Print something" + silent: true +``` + +* At task level: + +```yml +echo: + cmds: + - echo "Print something" + silent: true +``` + +* Or globally with `--silent` or `-s` flag + +If you want to supress stdout instead, just redirect a command to `/dev/null`: + +```yml +echo: + cmds: + - echo "This will print nothing" > /dev/null +``` + ## Watch tasks (experimental) If you give a `--watch` or `-w` argument, task will watch for files changes diff --git a/cmd/task/task.go b/cmd/task/task.go index 4d6cdd15..461aa819 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -14,7 +14,7 @@ var ( version = "master" ) -const usage = `Usage: task [-ilfwv] [--init] [--list] [--force] [--watch] [--verbose] [task...] +const usage = `Usage: task [-ilfwvs] [--init] [--list] [--force] [--watch] [--verbose] [--silent] [task...] Runs the specified task(s). Falls back to the "default" task if no task name was specified, or lists all tasks if an unknown task name was specified. @@ -49,6 +49,7 @@ func main() { force bool watch bool verbose bool + silent bool ) pflag.BoolVar(&versionFlag, "version", false, "show Task version") @@ -57,6 +58,7 @@ func main() { 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") + pflag.BoolVarP(&silent, "silent", "s", false, "disables echoing") pflag.Parse() if versionFlag { @@ -79,6 +81,7 @@ func main() { Force: force, Watch: watch, Verbose: verbose, + Silent: silent, Stdin: os.Stdin, Stdout: os.Stdout, diff --git a/command.go b/command.go index 3d369fd4..c14c94ba 100644 --- a/command.go +++ b/command.go @@ -7,9 +7,10 @@ import ( // Cmd is a task command type Cmd struct { - Cmd string - Task string - Vars Vars + Cmd string + Silent bool + Task string + Vars Vars } // Dep is a task dependency @@ -36,6 +37,15 @@ func (c *Cmd) UnmarshalYAML(unmarshal func(interface{}) error) error { } return nil } + var cmdStruct struct { + Cmd string + Silent bool + } + if err := unmarshal(&cmdStruct); err == nil && cmdStruct.Cmd != "" { + c.Cmd = cmdStruct.Cmd + c.Silent = cmdStruct.Silent + return nil + } var taskCall struct { Task string Vars Vars diff --git a/task.go b/task.go index dda71137..477f42f1 100644 --- a/task.go +++ b/task.go @@ -31,6 +31,7 @@ type Executor struct { Force bool Watch bool Verbose bool + Silent bool Stdin io.Reader Stdout io.Writer @@ -60,6 +61,7 @@ type Task struct { Vars Vars Set string Env Vars + Silent bool } // Run runs Task @@ -194,7 +196,9 @@ func (e *Executor) runCommand(ctx context.Context, t *Task, call Call, i int) er Stderr: e.Stderr, } - e.println(cmd.Cmd) + if !cmd.Silent && !t.Silent && !e.Silent { + e.println(cmd.Cmd) + } if t.Set != "" { var stdout bytes.Buffer opts.Stdout = &stdout diff --git a/variables.go b/variables.go index 013c1f3e..6ff322fc 100644 --- a/variables.go +++ b/variables.go @@ -185,15 +185,17 @@ func (t *Task) ReplaceVariables(vars Vars) (*Task, error) { Vars: r.replaceVars(t.Vars), Set: r.replace(t.Set), Env: r.replaceVars(t.Env), + Silent: t.Silent, } if len(t.Cmds) > 0 { new.Cmds = make([]*Cmd, len(t.Cmds)) for i, cmd := range t.Cmds { new.Cmds[i] = &Cmd{ - Task: r.replace(cmd.Task), - Cmd: r.replace(cmd.Cmd), - Vars: r.replaceVars(cmd.Vars), + Task: r.replace(cmd.Task), + Silent: cmd.Silent, + Cmd: r.replace(cmd.Cmd), + Vars: r.replaceVars(cmd.Vars), } }