1
0
mirror of https://github.com/go-task/task.git synced 2024-12-12 10:45:49 +02:00

Add --init flag to create a new Taskfile

This commit is contained in:
Andrey Nering 2017-05-17 15:38:46 -03:00
parent 83f1b213fa
commit 2615000609
6 changed files with 59 additions and 1 deletions

View File

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

View File

@ -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 {

29
init.go Normal file
View File

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

View File

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

View File

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

1
testdata/init/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.yml