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:
parent
7d4c52546a
commit
88c4ba1740
@ -44,7 +44,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func run() error {
|
func run() error {
|
||||||
logger := &logger.Logger{
|
log := &logger.Logger{
|
||||||
Stdout: os.Stdout,
|
Stdout: os.Stdout,
|
||||||
Stderr: os.Stderr,
|
Stderr: os.Stderr,
|
||||||
Verbose: flags.Verbose,
|
Verbose: flags.Verbose,
|
||||||
@ -69,7 +69,7 @@ func run() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if flags.Experiments {
|
if flags.Experiments {
|
||||||
return experiments.List(logger)
|
return experiments.List(log)
|
||||||
}
|
}
|
||||||
|
|
||||||
if flags.Init {
|
if flags.Init {
|
||||||
@ -77,9 +77,18 @@ func run() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := task.InitTaskfile(os.Stdout, wd); err != nil {
|
if err := task.InitTaskfile(os.Stdout, wd); err != nil {
|
||||||
return err
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,7 +155,7 @@ func run() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if experiments.AnyVariables.Enabled {
|
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
|
// If the download flag is specified, we should stop execution as soon as
|
||||||
|
13
init.go
13
init.go
@ -1,7 +1,6 @@
|
|||||||
package task
|
package task
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
@ -9,7 +8,7 @@ import (
|
|||||||
"github.com/go-task/task/v3/internal/filepathext"
|
"github.com/go-task/task/v3/internal/filepathext"
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultTaskfile = `# https://taskfile.dev
|
const DefaultTaskfile = `# https://taskfile.dev
|
||||||
|
|
||||||
version: '3'
|
version: '3'
|
||||||
|
|
||||||
@ -23,19 +22,19 @@ tasks:
|
|||||||
silent: true
|
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 {
|
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 {
|
if _, err := os.Stat(f); err == nil {
|
||||||
return errors.TaskfileAlreadyExistsError{}
|
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
|
return err
|
||||||
}
|
}
|
||||||
fmt.Fprintf(w, "%s created in the current directory\n", defaultTaskfile)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
31
init_test.go
Normal file
31
init_test.go
Normal 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)
|
||||||
|
}
|
21
task_test.go
21
task_test.go
@ -1011,27 +1011,6 @@ func TestCmdsVariables(t *testing.T) {
|
|||||||
assert.Contains(t, buff.String(), tf)
|
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) {
|
func TestCyclicDep(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user