Skip to content

Commit 2615000

Browse files
committed
Add --init flag to create a new Taskfile
1 parent 83f1b21 commit 2615000

File tree

6 files changed

+59
-1
lines changed

6 files changed

+59
-1
lines changed

cmd/task/task.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ hello:
2525
`)
2626
pflag.PrintDefaults()
2727
}
28+
pflag.BoolVarP(&task.Init, "init", "i", false, "creates a new Taskfile.yml in the current folder")
2829
pflag.BoolVarP(&task.Force, "force", "f", false, "forces execution even when the task is up-to-date")
2930
pflag.BoolVarP(&task.Watch, "watch", "w", false, "enables watch of the given task")
3031
pflag.Parse()

errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type taskFileNotFound struct {
99
}
1010

1111
func (err taskFileNotFound) Error() string {
12-
return fmt.Sprintf(`task: No task file found (is it named "%s"?)`, err.taskFile)
12+
return fmt.Sprintf(`task: No task file found (is it named "%s"?). Use "task --init" to create a new one`, err.taskFile)
1313
}
1414

1515
type taskNotFoundError struct {

init.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package task
2+
3+
import (
4+
"io/ioutil"
5+
"log"
6+
"os"
7+
)
8+
9+
const defaultTaskfile = `# github.com/go-task/task
10+
11+
default:
12+
cmds:
13+
- echo "Hello, World!"
14+
`
15+
16+
func initTaskfile() {
17+
for _, f := range []string{"Taskfile.yml", "Taskfile.toml", "Taskfile.json"} {
18+
if _, err := os.Stat(f); err == nil {
19+
log.Printf("A Taskfile already exists")
20+
os.Exit(1)
21+
return
22+
}
23+
}
24+
25+
if err := ioutil.WriteFile("Taskfile.yml", []byte(defaultTaskfile), 0666); err != nil {
26+
log.Fatalf("%v", err)
27+
}
28+
log.Printf("Taskfile.yml created in the current directory")
29+
}

task.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ var (
1818
// TaskFilePath is the default Taskfile
1919
TaskFilePath = "Taskfile"
2020

21+
// Init (--init or -f flag) creates a Taskfile.yml in the current folder if not exists
22+
Init bool
2123
// Force (--force or -f flag) forces a task to run even when it's up-to-date
2224
Force bool
2325
// Watch (--watch or -w flag) enables watch of a task
@@ -46,6 +48,12 @@ func Run() {
4648
log.SetFlags(0)
4749

4850
args := pflag.Args()
51+
52+
if Init {
53+
initTaskfile()
54+
return
55+
}
56+
4957
if len(args) == 0 {
5058
log.Println("task: No argument given, trying default task")
5159
args = []string{"default"}

task_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,22 @@ func TestStatus(t *testing.T) {
144144
t.Errorf("Wrong output message: %s", buff.String())
145145
}
146146
}
147+
148+
func TestInit(t *testing.T) {
149+
const dir = "testdata/init"
150+
var file = filepath.Join(dir, "Taskfile.yml")
151+
152+
_ = os.Remove(file)
153+
if _, err := os.Stat(file); err == nil {
154+
t.Errorf("Taskfile.yml should not exists")
155+
}
156+
157+
c := exec.Command("task", "--init")
158+
c.Dir = dir
159+
if err := c.Run(); err != nil {
160+
t.Error(err)
161+
}
162+
if _, err := os.Stat(file); err != nil {
163+
t.Errorf("Taskfile.yml should exists")
164+
}
165+
}

testdata/init/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.yml

0 commit comments

Comments
 (0)