Skip to content

Commit

Permalink
fix: change postRendererArgs to Slice Type and use args...
Browse files Browse the repository at this point in the history
Signed-off-by: guofutan <guofutan@tencent.com>
  • Loading branch information
guofutan committed Jan 22, 2022
1 parent 44423fb commit d12170b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 38 deletions.
61 changes: 35 additions & 26 deletions cmd/helm/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ func (o *outputValue) Set(s string) error {

func bindPostRenderFlag(cmd *cobra.Command, varRef *postrender.PostRenderer) {
p := &postRendererOptions{varRef, "", []string{}}
cmd.Flags().Var(&postRendererExecFlag{p}, postRenderFlag, "the path to an executable to be used for post rendering. If it exists in $PATH, the binary will be used, otherwise it will try to look for the executable at the given path")
cmd.Flags().Var(&postRendererArgsFlag{p}, postRenderArgsFlag, "the args to an executable to be used for post rendering. (can specify multiple)")
cmd.Flags().Var(&postRendererString{p}, postRenderFlag, "the path to an executable to be used for post rendering. If it exists in $PATH, the binary will be used, otherwise it will try to look for the executable at the given path")
cmd.Flags().Var(&postRendererArgsSlice{p}, postRenderArgsFlag, "the args to an executable to be used for post rendering. (can specify multiple)")
}

type postRendererOptions struct {
Expand All @@ -126,62 +126,71 @@ type postRendererOptions struct {
args []string
}

type postRendererExecFlag struct {
type postRendererString struct {
options *postRendererOptions
}

func (p postRendererExecFlag) String() string {
return ""
func (p *postRendererString) String() string {
return p.options.binaryPath
}

func (p postRendererExecFlag) Type() string {
return "postrenderer-exec"
func (p *postRendererString) Type() string {
return "postRendererString"
}

func (p postRendererExecFlag) Set(s string) error {
if s == "" {
func (p *postRendererString) Set(val string) error {
if val == "" {
return nil
}
p.options.binaryPath = s
pr, err := postrender.NewExecWithArgs(p.options.binaryPath, p.options.args)
p.options.binaryPath = val
pr, err := postrender.NewExec(p.options.binaryPath, p.options.args...)
if err != nil {
return err
}
*p.options.renderer = pr
return nil
}

type postRendererArgsFlag struct {
type postRendererArgsSlice struct {
options *postRendererOptions
}

func (p postRendererArgsFlag) String() string {
return ""
func (p *postRendererArgsSlice) String() string {
return "[" + strings.Join(p.options.args, ",") + "]"
}

func (p postRendererArgsFlag) Type() string {
return "postrenderer-args"
func (p *postRendererArgsSlice) Type() string {
return "postRendererArgsSlice"
}

func (p postRendererArgsFlag) Set(s string) error {
if s == "" {
func (p *postRendererArgsSlice) Set(val string) error {
if val == "" || p.options.binaryPath == "" {
return nil
}
p.options.args = append(p.options.args, s)
// skip if postRenderFlag not set or parsed
if len(p.options.binaryPath) == 0 {
return nil
}
// update if already create PostRenderer
pr, err := postrender.NewExecWithArgs(p.options.binaryPath, p.options.args)
p.options.args = append(p.options.args, val)
// overwrite if already create PostRenderer by `post-renderer` flags
pr, err := postrender.NewExec(p.options.binaryPath, p.options.args...)
if err != nil {
return err
}

*p.options.renderer = pr
return nil
}

func (p *postRendererArgsSlice) Append(val string) error {
p.options.args = append(p.options.args, val)
return nil
}

func (p *postRendererArgsSlice) Replace(val []string) error {
p.options.args = val
return nil
}

func (p *postRendererArgsSlice) GetSlice() []string {
return p.options.args
}

func compVersionFlag(chartRef string, toComplete string) ([]string, cobra.ShellCompDirective) {
chartInfo := strings.Split(chartRef, "/")
if len(chartInfo) != 2 {
Expand Down
10 changes: 1 addition & 9 deletions pkg/postrender/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,7 @@ type execRender struct {
// It returns an error if the binary cannot be found. If the path does not
// contain any separators, it will search in $PATH, otherwise it will resolve
// any relative paths to a fully qualified path
func NewExec(binaryPath string) (PostRenderer, error) {
fullPath, err := getFullPath(binaryPath)
if err != nil {
return nil, err
}
return &execRender{fullPath, []string{}}, nil
}

func NewExecWithArgs(binaryPath string, args []string) (PostRenderer, error) {
func NewExec(binaryPath string, args ...string) (PostRenderer, error) {
fullPath, err := getFullPath(binaryPath)
if err != nil {
return nil, err
Expand Down
27 changes: 24 additions & 3 deletions pkg/postrender/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ import (
)

const testingScript = `#!/bin/sh
if [ $# -eq 0 ]; then
sed s/FOOTEST/BARTEST/g <&0
else
sed s/FOOTEST/BARTEST$#/g <&0
fi
`

func TestGetFullPath(t *testing.T) {
Expand Down Expand Up @@ -124,7 +128,7 @@ func TestExecRun(t *testing.T) {
is.Contains(output.String(), "BARTEST")
}

func TestNewExecWithArgsRun(t *testing.T) {
func TestNewExecWithOneArgsRun(t *testing.T) {
if runtime.GOOS == "windows" {
// the actual Run test uses a basic sed example, so skip this test on windows
t.Skip("skipping on windows")
Expand All @@ -133,12 +137,29 @@ func TestNewExecWithArgsRun(t *testing.T) {
testpath, cleanup := setupTestingScript(t)
defer cleanup()

renderer, err := NewExecWithArgs(testpath, []string{})
renderer, err := NewExec(testpath, "FOOTEST")
require.NoError(t, err)

output, err := renderer.Run(bytes.NewBufferString("FOOTEST"))
is.NoError(err)
is.Contains(output.String(), "BARTEST")
is.Contains(output.String(), "BARTEST1")
}

func TestNewExecWithTwoArgsRun(t *testing.T) {
if runtime.GOOS == "windows" {
// the actual Run test uses a basic sed example, so skip this test on windows
t.Skip("skipping on windows")
}
is := assert.New(t)
testpath, cleanup := setupTestingScript(t)
defer cleanup()

renderer, err := NewExec(testpath, "FOOTEST", "FOOTEST")
require.NoError(t, err)

output, err := renderer.Run(bytes.NewBufferString("FOOTEST"))
is.NoError(err)
is.Contains(output.String(), "BARTEST2")
}

func setupTestingScript(t *testing.T) (filepath string, cleanup func()) {
Expand Down

0 comments on commit d12170b

Please sign in to comment.