2022-10-31 21:08:44 +01:00
|
|
|
package templatecmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"errors"
|
2022-10-31 22:00:45 +01:00
|
|
|
"fmt"
|
2022-10-31 21:08:44 +01:00
|
|
|
"os"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed templates/gobin_default/*
|
|
|
|
var gobinDefault embed.FS
|
|
|
|
|
2022-10-31 22:00:45 +01:00
|
|
|
//go:embed templates/docker/*
|
|
|
|
var docker embed.FS
|
|
|
|
|
2022-10-31 22:35:36 +01:00
|
|
|
//go:embed templates/default/*
|
|
|
|
var defaultFs embed.FS
|
|
|
|
|
2022-11-06 00:39:03 +01:00
|
|
|
//go:embed templates/rustbin_default/*
|
|
|
|
var rustbinDefault embed.FS
|
|
|
|
|
2022-10-31 21:08:44 +01:00
|
|
|
func NewInitCmd() *cobra.Command {
|
|
|
|
var (
|
|
|
|
template string
|
|
|
|
name string
|
|
|
|
)
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "init",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if err := cmd.ParseFlags(args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch template {
|
|
|
|
case "gobin_default":
|
2022-10-31 22:00:45 +01:00
|
|
|
if err := initializeTemplate(&gobinDefault, "gobin_default", name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
break
|
|
|
|
case "docker":
|
|
|
|
if err := initializeTemplate(&docker, "docker", name); err != nil {
|
2022-10-31 21:08:44 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
break
|
2022-10-31 22:35:36 +01:00
|
|
|
case "default":
|
|
|
|
if err := initializeTemplate(&defaultFs, "default", name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
break
|
2022-11-06 00:39:03 +01:00
|
|
|
case "rustbin_default":
|
|
|
|
if err := initializeTemplate(&rustbinDefault, "rustbin_default", name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
break
|
2022-10-31 21:08:44 +01:00
|
|
|
default:
|
|
|
|
return errors.New("could not find matching templates, please run [bust template ls] instead")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.PersistentFlags().StringVarP(&template, "template", "p", "", "The template to initialize")
|
|
|
|
cmd.MarkPersistentFlagRequired("template")
|
|
|
|
|
|
|
|
cmd.PersistentFlags().StringVarP(&name, "name", "n", "", "The name into the template")
|
|
|
|
cmd.MarkPersistentFlagRequired("name")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2022-10-31 22:00:45 +01:00
|
|
|
func initializeTemplate(t *embed.FS, path string, name string) error {
|
2022-10-31 21:08:44 +01:00
|
|
|
tinit := template.
|
|
|
|
Must(
|
|
|
|
template.
|
|
|
|
New("").
|
|
|
|
Delims("[[", "]]").
|
2022-10-31 22:00:45 +01:00
|
|
|
ParseFS(
|
|
|
|
t,
|
|
|
|
fmt.Sprintf("templates/%s/*", path),
|
|
|
|
),
|
2022-10-31 21:08:44 +01:00
|
|
|
)
|
|
|
|
type data struct {
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
droneWriter, err := os.Create(".drone.yml")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tinit.ExecuteTemplate(droneWriter, ".drone.yml", data{
|
|
|
|
Name: name,
|
|
|
|
})
|
|
|
|
}
|