-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kando command for creating phase output (#4021)
* Kando command for creating phase output * Fix typo * Minor improvements
- Loading branch information
Showing
4 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |