Skip to content

Commit

Permalink
Fix tinkerbell#377: Removing name flag from the 'tink template crea…
Browse files Browse the repository at this point in the history
…te/update' command in tink-cli

Signed-off-by: parauliya <aman@infracloud.io>
  • Loading branch information
parauliya committed Dec 11, 2020
1 parent 6d9159b commit afde2c4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
22 changes: 17 additions & 5 deletions cmd/tink-cli/cmd/template/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package template

import (
"context"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -12,21 +13,25 @@ import (
"github.com/spf13/cobra"
"github.com/tinkerbell/tink/client"
"github.com/tinkerbell/tink/protos/template"
"gopkg.in/yaml.v2"
)

var (
fPath = "path"
fName = "name"
filePath string
templateName string
)

type TemplateName struct {
Name string `yaml:"name"`
}

// 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`,
cat /tmp/example.tmpl | tink template create `,
PreRunE: func(c *cobra.Command, args []string) error {
if !isInputFromPipe() {
path, _ := c.Flags().GetString(fPath)
Expand Down Expand Up @@ -69,16 +74,23 @@ 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
var templ TemplateName
err := yaml.Unmarshal([]byte(data), &templ)
if err != nil {
return err
}
if templ.Name != "" {
templateName = templ.Name
return nil
}
return errors.New("Template does not have `name` field which is mandatory")
}

func createTemplate(data []byte) {
Expand Down
11 changes: 4 additions & 7 deletions cmd/tink-cli/cmd/template/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ var updateCmd = &cobra.Command{
Short: "update a template",
Example: "tink template update [id] [flags]",
PreRunE: func(c *cobra.Command, args []string) error {
name, _ := c.Flags().GetString(fName)
path, _ := c.Flags().GetString(fPath)
if name == "" && path == "" {
if path == "" {
return fmt.Errorf("%v requires at least one flag", c.UseLine())
}
return nil
Expand All @@ -46,19 +45,17 @@ 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 {
log.Fatal(err)
}
req.Name = templateName
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 Down

0 comments on commit afde2c4

Please sign in to comment.