diff --git a/internal/cli/ops_manager_automation.go b/internal/cli/ops_manager_automation.go index e76b6bd5d0..10e3d18780 100644 --- a/internal/cli/ops_manager_automation.go +++ b/internal/cli/ops_manager_automation.go @@ -26,5 +26,6 @@ func OpsManagerAutomationBuilder() *cobra.Command { } cmd.AddCommand(OpsManagerAutomationStatusBuilder()) + cmd.AddCommand(OpsManagerAutomationShowBuilder()) return cmd } diff --git a/internal/cli/ops_manager_automation_show.go b/internal/cli/ops_manager_automation_show.go new file mode 100644 index 0000000000..181122bb59 --- /dev/null +++ b/internal/cli/ops_manager_automation_show.go @@ -0,0 +1,69 @@ +// Copyright 2020 MongoDB Inc +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cli + +import ( + "github.com/mongodb/mongocli/internal/flags" + "github.com/mongodb/mongocli/internal/json" + "github.com/mongodb/mongocli/internal/store" + "github.com/mongodb/mongocli/internal/usage" + "github.com/spf13/cobra" +) + +type opsManagerAutomationShowOpts struct { + *globalOpts + store store.AutomationGetter +} + +func (opts *opsManagerAutomationShowOpts) init() error { + if opts.ProjectID() == "" { + return errMissingProjectID + } + var err error + opts.store, err = store.New() + return err +} + +func (opts *opsManagerAutomationShowOpts) Run() error { + r, err := opts.store.GetAutomationConfig(opts.projectID) + + if err != nil { + return err + } + + return json.PrettyPrint(r) +} + +// mongocli ops-manager automation show [--projectId projectId] +func OpsManagerAutomationShowBuilder() *cobra.Command { + opts := &opsManagerAutomationShowOpts{ + globalOpts: newGlobalOpts(), + } + cmd := &cobra.Command{ + Use: "show", + Args: cobra.NoArgs, + Hidden: true, + PreRunE: func(cmd *cobra.Command, args []string) error { + return opts.init() + }, + RunE: func(cmd *cobra.Command, args []string) error { + return opts.Run() + }, + } + + cmd.Flags().StringVar(&opts.projectID, flags.ProjectID, "", usage.ProjectID) + + return cmd +} diff --git a/internal/cli/ops_manager_automation_show_test.go b/internal/cli/ops_manager_automation_show_test.go new file mode 100644 index 0000000000..491537caf0 --- /dev/null +++ b/internal/cli/ops_manager_automation_show_test.go @@ -0,0 +1,48 @@ +// Copyright 2020 MongoDB Inc +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cli + +import ( + "testing" + + "github.com/golang/mock/gomock" + "github.com/mongodb/mongocli/internal/fixtures" + "github.com/mongodb/mongocli/internal/mocks" +) + +func TestOpsManagerAutomationShowOpts_Run(t *testing.T) { + ctrl := gomock.NewController(t) + mockStore := mocks.NewMockAutomationGetter(ctrl) + + defer ctrl.Finish() + + expected := fixtures.AutomationConfig() + + opts := &opsManagerAutomationShowOpts{ + globalOpts: newGlobalOpts(), + store: mockStore, + } + + mockStore. + EXPECT(). + GetAutomationConfig(opts.projectID). + Return(expected, nil). + Times(1) + + err := opts.Run() + if err != nil { + t.Fatalf("Run() unexpected error: %v", err) + } +}