2020-11-17 05:41:34 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
|
2021-11-02 03:22:27 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2020-11-17 05:41:34 +00:00
|
|
|
"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 {
|
2022-06-14 19:59:53 +00:00
|
|
|
if _, err := os.Lstat("tmpl.yaml"); !os.IsNotExist(err) {
|
2020-11-17 05:41:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-14 19:59:53 +00:00
|
|
|
return errors.New("tmpl.yaml already detected, aborting initialization")
|
2020-11-17 05:41:34 +00:00
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2022-06-14 19:59:53 +00:00
|
|
|
fi, err := os.Create("tmpl.yaml")
|
2020-11-17 05:41:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := fi.WriteString(comments); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Mkdir("template", os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-02 03:22:27 +00:00
|
|
|
log.Info().Msg("Template initialized!")
|
2020-11-17 05:41:34 +00:00
|
|
|
return fi.Close()
|
|
|
|
}
|
|
|
|
|
2022-06-14 19:59:53 +00:00
|
|
|
var comments = `# tmpl.yaml
|
2020-11-17 05:41:34 +00:00
|
|
|
# Write any template args here to prompt the user for, giving any defaults/options as applicable
|
|
|
|
|
2022-06-14 19:59:53 +00:00
|
|
|
prompts:
|
|
|
|
- id: name # The unique ID for the prompt
|
|
|
|
label: Project Name # The prompt message/label
|
|
|
|
help: The name to use in the project # Optional help message for the prompt
|
|
|
|
default: tmpl # Prompt default
|
2020-11-17 05:41:34 +00:00
|
|
|
`
|