1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.arvados.org/arvados.git/sdk/go/ctxlog"
16 "github.com/ghodss/yaml"
17 "github.com/sirupsen/logrus"
20 var DumpCommand dumpCommand
22 type dumpCommand struct{}
24 func (dumpCommand) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
28 fmt.Fprintf(stderr, "%s\n", err)
34 Logger: ctxlog.New(stderr, "text", "info"),
37 flags := flag.NewFlagSet("", flag.ContinueOnError)
38 flags.SetOutput(stderr)
39 loader.SetupFlags(flags)
41 err = flags.Parse(args)
42 if err == flag.ErrHelp {
45 } else if err != nil {
47 } else if flags.NArg() != 0 {
48 err = fmt.Errorf("unrecognized command line arguments: %v", flags.Args())
52 cfg, err := loader.Load()
56 out, err := yaml.Marshal(cfg)
60 _, err = stdout.Write(out)
67 var CheckCommand checkCommand
69 type checkCommand struct{}
71 func (checkCommand) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
73 var logbuf = &bytes.Buffer{}
75 io.Copy(stderr, logbuf)
77 fmt.Fprintf(stderr, "%s\n", err)
81 logger := logrus.New()
88 flags := flag.NewFlagSet("", flag.ContinueOnError)
89 flags.SetOutput(stderr)
90 loader.SetupFlags(flags)
91 strict := flags.Bool("strict", true, "Strict validation of configuration file (warnings result in non-zero exit code)")
93 err = flags.Parse(args)
94 if err == flag.ErrHelp {
97 } else if err != nil {
99 } else if flags.NArg() != 0 {
100 err = fmt.Errorf("unrecognized command line arguments: %v", flags.Args())
104 // Load the config twice -- once without loading deprecated
105 // keys/files, once with -- and then compare the two resulting
106 // configs. This reveals whether the deprecated keys/files
107 // have any effect on the final configuration.
109 // If they do, show the operator how to update their config
110 // such that the deprecated keys/files are superfluous and can
112 loader.SkipDeprecated = true
113 loader.SkipLegacy = true
114 withoutDepr, err := loader.Load()
118 // Reset() to avoid printing the same warnings twice when they
119 // are logged by both without-legacy and with-legacy loads.
121 loader.SkipDeprecated = false
122 loader.SkipLegacy = false
123 withDepr, err := loader.Load()
127 cmd := exec.Command("diff", "-u", "--label", "without-deprecated-configs", "--label", "relying-on-deprecated-configs", "/dev/fd/3", "/dev/fd/4")
128 for _, obj := range []interface{}{withoutDepr, withDepr} {
129 y, _ := yaml.Marshal(obj)
130 pr, pw, err := os.Pipe()
136 io.Copy(pw, bytes.NewBuffer(y))
139 cmd.ExtraFiles = append(cmd.ExtraFiles, pr)
141 diff, err := cmd.CombinedOutput()
142 if bytes.HasPrefix(diff, []byte("--- ")) {
143 fmt.Fprintln(stdout, "Your configuration is relying on deprecated entries. Suggest making the following changes.")
149 } else if len(diff) > 0 {
150 fmt.Fprintf(stderr, "Unexpected diff output:\n%s", diff)
154 } else if err != nil {
157 if logbuf.Len() > 0 {
165 var DumpDefaultsCommand defaultsCommand
167 type defaultsCommand struct{}
169 func (defaultsCommand) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
170 _, err := stdout.Write(DefaultYAML)
172 fmt.Fprintln(stderr, err)