Home

tmpl @d02502078f5c143f365e07447af8ca4f0a5f178c - refs - log -
-
https://git.jolheiser.com/tmpl.git
Template automation
tmpl / cmd / init.go
- raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package cmd

import (
	"errors"
	"os"

	"github.com/rs/zerolog/log"
	"github.com/urfave/cli/v2"
)

var Init = &cli.Command{
	Name:        "init",
	Usage:       "Initialize a template",
	Description: "Initializes a template structure for creating a new tmpl template",
	Action:      runInit,
}

func runInit(_ *cli.Context) error {
	if _, err := os.Lstat("tmpl.yaml"); !os.IsNotExist(err) {
		if err != nil {
			return err
		}
		return errors.New("tmpl.yaml already detected, aborting initialization")
	}
	if fi, err := os.Lstat("template"); !os.IsNotExist(err) {
		if err != nil {
			return err
		}
		if !fi.IsDir() {
			return errors.New("template file found instead of directory, aborting initialization")
		}
		return errors.New("template directory already detected, aborting initialization")
	}

	fi, err := os.Create("tmpl.yaml")
	if err != nil {
		return err
	}
	if _, err := fi.WriteString(initConfig); err != nil {
		return err
	}
	if err := os.Mkdir("template", os.ModePerm); err != nil {
		return err
	}
	log.Info().Msg("Template initialized!")
	return fi.Close()
}

var initConfig = `# tmpl.yaml
# Write any template args here to prompt the user for, giving any defaults/options as applicable

prompts:
  - id: name                              # The unique ID for the prompt
    label: Project Name                   # (Optional) Prompt message/label, defaults to id
    help: The name to use in the project  # (Optional) Help message for the prompt
    default: tmpl                         # (Optional) Prompt default
    options:                              # (Optional) Set of options the user can choose from
      - coolproject123
      - ${USER}'s cool project
`