Skip to content

Commit

Permalink
Kando command for creating phase output (#4021)
Browse files Browse the repository at this point in the history
* Kando command for creating phase output

* Fix typo

* Minor improvements
  • Loading branch information
pavannd1 authored and Ilya Kislenko committed Oct 9, 2018
1 parent 2f639d0 commit 4534e87
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/kando/kando.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ func newRootCommand() *cobra.Command {
Version: version.VersionString(),
}
rootCmd.AddCommand(newLocationCommand())
rootCmd.AddCommand(newOutputCommand())
return rootCmd
}
34 changes: 34 additions & 0 deletions pkg/kando/output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package kando

import (
"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/kanisterio/kanister/pkg/output"
)

func newOutputCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "output <key> <value>",
Short: "Create phase output with given key:value",
Args: func(c *cobra.Command, args []string) error {
return validateArguments(c, args)
},
// TODO: Example invocations
RunE: func(c *cobra.Command, args []string) error {
return runOutputCommand(c, args)
},
}
return cmd
}

func validateArguments(c *cobra.Command, args []string) error {
if err := cobra.ExactArgs(2); err != nil {
return errors.New("Command requires exactly two arguments")
}
return output.ValidateKey(args[0])
}

func runOutputCommand(c *cobra.Command, args []string) error {
return output.PrintOutput(args[0], args[1])
}
54 changes: 54 additions & 0 deletions pkg/output/output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package output

import (
"encoding/json"
"fmt"
"regexp"

"github.com/pkg/errors"
)

const (
phaseOpString = "###Phase-output###:"
)

type Output struct {
Key string `json:"key"`
Value string `json:"value"`
}

func marshalOutput(key, value string) (string, error) {
out := &Output{
Key: key,
Value: value,
}
outString, err := json.Marshal(out)
if err != nil {
return "", errors.Wrapf(err, "Failed to marshall key-value pair")
}
return string(outString), nil
}

// ValidateKey validates the key argument
func ValidateKey(key string) error {
// key should be non-empty
if key == "" {
return errors.New("Key should not be empty")
}
// key can contain only alpha numeric characters and underscore
valid := regexp.MustCompile("^[a-zA-Z0-9_]*$").MatchString
if !valid(key) {
return errors.New("Key should contain only alphanumeric characters and underscore")
}
return nil
}

// PrintOutput runs the `kando output` command
func PrintOutput(key, value string) error {
outString, err := marshalOutput(key, value)
if err != nil {
return err
}
fmt.Println(phaseOpString, outString)
return nil
}
31 changes: 31 additions & 0 deletions pkg/output/output_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package output

import (
"testing"

. "gopkg.in/check.v1"
)

// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }

type OutputSuite struct{}

var _ = Suite(&OutputSuite{})

func (s *OutputSuite) TestValidateKey(c *C) {
for _, tc := range []struct {
key string
checker Checker
}{
{"validKey", IsNil},
{"validKey2", IsNil},
{"valid_key", IsNil},
{"invalid-key", NotNil},
{"invalid.key", NotNil},
{"`invalidKey", NotNil},
} {
err := ValidateKey(tc.key)
c.Check(err, tc.checker, Commentf("Key (%s) failed!", tc.key))
}
}

0 comments on commit 4534e87

Please sign in to comment.