diff --git a/cmd/task/task.go b/cmd/task/task.go index cf07a5be6a..1955739581 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -77,7 +77,15 @@ func run() error { if err != nil { return err } - if err := task.InitTaskfile(os.Stdout, wd); err != nil { + + verbosity := uint8(1) // Print filename only + if flags.Silent { + verbosity = 0 // Print nothing + } else if flags.Verbose { + verbosity = 2 // Print file contents + filename + } + + if err := task.InitTaskfile(os.Stdout, wd, verbosity, logger); err != nil { return err } return nil diff --git a/init.go b/init.go index 79d04348bc..763f1757a8 100644 --- a/init.go +++ b/init.go @@ -7,6 +7,7 @@ import ( "github.com/go-task/task/v3/errors" "github.com/go-task/task/v3/internal/filepathext" + "github.com/go-task/task/v3/internal/logger" ) const defaultTaskfile = `# https://taskfile.dev @@ -25,8 +26,16 @@ tasks: const defaultTaskfileName = "Taskfile.yml" -// InitTaskfile Taskfile creates a new Taskfile -func InitTaskfile(w io.Writer, dir string) error { +// InitTaskfile creates a new Taskfile +// +// verbosity specifies how much to print to the terminal: +// +// 0 = Don't print anything +// +// 1 = Print filename only +// +// 2 = Print file contents + filename +func InitTaskfile(w io.Writer, dir string, verbosity uint8, l *logger.Logger) error { f := filepathext.SmartJoin(dir, defaultTaskfileName) if _, err := os.Stat(f); err == nil { @@ -36,6 +45,12 @@ func InitTaskfile(w io.Writer, dir string) error { if err := os.WriteFile(f, []byte(defaultTaskfile), 0o644); err != nil { return err } - fmt.Fprintf(w, "%s created in the current directory\n", defaultTaskfile) + + if verbosity > 0 { + if verbosity > 1 { + fmt.Fprintf(w, "%s\n", defaultTaskfile) + } + l.Outf(logger.Green, "%s created in the current directory\n", defaultTaskfileName) + } return nil } diff --git a/task_test.go b/task_test.go index e1a1a918f9..1bdef5a640 100644 --- a/task_test.go +++ b/task_test.go @@ -1022,7 +1022,9 @@ func TestInit(t *testing.T) { t.Errorf("Taskfile.yml should not exist") } - if err := task.InitTaskfile(io.Discard, dir); err != nil { + l := &logger.Logger{Verbose: true} + + if err := task.InitTaskfile(io.Discard, dir, 0, l); err != nil { t.Error(err) }