fix: change postRendererArgs to Slice Type and use args...

Signed-off-by: guofutan <guofutan@tencent.com>
pull/10514/head
guofutan 3 years ago
parent 44423fb2ca
commit d12170b3f2

@ -116,8 +116,8 @@ func (o *outputValue) Set(s string) error {
func bindPostRenderFlag(cmd *cobra.Command, varRef *postrender.PostRenderer) { func bindPostRenderFlag(cmd *cobra.Command, varRef *postrender.PostRenderer) {
p := &postRendererOptions{varRef, "", []string{}} 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(&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(&postRendererArgsFlag{p}, postRenderArgsFlag, "the args to an executable to be used for post rendering. (can specify multiple)") cmd.Flags().Var(&postRendererArgsSlice{p}, postRenderArgsFlag, "the args to an executable to be used for post rendering. (can specify multiple)")
} }
type postRendererOptions struct { type postRendererOptions struct {
@ -126,24 +126,24 @@ type postRendererOptions struct {
args []string args []string
} }
type postRendererExecFlag struct { type postRendererString struct {
options *postRendererOptions options *postRendererOptions
} }
func (p postRendererExecFlag) String() string { func (p *postRendererString) String() string {
return "" return p.options.binaryPath
} }
func (p postRendererExecFlag) Type() string { func (p *postRendererString) Type() string {
return "postrenderer-exec" return "postRendererString"
} }
func (p postRendererExecFlag) Set(s string) error { func (p *postRendererString) Set(val string) error {
if s == "" { if val == "" {
return nil return nil
} }
p.options.binaryPath = s p.options.binaryPath = val
pr, err := postrender.NewExecWithArgs(p.options.binaryPath, p.options.args) pr, err := postrender.NewExec(p.options.binaryPath, p.options.args...)
if err != nil { if err != nil {
return err return err
} }
@ -151,37 +151,46 @@ func (p postRendererExecFlag) Set(s string) error {
return nil return nil
} }
type postRendererArgsFlag struct { type postRendererArgsSlice struct {
options *postRendererOptions options *postRendererOptions
} }
func (p postRendererArgsFlag) String() string { func (p *postRendererArgsSlice) String() string {
return "" return "[" + strings.Join(p.options.args, ",") + "]"
} }
func (p postRendererArgsFlag) Type() string { func (p *postRendererArgsSlice) Type() string {
return "postrenderer-args" return "postRendererArgsSlice"
} }
func (p postRendererArgsFlag) Set(s string) error { func (p *postRendererArgsSlice) Set(val string) error {
if s == "" { if val == "" || p.options.binaryPath == "" {
return nil return nil
} }
p.options.args = append(p.options.args, s) p.options.args = append(p.options.args, val)
// skip if postRenderFlag not set or parsed // overwrite if already create PostRenderer by `post-renderer` flags
if len(p.options.binaryPath) == 0 { pr, err := postrender.NewExec(p.options.binaryPath, p.options.args...)
return nil
}
// update if already create PostRenderer
pr, err := postrender.NewExecWithArgs(p.options.binaryPath, p.options.args)
if err != nil { if err != nil {
return err return err
} }
*p.options.renderer = pr *p.options.renderer = pr
return nil 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) { func compVersionFlag(chartRef string, toComplete string) ([]string, cobra.ShellCompDirective) {
chartInfo := strings.Split(chartRef, "/") chartInfo := strings.Split(chartRef, "/")
if len(chartInfo) != 2 { if len(chartInfo) != 2 {

@ -34,15 +34,7 @@ type execRender struct {
// It returns an error if the binary cannot be found. If the path does not // 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 // contain any separators, it will search in $PATH, otherwise it will resolve
// any relative paths to a fully qualified path // any relative paths to a fully qualified path
func NewExec(binaryPath string) (PostRenderer, error) { func NewExec(binaryPath string, args ...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) {
fullPath, err := getFullPath(binaryPath) fullPath, err := getFullPath(binaryPath)
if err != nil { if err != nil {
return nil, err return nil, err

@ -31,7 +31,11 @@ import (
) )
const testingScript = `#!/bin/sh const testingScript = `#!/bin/sh
if [ $# -eq 0 ]; then
sed s/FOOTEST/BARTEST/g <&0 sed s/FOOTEST/BARTEST/g <&0
else
sed s/FOOTEST/BARTEST$#/g <&0
fi
` `
func TestGetFullPath(t *testing.T) { func TestGetFullPath(t *testing.T) {
@ -124,7 +128,7 @@ func TestExecRun(t *testing.T) {
is.Contains(output.String(), "BARTEST") is.Contains(output.String(), "BARTEST")
} }
func TestNewExecWithArgsRun(t *testing.T) { func TestNewExecWithOneArgsRun(t *testing.T) {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
// the actual Run test uses a basic sed example, so skip this test on windows // the actual Run test uses a basic sed example, so skip this test on windows
t.Skip("skipping on windows") t.Skip("skipping on windows")
@ -133,12 +137,29 @@ func TestNewExecWithArgsRun(t *testing.T) {
testpath, cleanup := setupTestingScript(t) testpath, cleanup := setupTestingScript(t)
defer cleanup() defer cleanup()
renderer, err := NewExecWithArgs(testpath, []string{}) renderer, err := NewExec(testpath, "FOOTEST")
require.NoError(t, err) require.NoError(t, err)
output, err := renderer.Run(bytes.NewBufferString("FOOTEST")) output, err := renderer.Run(bytes.NewBufferString("FOOTEST"))
is.NoError(err) 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()) { func setupTestingScript(t *testing.T) (filepath string, cleanup func()) {

Loading…
Cancel
Save