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 {
49 if len(flags.Args()) != 0 {
54 cfg, err := loader.Load()
58 out, err := yaml.Marshal(cfg)
62 _, err = stdout.Write(out)
69 var CheckCommand checkCommand
71 type checkCommand struct{}
73 func (checkCommand) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
75 var logbuf = &bytes.Buffer{}
77 io.Copy(stderr, logbuf)
79 fmt.Fprintf(stderr, "%s\n", err)
83 logger := logrus.New()
90 flags := flag.NewFlagSet("", flag.ContinueOnError)
91 flags.SetOutput(stderr)
92 loader.SetupFlags(flags)
93 strict := flags.Bool("strict", true, "Strict validation of configuration file (warnings result in non-zero exit code)")
95 err = flags.Parse(args)
96 if err == flag.ErrHelp {
99 } else if err != nil {
103 if len(flags.Args()) != 0 {
108 // Load the config twice -- once without loading deprecated
109 // keys/files, once with -- and then compare the two resulting
110 // configs. This reveals whether the deprecated keys/files
111 // have any effect on the final configuration.
113 // If they do, show the operator how to update their config
114 // such that the deprecated keys/files are superfluous and can
116 loader.SkipDeprecated = true
117 loader.SkipLegacy = true
118 withoutDepr, err := loader.Load()
122 // Reset() to avoid printing the same warnings twice when they
123 // are logged by both without-legacy and with-legacy loads.
125 loader.SkipDeprecated = false
126 loader.SkipLegacy = false
127 withDepr, err := loader.Load()
131 cmd := exec.Command("diff", "-u", "--label", "without-deprecated-configs", "--label", "relying-on-deprecated-configs", "/dev/fd/3", "/dev/fd/4")
132 for _, obj := range []interface{}{withoutDepr, withDepr} {
133 y, _ := yaml.Marshal(obj)
134 pr, pw, err := os.Pipe()
140 io.Copy(pw, bytes.NewBuffer(y))
143 cmd.ExtraFiles = append(cmd.ExtraFiles, pr)
145 diff, err := cmd.CombinedOutput()
146 if bytes.HasPrefix(diff, []byte("--- ")) {
147 fmt.Fprintln(stdout, "Your configuration is relying on deprecated entries. Suggest making the following changes.")
153 } else if len(diff) > 0 {
154 fmt.Fprintf(stderr, "Unexpected diff output:\n%s", diff)
158 } else if err != nil {
161 if logbuf.Len() > 0 {
169 var DumpDefaultsCommand defaultsCommand
171 type defaultsCommand struct{}
173 func (defaultsCommand) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
174 _, err := stdout.Write(DefaultYAML)
176 fmt.Fprintln(stderr, err)