diff --git a/cmd/tink-cli/cmd/template/create.go b/cmd/tink-cli/cmd/template/create.go index 043df9921..79657563f 100644 --- a/cmd/tink-cli/cmd/template/create.go +++ b/cmd/tink-cli/cmd/template/create.go @@ -3,6 +3,7 @@ package template import ( "context" "fmt" + "io" "io/ioutil" "log" "os" @@ -22,54 +23,80 @@ var ( // createCmd represents the create subcommand for template command var createCmd = &cobra.Command{ - Use: "create", - Short: "create a workflow template ", - Example: "tink template create [flags]", + Use: "create", + Short: "create a workflow template ", + Example: `tink template create [flags] +cat /tmp/example.tmpl | tink template create -n example`, + 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") + } + } + return nil + }, Run: func(c *cobra.Command, args []string) { - validateTemplate() - createTemplate(c, args) + var reader io.Reader + if isInputFromPipe() { + reader = os.Stdin + } else { + f, err := os.Open(filePath) + if err != nil { + log.Println(err) + } + reader = f + } + + data := readAll(reader) + if data != nil { + if err := tryParseTemplate(data); err != nil { + log.Println(err) + return + } + createTemplate(data) + } }, } +func readAll(reader io.Reader) []byte { + data, err := ioutil.ReadAll(reader) + if err != nil { + log.Println(err) + } + return data +} + 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)") - - createCmd.MarkPersistentFlagRequired(fPath) createCmd.MarkPersistentFlagRequired(fName) } -func validateTemplate() { - _, err := tt.ParseFiles(filePath) - if err != nil { - log.Fatalln(err) +func tryParseTemplate(data []byte) error { + tmpl := *tt.New("") + if _, err := tmpl.Parse(string(data)); err != nil { + return err } + return nil } -func readTemplateData() []byte { - f, err := os.Open(filePath) - if err != nil { - log.Fatal(err) - } - defer f.Close() - - data, err := ioutil.ReadAll(f) - if err != nil { - log.Fatal(err) - } - return data -} - -func createTemplate(c *cobra.Command, args []string) { - req := template.WorkflowTemplate{Name: templateName, Data: readTemplateData()} +func createTemplate(data []byte) { + req := template.WorkflowTemplate{Name: templateName, Data: data} res, err := client.TemplateClient.CreateTemplate(context.Background(), &req) if err != nil { - log.Fatal(err) + log.Println(err) + return } fmt.Println("Created Template: ", res.Id) } +func isInputFromPipe() bool { + fileInfo, _ := os.Stdin.Stat() + return fileInfo.Mode()&os.ModeCharDevice == 0 +} + func init() { addFlags() SubCommands = append(SubCommands, createCmd) diff --git a/cmd/tink-cli/cmd/template/update.go b/cmd/tink-cli/cmd/template/update.go index f71d1115d..805d03334 100644 --- a/cmd/tink-cli/cmd/template/update.go +++ b/cmd/tink-cli/cmd/template/update.go @@ -3,7 +3,9 @@ package template import ( "context" "fmt" + "io/ioutil" "log" + "os" uuid "github.com/satori/go.uuid" "github.com/spf13/cobra" @@ -47,8 +49,14 @@ func updateTemplate(id string) { if filePath == "" && templateName != "" { req.Name = templateName } else if filePath != "" && templateName == "" { - validateTemplate() - req.Data = readTemplateData() + data := readTemplateData() + if data != nil { + if err := tryParseTemplate(data); err != nil { + log.Println(err) + return + } + req.Data = data + } } else { req.Name = templateName req.Data = readTemplateData() @@ -61,6 +69,20 @@ func updateTemplate(id string) { fmt.Println("Updated Template: ", id) } +func readTemplateData() []byte { + f, err := os.Open(filePath) + if err != nil { + log.Println(err) + } + defer f.Close() + + data, err := ioutil.ReadAll(f) + if err != nil { + log.Println(err) + } + return data +} + func init() { flags := updateCmd.PersistentFlags() flags.StringVarP(&filePath, "path", "p", "", "path to the template file")