Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #377: Removing name flag from the tink template create/update command #385

Merged
merged 4 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 19 additions & 27 deletions cmd/tink-cli/cmd/template/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
},
}
Expand All @@ -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)
Expand Down
31 changes: 16 additions & 15 deletions cmd/tink-cli/cmd/template/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand All @@ -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)
Expand All @@ -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)
}
26 changes: 16 additions & 10 deletions cmd/tink-cli/cmd/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"bytes"
"fmt"
"strings"
"testing"

Expand Down Expand Up @@ -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()))
}
},
},
Expand All @@ -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()))
}
},
},
Expand All @@ -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()))
}
},
},
Expand All @@ -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()))
}
},
},
Expand All @@ -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()))
}
},
},
Expand Down