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) {
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 {
@ -126,24 +126,24 @@ 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
}
@ -151,37 +151,46 @@ func (p postRendererExecFlag) Set(s string) error {
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 {

@ -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

@ -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) {
@ -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")
@ -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()) {

Loading…
Cancel
Save