Skip to content

Commit

Permalink
Fix help and usage for tink template update (#587)
Browse files Browse the repository at this point in the history
## Description

`tink template update` previously had the wrong help output (stating argument is `--file` instead of `--path`) and did not support providing the updated template data over stdin.

## Why is this needed

- sandbox users run commands through `docker exec`, so supporting stdin makes it easier for them to use the command
- users should expect the help output to match the expected usage.

## How Has This Been Tested?
TODO

## How are existing users impacted? What migration steps/scripts do we need?

Should not affect existing users, only fix issues users are already encountering.
  • Loading branch information
mergify[bot] authored Feb 9, 2022
2 parents c047011 + 239df24 commit 03a3d54
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
31 changes: 23 additions & 8 deletions cmd/tink-cli/cmd/template/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package template
import (
"context"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"

"github.com/google/uuid"
"github.com/spf13/cobra"
Expand All @@ -20,12 +22,16 @@ func NewUpdateCommand() *cobra.Command {
Use: "update [id] [flags]",
Short: "update a workflow template",
Long: `The update command allows you change the definition of an existing workflow template:
# Pipe the file to create a template:
$ cat /tmp/example.tmpl | tink template update 614168df-45a5-11eb-b13d-0242ac120003
# Update an existing template:
$ tink template update 614168df-45a5-11eb-b13d-0242ac120003 --file /tmp/example.tmpl
$ tink template update 614168df-45a5-11eb-b13d-0242ac120003 --path /tmp/example.tmpl
`,
PreRunE: func(c *cobra.Command, args []string) error {
if filePath == "" {
return fmt.Errorf("%v requires the '--file' flag", c.UseLine())
if !isInputFromPipe() {
if filePath == "" {
return fmt.Errorf("%v requires the '--path' flag", c.UseLine())
}
}
return nil
},
Expand Down Expand Up @@ -79,15 +85,24 @@ func updateTemplate(id string) {
}

func readTemplateData() (string, error) {
f, err := os.Open(filePath)
if err != nil {
return "", err
var reader io.Reader
if isInputFromPipe() {
reader = os.Stdin
} else {
f, err := os.Open(filepath.Clean(filePath))
if err != nil {
return "", err
}

defer f.Close()

reader = f
}
defer f.Close()

data, err := ioutil.ReadAll(f)
data, err := ioutil.ReadAll(reader)
if err != nil {
return "", err
}

return string(data), nil
}
1 change: 1 addition & 0 deletions tools.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//go:build tools
// +build tools

package tools

Expand Down

0 comments on commit 03a3d54

Please sign in to comment.