Skip to content

Commit

Permalink
Fix and honor --json flag for find command.
Browse files Browse the repository at this point in the history
Currently `mc find` didn't honor `--json` flag properly,
this PR addresses that.

Additionally also take care of `List` returning errors
and handle them appropriately.

Fixes #2252
  • Loading branch information
harshavardhana committed Sep 27, 2017
1 parent 36d7d0b commit 14125ca
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
cover.out
./mc
*~
*.test
release
Expand All @@ -10,4 +9,5 @@ coverage.txt
parts/
prime/
snap/.snapcraft/
stage/
stage/
mc
3 changes: 2 additions & 1 deletion cmd/find-main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ var findCmd = cli.Command{
Name: "find",
Usage: "Finds files which match the given set of parameters.",
Action: mainFind,
Before: setGlobalsFromContext,
Flags: append(findFlags, globalFlags...),
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
Expand Down Expand Up @@ -189,7 +190,7 @@ func mainFind(ctx *cli.Context) error {
clnt, err := newClient(targetURL)
fatalIf(err.Trace(targetURL), "Unable to initialize `"+targetURL+"`")

DoFind(clnt, ctx)
doFind(clnt, ctx)

}
return cErr
Expand Down
53 changes: 33 additions & 20 deletions cmd/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cmd

import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
Expand All @@ -43,21 +42,17 @@ import (

// findMSG holds JSON and string values for printing
type findMSG struct {
Path string `json:"path"`
contentMessage
}

// String calls tells the console what to print and how to print it
func (f findMSG) String() string {
return console.Colorize("Find", f.Path)
return console.Colorize("Find", f.contentMessage.Key)
}

// JSON formats output to be JSON output
func (f findMSG) JSON() string {
f.Path = "path"
jsonMessageBytes, e := json.Marshal(f)
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")

return string(jsonMessageBytes)
return f.contentMessage.JSON()
}

// nameMatch pattern matches off of the base of the filepath
Expand All @@ -79,10 +74,8 @@ func regexMatch(path, pattern string) (bool, error) {

// doFindPrint prints the output in accordance with the supplied substitution arguments
func doFindPrint(path string, ctx *cli.Context, fileContent contentMessage) {
printString := SubArgsHelper(ctx.String("print"), path, fileContent)
printMsg(findMSG{
Path: printString,
})
fileContent.Key = SubArgsHelper(ctx.String("print"), path, fileContent)
printMsg(findMSG{fileContent})
}

// doFindExec passes the users input along to the command line, also dealing with substitution arguments
Expand Down Expand Up @@ -165,14 +158,38 @@ func doFindSmallerSize(size int64, pattern string) bool {
return int64(i) > size
}

// DoFind is used to handle most of the users input
func DoFind(clnt Client, ctx *cli.Context) {
// doFind is used to handle most of the users input
func doFind(clnt Client, ctx *cli.Context) {
pathnameParts := strings.SplitAfter(ctx.Args().Get(0), "/")
alias := strings.TrimSuffix(pathnameParts[0], "/")
_, err := getHostConfig(alias)

// iterate over all content which is within the given directory
doList(clnt, true, false)
for content := range clnt.List(true, false, DirNone) {
if content.Err != nil {
switch content.Err.ToGoError().(type) {
// handle this specifically for filesystem related errors.
case BrokenSymlink:
errorIf(content.Err.Trace(clnt.GetURL().String()), "Unable to list broken link.")
continue
case TooManyLevelsSymlink:
errorIf(content.Err.Trace(clnt.GetURL().String()), "Unable to list too many levels link.")
continue
case PathNotFound:
errorIf(content.Err.Trace(clnt.GetURL().String()), "Unable to list folder.")
continue
case PathInsufficientPermission:
errorIf(content.Err.Trace(clnt.GetURL().String()), "Unable to list folder.")
continue
case ObjectOnGlacier:
errorIf(content.Err.Trace(clnt.GetURL().String()), "")
continue
}
fatalIf(content.Err.Trace(clnt.GetURL().String()), "Unable to list folder.")
continue
}

fileContent := parseContent(content)
filePath := fileContent.Key

Expand Down Expand Up @@ -220,9 +237,7 @@ func DoFind(clnt Client, ctx *cli.Context) {
if match && ctx.String("print") != "" {
doFindPrint(filePath, ctx, fileContent)
} else if match {
printMsg(findMSG{
Path: filePath,
})
printMsg(findMSG{fileContent})
}

if !ctx.Bool("watch") && match && ctx.String("exec") != "" {
Expand Down Expand Up @@ -355,9 +370,7 @@ func watchEvents(ctx *cli.Context, clnt Client, params watchParams, alias string
if match && ctx.String("print") != "" {
doFindExec(ctx, msg.Key, fileContent)
} else if match {
printMsg(findMSG{
Path: msg.Key,
})
printMsg(findMSG{msg})
}

case err, ok := <-watchObj.Errors():
Expand Down

0 comments on commit 14125ca

Please sign in to comment.