init.go 770 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package task
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "github.com/go-task/task/v3/errors"
  7. "github.com/go-task/task/v3/internal/filepathext"
  8. )
  9. const defaultTaskfile = `# https://taskfile.dev
  10. version: '3'
  11. vars:
  12. GREETING: Hello, World!
  13. tasks:
  14. default:
  15. cmds:
  16. - echo "{{.GREETING}}"
  17. silent: true
  18. `
  19. const defaultTaskfileName = "Taskfile.yml"
  20. // InitTaskfile Taskfile creates a new Taskfile
  21. func InitTaskfile(w io.Writer, dir string) error {
  22. f := filepathext.SmartJoin(dir, defaultTaskfileName)
  23. if _, err := os.Stat(f); err == nil {
  24. return errors.TaskfileAlreadyExistsError{}
  25. }
  26. if err := os.WriteFile(f, []byte(defaultTaskfile), 0o644); err != nil {
  27. return err
  28. }
  29. fmt.Fprintf(w, "%s created in the current directory\n", defaultTaskfile)
  30. return nil
  31. }