From 50d3c3897f0fad1bfcc4fc86096155d15d25483e Mon Sep 17 00:00:00 2001 From: Tom Clegg Date: Tue, 16 Apr 2019 15:33:33 -0400 Subject: [PATCH] 15003: Add 'arvados-server dump-config' command. Arvados-DCO-1.1-Signed-off-by: Tom Clegg --- cmd/arvados-server/cmd.go | 2 ++ lib/config/cmd.go | 45 +++++++++++++++++++++++++++++++++++++++ lib/config/cmd_test.go | 44 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 lib/config/cmd.go create mode 100644 lib/config/cmd_test.go diff --git a/cmd/arvados-server/cmd.go b/cmd/arvados-server/cmd.go index cd15d25dda..cad540c91b 100644 --- a/cmd/arvados-server/cmd.go +++ b/cmd/arvados-server/cmd.go @@ -8,6 +8,7 @@ import ( "os" "git.curoverse.com/arvados.git/lib/cmd" + "git.curoverse.com/arvados.git/lib/config" "git.curoverse.com/arvados.git/lib/controller" "git.curoverse.com/arvados.git/lib/dispatchcloud" ) @@ -21,6 +22,7 @@ var ( "controller": controller.Command, "dispatch-cloud": dispatchcloud.Command, + "dump-config": config.DumpCommand, }) ) diff --git a/lib/config/cmd.go b/lib/config/cmd.go new file mode 100644 index 0000000000..aa3e3bca12 --- /dev/null +++ b/lib/config/cmd.go @@ -0,0 +1,45 @@ +// Copyright (C) The Arvados Authors. All rights reserved. +// +// SPDX-License-Identifier: AGPL-3.0 + +package config + +import ( + "fmt" + "io" + + "git.curoverse.com/arvados.git/lib/cmd" + "git.curoverse.com/arvados.git/sdk/go/ctxlog" + "github.com/ghodss/yaml" +) + +var DumpCommand cmd.Handler = dumpCommand{} + +type dumpCommand struct{} + +func (dumpCommand) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int { + var err error + defer func() { + if err != nil { + fmt.Fprintf(stderr, "%s\n", err) + } + }() + if len(args) != 0 { + err = fmt.Errorf("usage: %s config-min.yaml", prog) + return 2 + } + log := ctxlog.New(stderr, "text", "info") + cfg, err := Load(stdin, log) + if err != nil { + return 1 + } + out, err := yaml.Marshal(cfg) + if err != nil { + return 1 + } + _, err = stdout.Write(out) + if err != nil { + return 1 + } + return 0 +} diff --git a/lib/config/cmd_test.go b/lib/config/cmd_test.go new file mode 100644 index 0000000000..0a60c25b57 --- /dev/null +++ b/lib/config/cmd_test.go @@ -0,0 +1,44 @@ +// Copyright (C) The Arvados Authors. All rights reserved. +// +// SPDX-License-Identifier: AGPL-3.0 + +package config + +import ( + "bytes" + + check "gopkg.in/check.v1" +) + +var _ = check.Suite(&CommandSuite{}) + +type CommandSuite struct{} + +func (s *CommandSuite) TestBadArg(c *check.C) { + var stderr bytes.Buffer + code := DumpCommand.RunCommand("arvados dump-config", []string{"-badarg"}, bytes.NewBuffer(nil), bytes.NewBuffer(nil), &stderr) + c.Check(code, check.Equals, 2) + c.Check(stderr.String(), check.Matches, `(?ms)usage: .*`) +} + +func (s *CommandSuite) TestEmptyInput(c *check.C) { + var stdout, stderr bytes.Buffer + code := DumpCommand.RunCommand("arvados dump-config", nil, &bytes.Buffer{}, &stdout, &stderr) + c.Check(code, check.Equals, 1) + c.Check(stderr.String(), check.Matches, `config does not define any clusters\n`) +} + +func (s *CommandSuite) TestUnknownKey(c *check.C) { + var stdout, stderr bytes.Buffer + in := ` +Clusters: + z1234: + UnknownKey: foobar + ManagementToken: secret +` + code := DumpCommand.RunCommand("arvados dump-config", nil, bytes.NewBufferString(in), &stdout, &stderr) + c.Check(code, check.Equals, 0) + c.Check(stdout.String(), check.Matches, `(?ms)Clusters:\n z1234:\n.*`) + c.Check(stdout.String(), check.Matches, `(?ms).*\n *ManagementToken: secret\n.*`) + c.Check(stdout.String(), check.Not(check.Matches), `(?ms).*UnknownKey.*`) +} -- 2.30.2