This commit is contained in:
parent
b9d4260597
commit
03fb65a9b4
@ -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
|
||||
}
|
||||
|
71
pkg/cli/templatecmd/init.go
Normal file
71
pkg/cli/templatecmd/init.go
Normal 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,
|
||||
})
|
||||
}
|
13
pkg/cli/templatecmd/template.go
Normal file
13
pkg/cli/templatecmd/template.go
Normal 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
|
||||
}
|
4
pkg/cli/templatecmd/templates/gobin_default/.drone.yml
Normal file
4
pkg/cli/templatecmd/templates/gobin_default/.drone.yml
Normal file
@ -0,0 +1,4 @@
|
||||
kind: template
|
||||
load: bust_gobin_default_template.yaml
|
||||
name: [[.Name]]
|
||||
data: {}
|
4
tmp/.drone.yml
Normal file
4
tmp/.drone.yml
Normal file
@ -0,0 +1,4 @@
|
||||
kind: template
|
||||
load: bust_gobin_default_template.yaml
|
||||
name: something
|
||||
data: {}
|
Loading…
Reference in New Issue
Block a user