From ff917e5a49fc6660fa2c048362761439c0ef3029 Mon Sep 17 00:00:00 2001 From: Dmitry Ukov Date: Mon, 2 Dec 2019 13:56:27 +0400 Subject: [PATCH] Add render sub-command frame Render command filters documents and prints them to user-defined output in a form of multi-document YAML. Sub-command receives following document filter flags: * label * annotation * apiVersion (a.k.a group-version) * kind * filter Related: #16 Change-Id: I7efb0a478e1070efd1791ab10d7c3946c8c28630 --- cmd/document/document.go | 1 + cmd/document/document_test.go | 26 ++++++++++++++ cmd/document/render.go | 34 +++++++++++++++++++ .../document-render-with-help.golden | 12 +++++++ .../document-with-defaults.golden | 15 ++++++++ pkg/document/render/type.go | 20 +++++++++++ 6 files changed, 108 insertions(+) create mode 100644 cmd/document/document_test.go create mode 100644 cmd/document/render.go create mode 100644 cmd/document/testdata/TestDocumentGoldenOutput/document-render-with-help.golden create mode 100644 cmd/document/testdata/TestDocumentGoldenOutput/document-with-defaults.golden create mode 100644 pkg/document/render/type.go diff --git a/cmd/document/document.go b/cmd/document/document.go index b9669d952..61f0735e3 100644 --- a/cmd/document/document.go +++ b/cmd/document/document.go @@ -16,6 +16,7 @@ func NewDocumentCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Com documentRootCmd.AddCommand(NewDocumentPullCommand(rootSettings)) documentRootCmd.AddCommand(secret.NewSecretCommand(rootSettings)) + documentRootCmd.AddCommand(NewRenderCommand(rootSettings)) return documentRootCmd } diff --git a/cmd/document/document_test.go b/cmd/document/document_test.go new file mode 100644 index 000000000..83a6db036 --- /dev/null +++ b/cmd/document/document_test.go @@ -0,0 +1,26 @@ +package document_test + +import ( + "testing" + + "opendev.org/airship/airshipctl/cmd/document" + "opendev.org/airship/airshipctl/testutil" +) + +func TestDocument(t *testing.T) { + tests := []*testutil.CmdTest{ + { + Name: "document-with-defaults", + CmdLine: "", + Cmd: document.NewDocumentCommand(nil), + }, + { + Name: "document-render-with-help", + CmdLine: "-h", + Cmd: document.NewRenderCommand(nil), + }, + } + for _, tt := range tests { + testutil.RunTest(t, tt) + } +} diff --git a/cmd/document/render.go b/cmd/document/render.go new file mode 100644 index 000000000..87a1d621b --- /dev/null +++ b/cmd/document/render.go @@ -0,0 +1,34 @@ +package document + +import ( + "github.com/spf13/cobra" + + "opendev.org/airship/airshipctl/pkg/document/render" + "opendev.org/airship/airshipctl/pkg/environment" + "opendev.org/airship/airshipctl/pkg/errors" +) + +// InitFlags add flags for document render sub-command +func initRenderFlags(settings *render.Settings, cmd *cobra.Command) { + flags := cmd.Flags() + flags.StringArrayVarP(&settings.Label, "label", "l", nil, "Filter documents by Labels") + flags.StringArrayVarP(&settings.Annotation, "annotation", "a", nil, "Filter documents by Annotations") + flags.StringArrayVarP(&settings.GroupVersion, "apiversion", "g", nil, "Filter documents by API version") + flags.StringArrayVarP(&settings.Kind, "kind", "k", nil, "Filter documents by Kinds") + flags.StringVarP(&settings.RawFilter, "filter", "f", "", "Logical expression for document filtering") +} + +// NewRenderCommand create a new command for document rendering +func NewRenderCommand(rootSettings *environment.AirshipCTLSettings) *cobra.Command { + renderSettings := &render.Settings{AirshipCTLSettings: rootSettings} + renderCmd := &cobra.Command{ + Use: "render", + Short: "Render documents from model", + RunE: func(cmd *cobra.Command, args []string) error { + return errors.ErrNotImplemented{} + }, + } + + initRenderFlags(renderSettings, renderCmd) + return renderCmd +} diff --git a/cmd/document/testdata/TestDocumentGoldenOutput/document-render-with-help.golden b/cmd/document/testdata/TestDocumentGoldenOutput/document-render-with-help.golden new file mode 100644 index 000000000..a00aa24a1 --- /dev/null +++ b/cmd/document/testdata/TestDocumentGoldenOutput/document-render-with-help.golden @@ -0,0 +1,12 @@ +Render documents from model + +Usage: + render [flags] + +Flags: + -a, --annotation stringArray Filter documents by Annotations + -g, --apiversion stringArray Filter documents by API version + -f, --filter string Logical expression for document filtering + -h, --help help for render + -k, --kind stringArray Filter documents by Kinds + -l, --label stringArray Filter documents by Labels diff --git a/cmd/document/testdata/TestDocumentGoldenOutput/document-with-defaults.golden b/cmd/document/testdata/TestDocumentGoldenOutput/document-with-defaults.golden new file mode 100644 index 000000000..19aba23f7 --- /dev/null +++ b/cmd/document/testdata/TestDocumentGoldenOutput/document-with-defaults.golden @@ -0,0 +1,15 @@ +manages deployment documents + +Usage: + document [command] + +Available Commands: + help Help about any command + pull pulls documents from remote git repository + render Render documents from model + secret manages secrets + +Flags: + -h, --help help for document + +Use "document [command] --help" for more information about a command. diff --git a/pkg/document/render/type.go b/pkg/document/render/type.go new file mode 100644 index 000000000..99707b2f1 --- /dev/null +++ b/pkg/document/render/type.go @@ -0,0 +1,20 @@ +package render + +import ( + "opendev.org/airship/airshipctl/pkg/environment" +) + +// Settings for document rendering +type Settings struct { + *environment.AirshipCTLSettings + // Label filter documents by label string + Label []string + // Annotation filter documents by annotation string + Annotation []string + // GroupVersion filter documents by API version + GroupVersion []string + // Kind filter documents by document kind + Kind []string + // RawFilter contains logical expression to filter documents + RawFilter string +}