From 03fb65a9b4c75c32a9e4d7a46bb90f490c2fa0ed Mon Sep 17 00:00:00 2001 From: kjuulh Date: Mon, 31 Oct 2022 21:08:44 +0100 Subject: [PATCH] add template option --- pkg/cli/root.go | 8 ++- pkg/cli/templatecmd/init.go | 71 +++++++++++++++++++ pkg/cli/templatecmd/template.go | 13 ++++ .../templates/gobin_default/.drone.yml | 4 ++ tmp/.drone.yml | 4 ++ 5 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 pkg/cli/templatecmd/init.go create mode 100644 pkg/cli/templatecmd/template.go create mode 100644 pkg/cli/templatecmd/templates/gobin_default/.drone.yml create mode 100644 tmp/.drone.yml diff --git a/pkg/cli/root.go b/pkg/cli/root.go index 78f9540..409b3b0 100644 --- a/pkg/cli/root.go +++ b/pkg/cli/root.go @@ -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 } diff --git a/pkg/cli/templatecmd/init.go b/pkg/cli/templatecmd/init.go new file mode 100644 index 0000000..c7b33fa --- /dev/null +++ b/pkg/cli/templatecmd/init.go @@ -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, + }) +} diff --git a/pkg/cli/templatecmd/template.go b/pkg/cli/templatecmd/template.go new file mode 100644 index 0000000..dc6e0dc --- /dev/null +++ b/pkg/cli/templatecmd/template.go @@ -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 +} diff --git a/pkg/cli/templatecmd/templates/gobin_default/.drone.yml b/pkg/cli/templatecmd/templates/gobin_default/.drone.yml new file mode 100644 index 0000000..e458ff3 --- /dev/null +++ b/pkg/cli/templatecmd/templates/gobin_default/.drone.yml @@ -0,0 +1,4 @@ +kind: template +load: bust_gobin_default_template.yaml +name: [[.Name]] +data: {} diff --git a/tmp/.drone.yml b/tmp/.drone.yml new file mode 100644 index 0000000..d3e7718 --- /dev/null +++ b/tmp/.drone.yml @@ -0,0 +1,4 @@ +kind: template +load: bust_gobin_default_template.yaml +name: something +data: {}