diff --git a/cmd/task/task.go b/cmd/task/task.go index c9f069e7..70fe9f01 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -25,6 +25,7 @@ hello: `) pflag.PrintDefaults() } + pflag.BoolVarP(&task.Init, "init", "i", false, "creates a new Taskfile.yml in the current folder") pflag.BoolVarP(&task.Force, "force", "f", false, "forces execution even when the task is up-to-date") pflag.BoolVarP(&task.Watch, "watch", "w", false, "enables watch of the given task") pflag.Parse() diff --git a/errors.go b/errors.go index 4b6c2dc1..11945dc4 100644 --- a/errors.go +++ b/errors.go @@ -9,7 +9,7 @@ type taskFileNotFound struct { } func (err taskFileNotFound) Error() string { - return fmt.Sprintf(`task: No task file found (is it named "%s"?)`, err.taskFile) + return fmt.Sprintf(`task: No task file found (is it named "%s"?). Use "task --init" to create a new one`, err.taskFile) } type taskNotFoundError struct { diff --git a/init.go b/init.go new file mode 100644 index 00000000..2d015e4e --- /dev/null +++ b/init.go @@ -0,0 +1,29 @@ +package task + +import ( + "io/ioutil" + "log" + "os" +) + +const defaultTaskfile = `# github.com/go-task/task + +default: + cmds: + - echo "Hello, World!" +` + +func initTaskfile() { + for _, f := range []string{"Taskfile.yml", "Taskfile.toml", "Taskfile.json"} { + if _, err := os.Stat(f); err == nil { + log.Printf("A Taskfile already exists") + os.Exit(1) + return + } + } + + if err := ioutil.WriteFile("Taskfile.yml", []byte(defaultTaskfile), 0666); err != nil { + log.Fatalf("%v", err) + } + log.Printf("Taskfile.yml created in the current directory") +} diff --git a/task.go b/task.go index 497a4231..12cbadec 100644 --- a/task.go +++ b/task.go @@ -18,6 +18,8 @@ var ( // TaskFilePath is the default Taskfile TaskFilePath = "Taskfile" + // Init (--init or -f flag) creates a Taskfile.yml in the current folder if not exists + Init bool // Force (--force or -f flag) forces a task to run even when it's up-to-date Force bool // Watch (--watch or -w flag) enables watch of a task @@ -46,6 +48,12 @@ func Run() { log.SetFlags(0) args := pflag.Args() + + if Init { + initTaskfile() + return + } + if len(args) == 0 { log.Println("task: No argument given, trying default task") args = []string{"default"} diff --git a/task_test.go b/task_test.go index 6aa562d4..586893a1 100644 --- a/task_test.go +++ b/task_test.go @@ -144,3 +144,22 @@ func TestStatus(t *testing.T) { t.Errorf("Wrong output message: %s", buff.String()) } } + +func TestInit(t *testing.T) { + const dir = "testdata/init" + var file = filepath.Join(dir, "Taskfile.yml") + + _ = os.Remove(file) + if _, err := os.Stat(file); err == nil { + t.Errorf("Taskfile.yml should not exists") + } + + c := exec.Command("task", "--init") + c.Dir = dir + if err := c.Run(); err != nil { + t.Error(err) + } + if _, err := os.Stat(file); err != nil { + t.Errorf("Taskfile.yml should exists") + } +} diff --git a/testdata/init/.gitignore b/testdata/init/.gitignore new file mode 100644 index 00000000..1cda54be --- /dev/null +++ b/testdata/init/.gitignore @@ -0,0 +1 @@ +*.yml