2017-05-17 20:38:46 +02:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
2017-07-31 00:29:49 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-05-17 20:38:46 +02:00
|
|
|
"os"
|
2022-08-06 23:19:07 +02:00
|
|
|
|
|
|
|
"github.com/go-task/task/v3/internal/filepathext"
|
2017-05-17 20:38:46 +02:00
|
|
|
)
|
|
|
|
|
2019-03-05 04:23:30 +02:00
|
|
|
const defaultTaskfile = `# https://taskfile.dev
|
2017-05-17 20:38:46 +02:00
|
|
|
|
2019-08-19 21:01:36 +02:00
|
|
|
version: '3'
|
2018-03-04 21:17:51 +02:00
|
|
|
|
|
|
|
vars:
|
|
|
|
GREETING: Hello, World!
|
|
|
|
|
|
|
|
tasks:
|
|
|
|
default:
|
|
|
|
cmds:
|
|
|
|
- echo "{{.GREETING}}"
|
|
|
|
silent: true
|
2017-05-17 20:38:46 +02:00
|
|
|
`
|
|
|
|
|
2023-03-17 02:53:01 +02:00
|
|
|
const defaultTaskfileName = "Taskfile.yml"
|
|
|
|
|
2017-06-04 21:02:04 +02:00
|
|
|
// InitTaskfile Taskfile creates a new Taskfile
|
2018-03-04 21:17:51 +02:00
|
|
|
func InitTaskfile(w io.Writer, dir string) error {
|
2023-03-17 02:53:01 +02:00
|
|
|
f := filepathext.SmartJoin(dir, defaultTaskfileName)
|
2018-03-04 21:17:51 +02:00
|
|
|
|
|
|
|
if _, err := os.Stat(f); err == nil {
|
|
|
|
return ErrTaskfileAlreadyExists
|
2017-05-17 20:38:46 +02:00
|
|
|
}
|
|
|
|
|
2022-08-17 19:37:58 +02:00
|
|
|
if err := os.WriteFile(f, []byte(defaultTaskfile), 0o644); err != nil {
|
2017-06-04 21:02:04 +02:00
|
|
|
return err
|
2017-05-17 20:38:46 +02:00
|
|
|
}
|
2023-03-17 02:53:01 +02:00
|
|
|
fmt.Fprintf(w, "%s created in the current directory\n", defaultTaskfile)
|
2017-06-04 21:02:04 +02:00
|
|
|
return nil
|
2017-05-17 20:38:46 +02:00
|
|
|
}
|