1
0
mirror of https://github.com/go-task/task.git synced 2025-02-03 13:22:11 +02:00

feat: make Taskfile initialization less verbose by default (#2011)

* 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.
This commit is contained in:
Henrique Corrêa 2025-01-29 19:41:17 -03:00 committed by GitHub
parent 7d4c52546a
commit 88c4ba1740
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 49 additions and 31 deletions

View File

@ -44,7 +44,7 @@ func main() {
}
func run() error {
logger := &logger.Logger{
log := &logger.Logger{
Stdout: os.Stdout,
Stderr: os.Stderr,
Verbose: flags.Verbose,
@ -69,7 +69,7 @@ func run() error {
}
if flags.Experiments {
return experiments.List(logger)
return experiments.List(log)
}
if flags.Init {
@ -77,9 +77,18 @@ func run() error {
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
}
@ -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

13
init.go
View File

@ -1,7 +1,6 @@
package task
import (
"fmt"
"io"
"os"
@ -9,7 +8,7 @@ import (
"github.com/go-task/task/v3/internal/filepathext"
)
const defaultTaskfile = `# https://taskfile.dev
const DefaultTaskfile = `# https://taskfile.dev
version: '3'
@ -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
init_test.go Normal file
View File

@ -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)
}

View File

@ -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()