diff --git a/cmd/find.go b/cmd/find.go index d041cf0e..2ae77b7d 100644 --- a/cmd/find.go +++ b/cmd/find.go @@ -30,6 +30,7 @@ import ( "time" "github.com/dustin/go-humanize" + "github.com/google/shlex" "github.com/minio/mc/pkg/probe" "github.com/minio/pkg/console" @@ -114,16 +115,28 @@ func getExitStatus(err error) int { // execFind executes the input command line, additionally formats input // for the command line in accordance with subsititution arguments. -func execFind(command string) { - commandArgs := strings.Split(command, " ") - - cmd := exec.Command(commandArgs[0], commandArgs[1:]...) +func execFind(ctx context.Context, args string, fileContent contentMessage) { + split, err := shlex.Split(args) + if err != nil { + console.Println(console.Colorize("FindExecErr", "Unable to parse --exec: "+err.Error())) + os.Exit(getExitStatus(err)) + } + if len(split) == 0 { + return + } + for i, arg := range split { + split[i] = stringsReplace(ctx, arg, fileContent) + } + cmd := exec.Command(split[0], split[1:]...) var out bytes.Buffer var stderr bytes.Buffer cmd.Stdout = &out cmd.Stderr = &stderr if err := cmd.Run(); err != nil { - console.Print(console.Colorize("FindExecErr", stderr.String())) + if stderr.Len() > 0 { + console.Println(console.Colorize("FindExecErr", strings.TrimSpace(stderr.String()))) + } + console.Println(console.Colorize("FindExecErr", err.Error())) // Return exit status of the command run os.Exit(getExitStatus(err)) } @@ -230,7 +243,7 @@ func find(ctxCtx context.Context, ctx *findContext, fileContent contentMessage) // proceed to either exec, format the output string. if ctx.execCmd != "" { - execFind(stringsReplace(ctxCtx, ctx.execCmd, fileContent)) + execFind(ctxCtx, ctx.execCmd, fileContent) return } if ctx.printFmt != "" { @@ -290,7 +303,7 @@ func doFind(ctxCtx context.Context, ctx *findContext) error { // proceed to either exec, format the output string. if ctx.execCmd != "" { - execFind(stringsReplace(ctxCtx, ctx.execCmd, fileContent)) + execFind(ctxCtx, ctx.execCmd, fileContent) continue } if ctx.printFmt != "" { diff --git a/go.mod b/go.mod index 108a82da..94f2b5fe 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/dustin/go-humanize v1.0.0 github.com/fatih/color v1.13.0 github.com/golang-jwt/jwt v3.2.2+incompatible + github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 github.com/google/uuid v1.3.0 github.com/inconshreveable/mousetrap v1.0.0 github.com/json-iterator/go v1.1.12 diff --git a/go.sum b/go.sum index 01640842..fc737b94 100644 --- a/go.sum +++ b/go.sum @@ -175,6 +175,8 @@ github.com/google/pprof v0.0.0-20200229191704-1ebb73c60ed3/go.mod h1:ZgVRPoUq/hf github.com/google/pprof v0.0.0-20200430221834-fc25d7d30c6d/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=