Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make Taskfile initialization less verbose by default #2011

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion cmd/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 18 additions & 3 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
}
4 changes: 3 additions & 1 deletion task_test.go
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this test wasn't originally created by you, but can we move TestInit into a new init_test.go file? I think we should keep the task_test.go for executor tests, not function tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to bed right now but I'll be happy to do so tomorrow.

Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Loading