Skip to content

Commit

Permalink
tink template create now accepts data from STDIN (#133)
Browse files Browse the repository at this point in the history
At present, we create a template as shown below:
```
$ tink template create --name example --path /tmp/example.tmpl 
```

With the code changes in place, a user can now pipe the template data from a file. For example, if the data is saved in `/tmp/example.tmpl`, you can create a template with either of the following:
```
$ cat /tmp/example.tmpl | tink template create -n example
$ tink template create --name example --path /tmp/example.tmpl
```

Signed-off-by: Gaurav Gahlot <[email protected]>
  • Loading branch information
mergify[bot] authored Jun 10, 2020
2 parents a1c120e + 7384799 commit 6a9ed8a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 30 deletions.
83 changes: 55 additions & 28 deletions cmd/tink-cli/cmd/template/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package template
import (
"context"
"fmt"
"io"
"io/ioutil"
"log"
"os"
Expand All @@ -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)

This comment has been minimized.

Copy link
@rgl

rgl Jun 10, 2020

Contributor

Shouldn't these log.Println lines be replaced with proper error handling (e.g. log.Fatal)?

}
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)
Expand Down
26 changes: 24 additions & 2 deletions cmd/tink-cli/cmd/template/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package template
import (
"context"
"fmt"
"io/ioutil"
"log"
"os"

uuid "github.com/satori/go.uuid"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -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()
Expand All @@ -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")
Expand Down

0 comments on commit 6a9ed8a

Please sign in to comment.