diff --git a/cmd/tink-cli/cmd/template/create.go b/cmd/tink-cli/cmd/template/create.go index b19d825b8..7a9c85341 100644 --- a/cmd/tink-cli/cmd/template/create.go +++ b/cmd/tink-cli/cmd/template/create.go @@ -2,36 +2,37 @@ package template import ( "context" + "errors" "fmt" "io" "io/ioutil" "log" "os" - tt "text/template" "github.com/spf13/cobra" "github.com/tinkerbell/tink/client" "github.com/tinkerbell/tink/protos/template" + "github.com/tinkerbell/tink/workflow" ) -var ( - fPath = "path" - fName = "name" - filePath string - templateName string -) +var filePath string // createCmd represents the create subcommand for template command var createCmd = &cobra.Command{ Use: "create", Short: "create a workflow template ", - Example: `tink template create [flags] -cat /tmp/example.tmpl | tink template create -n example`, + Long: `The create command allows you create workflow templates: + +# Pipe the file to create a template: +$ cat /tmp/example.tmpl | tink template create + +# Create template using the --file flag: +$ tink template create --file /tmp/example.tmpl +`, PreRunE: func(c *cobra.Command, args []string) error { if !isInputFromPipe() { - path, _ := c.Flags().GetString(fPath) - if path == "" { - return fmt.Errorf("either pipe the template or provide the required '--path' flag") + if filePath == "" { + return errors.New("either pipe the template or provide the required '--file' flag") } } return nil @@ -50,10 +51,11 @@ cat /tmp/example.tmpl | tink template create -n example`, data := readAll(reader) if data != nil { - if err := tryParseTemplate(string(data)); err != nil { + wf, err := workflow.Parse(data) + if err != nil { log.Fatal(err) } - createTemplate(data) + createTemplate(wf.Name, data) } }, } @@ -68,21 +70,11 @@ func readAll(reader io.Reader) []byte { func addFlags() { flags := createCmd.PersistentFlags() - flags.StringVarP(&filePath, "path", "p", "", "path to the template file") - flags.StringVarP(&templateName, "name", "n", "", "unique name for the template (alphanumeric and case sensitive)") - _ = createCmd.MarkPersistentFlagRequired(fName) -} - -func tryParseTemplate(data string) error { - tmpl := *tt.New("") - if _, err := tmpl.Parse(data); err != nil { - return err - } - return nil + flags.StringVarP(&filePath, "file", "", "", "path to the template file") } -func createTemplate(data []byte) { - req := template.WorkflowTemplate{Name: templateName, Data: string(data)} +func createTemplate(name string, data []byte) { + req := template.WorkflowTemplate{Name: name, Data: string(data)} res, err := client.TemplateClient.CreateTemplate(context.Background(), &req) if err != nil { log.Fatal(err) diff --git a/cmd/tink-cli/cmd/template/update.go b/cmd/tink-cli/cmd/template/update.go index 1e07b9735..eecdb30d7 100644 --- a/cmd/tink-cli/cmd/template/update.go +++ b/cmd/tink-cli/cmd/template/update.go @@ -11,18 +11,21 @@ import ( "github.com/spf13/cobra" "github.com/tinkerbell/tink/client" "github.com/tinkerbell/tink/protos/template" + "github.com/tinkerbell/tink/workflow" ) // updateCmd represents the get subcommand for template command var updateCmd = &cobra.Command{ - Use: "update [id] [flags]", - Short: "update a template", - Example: "tink template update [id] [flags]", + Use: "update [id] [flags]", + Short: "update a workflow template", + Long: `The update command allows you change the definition of an existing workflow template: + +# Update an existing template: +$ tink template update 614168df-45a5-11eb-b13d-0242ac120003 --file /tmp/example.tmpl +`, PreRunE: func(c *cobra.Command, args []string) error { - name, _ := c.Flags().GetString(fName) - path, _ := c.Flags().GetString(fPath) - if name == "" && path == "" { - return fmt.Errorf("%v requires at least one flag", c.UseLine()) + if filePath == "" { + return fmt.Errorf("%v requires the '--file' flag", c.UseLine()) } return nil }, @@ -46,19 +49,18 @@ var updateCmd = &cobra.Command{ func updateTemplate(id string) { req := template.WorkflowTemplate{Id: id} - if filePath == "" && templateName != "" { - req.Name = templateName - } else if filePath != "" && templateName == "" { + if filePath != "" { data := readTemplateData() if data != "" { - if err := tryParseTemplate(data); err != nil { + wf, err := workflow.Parse([]byte(data)) + if err != nil { log.Fatal(err) } + req.Name = wf.Name req.Data = data } } else { - req.Name = templateName - req.Data = readTemplateData() + log.Fatal("Nothing is provided in the file path") } _, err := client.TemplateClient.UpdateTemplate(context.Background(), &req) @@ -84,8 +86,7 @@ func readTemplateData() string { func init() { flags := updateCmd.PersistentFlags() - flags.StringVarP(&filePath, "path", "p", "", "path to the template file") - flags.StringVarP(&templateName, "name", "n", "", "unique name for the template (alphanumeric)") + flags.StringVarP(&filePath, "file", "", "", "path to the template file") SubCommands = append(SubCommands, updateCmd) } diff --git a/cmd/tink-cli/cmd/template_test.go b/cmd/tink-cli/cmd/template_test.go index fdddcb82d..005d40094 100644 --- a/cmd/tink-cli/cmd/template_test.go +++ b/cmd/tink-cli/cmd/template_test.go @@ -2,6 +2,7 @@ package cmd import ( "bytes" + "fmt" "strings" "testing" @@ -44,8 +45,9 @@ func Test_templateCmd(t *testing.T) { if err := root.Execute(); err != nil { t.Error(err) } - if !strings.Contains(out.String(), "list all saved templates") { - t.Error("expected output should include list all saved templates") + want := "list all saved templates" + if !strings.Contains(out.String(), want) { + t.Error(fmt.Errorf("unexpected output, looking for %q as a substring in %q", want, out.String())) } }, }, @@ -61,8 +63,9 @@ func Test_templateCmd(t *testing.T) { if err := root.Execute(); err != nil { t.Error(err) } - if !strings.Contains(out.String(), "create a workflow template") { - t.Error("expected output should include create a workflow template") + want := "Create template using the --file flag" + if !strings.Contains(out.String(), want) { + t.Error(fmt.Errorf("unexpected output, looking for %q as a substring in %q", want, out.String())) } }, }, @@ -78,8 +81,9 @@ func Test_templateCmd(t *testing.T) { if err := root.Execute(); err != nil { t.Error(err) } - if !strings.Contains(out.String(), "delete a template") { - t.Error("expected output should include delete a template") + want := "delete a template" + if !strings.Contains(out.String(), want) { + t.Error(fmt.Errorf("unexpected output, looking for %q as a substring in %q", want, out.String())) } }, }, @@ -95,8 +99,9 @@ func Test_templateCmd(t *testing.T) { if err := root.Execute(); err != nil { t.Error(err) } - if !strings.Contains(out.String(), "get a template") { - t.Error("expected output should include get a template") + want := "get a template" + if !strings.Contains(out.String(), want) { + t.Error(fmt.Errorf("unexpected output, looking for %q as a substring in %q", want, out.String())) } }, }, @@ -112,8 +117,9 @@ func Test_templateCmd(t *testing.T) { if err := root.Execute(); err != nil { t.Error(err) } - if !strings.Contains(out.String(), "update a template") { - t.Error("expected output should include update a template") + want := "Update an existing template" + if !strings.Contains(out.String(), want) { + t.Error(fmt.Errorf("unexpected output, looking for %q as a substring in %q", want, out.String())) } }, },