1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.arvados.org/arvados.git/lib/cmd"
16 "git.arvados.org/arvados.git/sdk/go/ctxlog"
17 "github.com/ghodss/yaml"
18 "github.com/sirupsen/logrus"
21 var DumpCommand dumpCommand
23 type dumpCommand struct{}
25 func (dumpCommand) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
29 fmt.Fprintf(stderr, "%s\n", err)
35 Logger: ctxlog.New(stderr, "text", "info"),
38 flags := flag.NewFlagSet("", flag.ContinueOnError)
39 loader.SetupFlags(flags)
41 if ok, code := cmd.ParseFlags(flags, prog, args, "", stderr); !ok {
44 cfg, err := loader.Load()
48 out, err := yaml.Marshal(cfg)
52 _, err = stdout.Write(out)
59 var CheckCommand checkCommand
61 type checkCommand struct{}
63 func (checkCommand) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
65 var logbuf = &bytes.Buffer{}
67 io.Copy(stderr, logbuf)
69 fmt.Fprintf(stderr, "%s\n", err)
73 logger := logrus.New()
80 flags := flag.NewFlagSet(prog, flag.ContinueOnError)
81 loader.SetupFlags(flags)
82 strict := flags.Bool("strict", true, "Strict validation of configuration file (warnings result in non-zero exit code)")
83 if ok, code := cmd.ParseFlags(flags, prog, args, "", stderr); !ok {
87 // Load the config twice -- once without loading deprecated
88 // keys/files, once with -- and then compare the two resulting
89 // configs. This reveals whether the deprecated keys/files
90 // have any effect on the final configuration.
92 // If they do, show the operator how to update their config
93 // such that the deprecated keys/files are superfluous and can
95 loader.SkipDeprecated = true
96 loader.SkipLegacy = true
97 withoutDepr, err := loader.Load()
101 // Reset() to avoid printing the same warnings twice when they
102 // are logged by both without-legacy and with-legacy loads.
104 loader.SkipDeprecated = false
105 loader.SkipLegacy = false
106 withDepr, err := loader.Load()
110 cmd := exec.Command("diff", "-u", "--label", "without-deprecated-configs", "--label", "relying-on-deprecated-configs", "/dev/fd/3", "/dev/fd/4")
111 for _, obj := range []interface{}{withoutDepr, withDepr} {
112 y, _ := yaml.Marshal(obj)
113 pr, pw, err := os.Pipe()
119 io.Copy(pw, bytes.NewBuffer(y))
122 cmd.ExtraFiles = append(cmd.ExtraFiles, pr)
124 diff, err := cmd.CombinedOutput()
125 if bytes.HasPrefix(diff, []byte("--- ")) {
126 fmt.Fprintln(stdout, "Your configuration is relying on deprecated entries. Suggest making the following changes.")
132 } else if len(diff) > 0 {
133 fmt.Fprintf(stderr, "Unexpected diff output:\n%s", diff)
137 } else if err != nil {
140 if logbuf.Len() > 0 {
148 var DumpDefaultsCommand defaultsCommand
150 type defaultsCommand struct{}
152 func (defaultsCommand) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
153 _, err := stdout.Write(DefaultYAML)
155 fmt.Fprintln(stderr, err)