17503: the deduplication report tool should not require the Arvados
[arvados.git] / lib / deduplicationreport / report.go
index 663e734274b3cbafe1ebe55be9706983270e5b67..8109b5dbd22f9a09684bd92efeeaf92eee7497b8 100644 (file)
@@ -10,7 +10,6 @@ import (
        "io"
        "strings"
 
-       "git.arvados.org/arvados.git/lib/config"
        "git.arvados.org/arvados.git/sdk/go/arvados"
        "git.arvados.org/arvados.git/sdk/go/arvadosclient"
        "git.arvados.org/arvados.git/sdk/go/manifest"
@@ -30,7 +29,7 @@ func deDuplicate(inputs []string) (trimmed []string) {
        return
 }
 
-func parseFlags(prog string, args []string, loader *config.Loader, logger *logrus.Logger, stderr io.Writer) (exitcode int, inputs []string) {
+func parseFlags(prog string, args []string, logger *logrus.Logger, stderr io.Writer) (exitcode int, inputs []string) {
        flags := flag.NewFlagSet("", flag.ContinueOnError)
        flags.SetOutput(stderr)
        flags.Usage = func() {
@@ -60,18 +59,17 @@ Example:
 
   arv collection list --order 'file_size_total desc' --limit 100 | \
     jq -r '.items[] | [.portable_data_hash,.uuid] |@csv' | \
-    tail -n+2 |sed -e 's/"//g'|tr '\n' ' ' | \
+    sed -e 's/"//g'|tr '\n' ' ' | \
     xargs %s
 
 Options:
 `, prog, prog, prog)
                flags.PrintDefaults()
        }
-       loader.SetupFlags(flags)
        loglevel := flags.String("log-level", "info", "logging level (debug, info, ...)")
        err := flags.Parse(args)
        if err == flag.ErrHelp {
-               return 0, inputs
+               return 1, inputs
        } else if err != nil {
                return 2, inputs
        }
@@ -81,7 +79,7 @@ Options:
        inputs = deDuplicate(inputs)
 
        if len(inputs) < 1 {
-               logger.Errorf("Error: no collections provided\n")
+               logger.Errorf("Error: no collections provided")
                flags.Usage()
                return 2, inputs
        }
@@ -104,18 +102,22 @@ func blockList(collection arvados.Collection) (blocks map[string]int) {
        return
 }
 
-func report(prog string, args []string, loader *config.Loader, logger *logrus.Logger, stdout, stderr io.Writer) (exitcode int) {
+func report(prog string, args []string, logger *logrus.Logger, stdout, stderr io.Writer) (exitcode int) {
 
        var inputs []string
-       exitcode, inputs = parseFlags(prog, args, loader, logger, stderr)
+       exitcode, inputs = parseFlags(prog, args, logger, stderr)
        if exitcode != 0 {
+               if exitcode == 1 {
+                       // Asking for the cli help should not result in a non-zero exit code
+                       exitcode = 0
+               }
                return
        }
 
        // Arvados Client setup
        arv, err := arvadosclient.MakeArvadosClient()
        if err != nil {
-               logger.Errorf("Error creating Arvados object: %s\n", err)
+               logger.Errorf("Error creating Arvados object: %s", err)
                exitcode = 1
                return
        }
@@ -142,7 +144,7 @@ func report(prog string, args []string, loader *config.Loader, logger *logrus.Lo
                        uuid = input
                }
                if !strings.Contains(uuid, "-4zz18-") {
-                       logger.Errorf("Error: uuid must refer to collection object\n")
+                       logger.Errorf("Error: uuid must refer to collection object")
                        exitcode = 1
                        return
                }
@@ -157,14 +159,14 @@ func report(prog string, args []string, loader *config.Loader, logger *logrus.Lo
                        var collection arvados.Collection
                        err = arv.Get("collections", uuid, nil, &collection)
                        if err != nil {
-                               logger.Errorf("Error: unable to retrieve collection: %s\n", err)
+                               logger.Errorf("Error: unable to retrieve collection: %s", err)
                                exitcode = 1
                                return
                        }
                        blocks[uuid] = make(map[string]int)
                        blocks[uuid] = blockList(collection)
                        if pdh != "" && collection.PortableDataHash != pdh {
-                               logger.Errorf("Error: the collection with UUID %s has PDH %s, but a different PDH was provided in the arguments: %s\n", uuid, collection.PortableDataHash, pdh)
+                               logger.Errorf("Error: the collection with UUID %s has PDH %s, but a different PDH was provided in the arguments: %s", uuid, collection.PortableDataHash, pdh)
                                exitcode = 1
                                return
                        }