Merge branch '18324-lsf-gpu' refs #18324
[arvados.git] / lib / config / cmd.go
index 7889d85c8047b08cfdb1048791b28dddeb7ed360..eeab6ac8cd0dfad10f82f97880db0187f3e412bd 100644 (file)
@@ -12,7 +12,8 @@ import (
        "os"
        "os/exec"
 
-       "git.curoverse.com/arvados.git/sdk/go/ctxlog"
+       "git.arvados.org/arvados.git/lib/cmd"
+       "git.arvados.org/arvados.git/sdk/go/ctxlog"
        "github.com/ghodss/yaml"
        "github.com/sirupsen/logrus"
 )
@@ -35,22 +36,11 @@ func (dumpCommand) RunCommand(prog string, args []string, stdin io.Reader, stdou
        }
 
        flags := flag.NewFlagSet("", flag.ContinueOnError)
-       flags.SetOutput(stderr)
        loader.SetupFlags(flags)
 
-       err = flags.Parse(args)
-       if err == flag.ErrHelp {
-               err = nil
-               return 0
-       } else if err != nil {
-               return 2
+       if ok, code := cmd.ParseFlags(flags, prog, args, "", stderr); !ok {
+               return code
        }
-
-       if len(flags.Args()) != 0 {
-               flags.Usage()
-               return 2
-       }
-
        cfg, err := loader.Load()
        if err != nil {
                return 1
@@ -87,29 +77,32 @@ func (checkCommand) RunCommand(prog string, args []string, stdin io.Reader, stdo
                Logger: logger,
        }
 
-       flags := flag.NewFlagSet("", flag.ContinueOnError)
-       flags.SetOutput(stderr)
+       flags := flag.NewFlagSet(prog, flag.ContinueOnError)
        loader.SetupFlags(flags)
-
-       err = flags.Parse(args)
-       if err == flag.ErrHelp {
-               err = nil
-               return 0
-       } else if err != nil {
-               return 2
-       }
-
-       if len(flags.Args()) != 0 {
-               flags.Usage()
-               return 2
+       strict := flags.Bool("strict", true, "Strict validation of configuration file (warnings result in non-zero exit code)")
+       if ok, code := cmd.ParseFlags(flags, prog, args, "", stderr); !ok {
+               return code
        }
 
+       // Load the config twice -- once without loading deprecated
+       // keys/files, once with -- and then compare the two resulting
+       // configs. This reveals whether the deprecated keys/files
+       // have any effect on the final configuration.
+       //
+       // If they do, show the operator how to update their config
+       // such that the deprecated keys/files are superfluous and can
+       // be deleted.
        loader.SkipDeprecated = true
+       loader.SkipLegacy = true
        withoutDepr, err := loader.Load()
        if err != nil {
                return 1
        }
+       // Reset() to avoid printing the same warnings twice when they
+       // are logged by both without-legacy and with-legacy loads.
+       logbuf.Reset()
        loader.SkipDeprecated = false
+       loader.SkipLegacy = false
        withDepr, err := loader.Load()
        if err != nil {
                return 1
@@ -133,15 +126,21 @@ func (checkCommand) RunCommand(prog string, args []string, stdin io.Reader, stdo
                fmt.Fprintln(stdout, "Your configuration is relying on deprecated entries. Suggest making the following changes.")
                stdout.Write(diff)
                err = nil
-               return 1
+               if *strict {
+                       return 1
+               }
        } else if len(diff) > 0 {
                fmt.Fprintf(stderr, "Unexpected diff output:\n%s", diff)
-               return 1
+               if *strict {
+                       return 1
+               }
        } else if err != nil {
                return 1
        }
        if logbuf.Len() > 0 {
-               return 1
+               if *strict {
+                       return 1
+               }
        }
        return 0
 }