add template option
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Kasper Juul Hermansen 2022-10-31 21:08:44 +01:00
parent b9d4260597
commit 03fb65a9b4
Signed by: kjuulh
GPG Key ID: 0F95C140730F2F23
5 changed files with 98 additions and 2 deletions

View File

@ -1,13 +1,17 @@
package cli
import "github.com/spf13/cobra"
import (
"git.front.kjuulh.io/kjuulh/bust/pkg/cli/templatecmd"
"github.com/spf13/cobra"
)
func NewCli() *cobra.Command {
cmd := &cobra.Command{
Use: "dagger",
Use: "bust",
}
cmd.AddCommand(Build())
cmd.AddCommand(templatecmd.NewTemplateCmd())
return cmd
}

View File

@ -0,0 +1,71 @@
package templatecmd
import (
"embed"
"errors"
"os"
"text/template"
"github.com/spf13/cobra"
)
//go:embed templates/gobin_default/*
var gobinDefault embed.FS
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":
if err := initializeTemplate(&gobinDefault, name); err != nil {
return err
}
break
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
}
func initializeTemplate(t *embed.FS, name string) error {
tinit := template.
Must(
template.
New("").
Delims("[[", "]]").
ParseFS(t, "templates/gobin_default/*"),
)
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,
})
}

View File

@ -0,0 +1,13 @@
package templatecmd
import "github.com/spf13/cobra"
func NewTemplateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "template",
}
cmd.AddCommand(NewInitCmd())
return cmd
}

View File

@ -0,0 +1,4 @@
kind: template
load: bust_gobin_default_template.yaml
name: [[.Name]]
data: {}

4
tmp/.drone.yml Normal file
View File

@ -0,0 +1,4 @@
kind: template
load: bust_gobin_default_template.yaml
name: something
data: {}