Skip to content

Commit

Permalink
feat: make Taskfile initialization less verbose by default (#2011)
Browse files Browse the repository at this point in the history
* change what is printed when creating Taskfile

When using --init to create a new Taskfile, it used to print the whole contents of the file to the terminal, which was unnecessarily verbose (and honestly felt unintentional).

Now only the filename is printed by default and the --silent and --verbose flags can be used to control the behavior (print nothing or content + filename, respectively).

* include additional new line with -i -v

it looks slightly better in the terminal.

* print init success text in green

* fix TestInit, create and pass in a logger

* move logging outside of InitTaskfile

- revert API changes made to InitTaskfile
- make consts in init.go public so they can be accessed from task.go
- rename variable "logger" to "log" in task.go to fix conflict with logger package

* move TestInit into init_test.go file

as requested by pd93.
  • Loading branch information
HeCorr authored Jan 29, 2025
1 parent 7d4c525 commit 88c4ba1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 31 deletions.
15 changes: 12 additions & 3 deletions cmd/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func main() {
}

func run() error {
logger := &logger.Logger{
log := &logger.Logger{
Stdout: os.Stdout,
Stderr: os.Stderr,
Verbose: flags.Verbose,
Expand All @@ -69,17 +69,26 @@ func run() error {
}

if flags.Experiments {
return experiments.List(logger)
return experiments.List(log)
}

if flags.Init {
wd, err := os.Getwd()
if err != nil {
return err
}

if err := task.InitTaskfile(os.Stdout, wd); err != nil {
return err
}

if !flags.Silent {
if flags.Verbose {
log.Outf(logger.Default, "%s\n", task.DefaultTaskfile)
}
log.Outf(logger.Green, "%s created in the current directory\n", task.DefaultTaskFilename)
}

return nil
}

Expand Down Expand Up @@ -146,7 +155,7 @@ func run() error {
return err
}
if experiments.AnyVariables.Enabled {
logger.Warnf("The 'Any Variables' experiment flag is no longer required to use non-map variable types. If you wish to use map variables, please use 'TASK_X_MAP_VARIABLES' instead. See https://github.com/go-task/task/issues/1585\n")
log.Warnf("The 'Any Variables' experiment flag is no longer required to use non-map variable types. If you wish to use map variables, please use 'TASK_X_MAP_VARIABLES' instead. See https://github.com/go-task/task/issues/1585\n")
}

// If the download flag is specified, we should stop execution as soon as
Expand Down
13 changes: 6 additions & 7 deletions init.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package task

import (
"fmt"
"io"
"os"

"github.com/go-task/task/v3/errors"
"github.com/go-task/task/v3/internal/filepathext"
)

const defaultTaskfile = `# https://taskfile.dev
const DefaultTaskfile = `# https://taskfile.dev
version: '3'
Expand All @@ -23,19 +22,19 @@ tasks:
silent: true
`

const defaultTaskfileName = "Taskfile.yml"
const DefaultTaskFilename = "Taskfile.yml"

// InitTaskfile Taskfile creates a new Taskfile
// InitTaskfile creates a new Taskfile
func InitTaskfile(w io.Writer, dir string) error {
f := filepathext.SmartJoin(dir, defaultTaskfileName)
f := filepathext.SmartJoin(dir, DefaultTaskFilename)

if _, err := os.Stat(f); err == nil {
return errors.TaskfileAlreadyExistsError{}
}

if err := os.WriteFile(f, []byte(defaultTaskfile), 0o644); err != nil {
if err := os.WriteFile(f, []byte(DefaultTaskfile), 0o644); err != nil {
return err
}
fmt.Fprintf(w, "%s created in the current directory\n", defaultTaskfile)

return nil
}
31 changes: 31 additions & 0 deletions init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package task_test

import (
"io"
"os"
"testing"

"github.com/go-task/task/v3"
"github.com/go-task/task/v3/internal/filepathext"
)

func TestInit(t *testing.T) {
t.Parallel()

const dir = "testdata/init"
file := filepathext.SmartJoin(dir, "Taskfile.yml")

_ = os.Remove(file)
if _, err := os.Stat(file); err == nil {
t.Errorf("Taskfile.yml should not exist")
}

if err := task.InitTaskfile(io.Discard, dir); err != nil {
t.Error(err)
}

if _, err := os.Stat(file); err != nil {
t.Errorf("Taskfile.yml should exist")
}
_ = os.Remove(file)
}
21 changes: 0 additions & 21 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1011,27 +1011,6 @@ func TestCmdsVariables(t *testing.T) {
assert.Contains(t, buff.String(), tf)
}

func TestInit(t *testing.T) {
t.Parallel()

const dir = "testdata/init"
file := filepathext.SmartJoin(dir, "Taskfile.yml")

_ = os.Remove(file)
if _, err := os.Stat(file); err == nil {
t.Errorf("Taskfile.yml should not exist")
}

if err := task.InitTaskfile(io.Discard, dir); err != nil {
t.Error(err)
}

if _, err := os.Stat(file); err != nil {
t.Errorf("Taskfile.yml should exist")
}
_ = os.Remove(file)
}

func TestCyclicDep(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 88c4ba1

Please sign in to comment.