1
0
mirror of https://github.com/go-task/task.git synced 2025-07-17 01:43:07 +02:00

add silent mode to disable echoing of commands

This commit is contained in:
Andrey Nering
2017-07-19 20:20:24 -03:00
parent dc6cb68327
commit d0b37df615
5 changed files with 82 additions and 8 deletions

View File

@ -19,6 +19,7 @@ It aims to be simpler and easier to use then [GNU Make][make].
- [Dynamic variables](#dynamic-variables) - [Dynamic variables](#dynamic-variables)
- [Go's template engine](#gos-template-engine) - [Go's template engine](#gos-template-engine)
- [Help](#help) - [Help](#help)
- [Silent mode](#silent-mode)
- [Watch tasks](#watch-tasks-experimental) - [Watch tasks](#watch-tasks-experimental)
- [Alternative task runners](#alternative-task-runners) - [Alternative task runners](#alternative-task-runners)
@ -421,6 +422,60 @@ would print the following output:
* test: Run all the go tests. * 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) ## Watch tasks (experimental)
If you give a `--watch` or `-w` argument, task will watch for files changes If you give a `--watch` or `-w` argument, task will watch for files changes

View File

@ -14,7 +14,7 @@ var (
version = "master" 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 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. was specified, or lists all tasks if an unknown task name was specified.
@ -49,6 +49,7 @@ func main() {
force bool force bool
watch bool watch bool
verbose bool verbose bool
silent bool
) )
pflag.BoolVar(&versionFlag, "version", false, "show Task version") 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(&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(&watch, "watch", "w", false, "enables watch of the given task")
pflag.BoolVarP(&verbose, "verbose", "v", false, "enables verbose mode") pflag.BoolVarP(&verbose, "verbose", "v", false, "enables verbose mode")
pflag.BoolVarP(&silent, "silent", "s", false, "disables echoing")
pflag.Parse() pflag.Parse()
if versionFlag { if versionFlag {
@ -79,6 +81,7 @@ func main() {
Force: force, Force: force,
Watch: watch, Watch: watch,
Verbose: verbose, Verbose: verbose,
Silent: silent,
Stdin: os.Stdin, Stdin: os.Stdin,
Stdout: os.Stdout, Stdout: os.Stdout,

View File

@ -7,9 +7,10 @@ import (
// Cmd is a task command // Cmd is a task command
type Cmd struct { type Cmd struct {
Cmd string Cmd string
Task string Silent bool
Vars Vars Task string
Vars Vars
} }
// Dep is a task dependency // Dep is a task dependency
@ -36,6 +37,15 @@ func (c *Cmd) UnmarshalYAML(unmarshal func(interface{}) error) error {
} }
return nil 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 { var taskCall struct {
Task string Task string
Vars Vars Vars Vars

View File

@ -31,6 +31,7 @@ type Executor struct {
Force bool Force bool
Watch bool Watch bool
Verbose bool Verbose bool
Silent bool
Stdin io.Reader Stdin io.Reader
Stdout io.Writer Stdout io.Writer
@ -60,6 +61,7 @@ type Task struct {
Vars Vars Vars Vars
Set string Set string
Env Vars Env Vars
Silent bool
} }
// Run runs Task // Run runs Task
@ -194,7 +196,9 @@ func (e *Executor) runCommand(ctx context.Context, t *Task, call Call, i int) er
Stderr: e.Stderr, Stderr: e.Stderr,
} }
e.println(cmd.Cmd) if !cmd.Silent && !t.Silent && !e.Silent {
e.println(cmd.Cmd)
}
if t.Set != "" { if t.Set != "" {
var stdout bytes.Buffer var stdout bytes.Buffer
opts.Stdout = &stdout opts.Stdout = &stdout

View File

@ -185,15 +185,17 @@ func (t *Task) ReplaceVariables(vars Vars) (*Task, error) {
Vars: r.replaceVars(t.Vars), Vars: r.replaceVars(t.Vars),
Set: r.replace(t.Set), Set: r.replace(t.Set),
Env: r.replaceVars(t.Env), Env: r.replaceVars(t.Env),
Silent: t.Silent,
} }
if len(t.Cmds) > 0 { if len(t.Cmds) > 0 {
new.Cmds = make([]*Cmd, len(t.Cmds)) new.Cmds = make([]*Cmd, len(t.Cmds))
for i, cmd := range t.Cmds { for i, cmd := range t.Cmds {
new.Cmds[i] = &Cmd{ new.Cmds[i] = &Cmd{
Task: r.replace(cmd.Task), Task: r.replace(cmd.Task),
Cmd: r.replace(cmd.Cmd), Silent: cmd.Silent,
Vars: r.replaceVars(cmd.Vars), Cmd: r.replace(cmd.Cmd),
Vars: r.replaceVars(cmd.Vars),
} }
} }