Skip to content

Commit 0b80129

Browse files
authored
CLOUDP-114507: [MongoCLI] mongocli config --service ops-manager is not adding ops_manager_url to config (#1017)
1 parent ce89412 commit 0b80129

File tree

8 files changed

+72
-32
lines changed

8 files changed

+72
-32
lines changed

internal/cli/atlas/config/init.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
const atlas = "atlas"
3232

3333
type initOpts struct {
34-
cli.ConfigOpts
34+
cli.DigestConfigOpts
3535
gov bool
3636
}
3737

@@ -109,6 +109,10 @@ func InitBuilder() *cobra.Command {
109109
To configure the tool to work with Atlas for Government
110110
$ atlas config init --gov
111111
`,
112+
113+
PreRun: func(cmd *cobra.Command, args []string) {
114+
opts.OutWriter = cmd.OutOrStdout()
115+
},
112116
RunE: func(cmd *cobra.Command, args []string) error {
113117
return opts.Run(cmd.Context())
114118
},

internal/cli/auth/login.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"context"
1919
"errors"
2020
"fmt"
21-
"io"
2221
"os"
2322
"time"
2423

@@ -49,7 +48,6 @@ type LoginConfig interface {
4948

5049
type loginOpts struct {
5150
cli.DefaultSetterOpts
52-
OutWriter io.Writer
5351
AccessToken string
5452
RefreshToken string
5553
isGov bool
@@ -66,7 +64,7 @@ func (opts *loginOpts) initFlow() error {
6664
return err
6765
}
6866

69-
func (opts *loginOpts) SetUpAccess() {
67+
func (opts *loginOpts) SetOAuthUpAccess() {
7068
switch {
7169
case opts.isGov:
7270
opts.Service = config.CloudGovService
@@ -95,7 +93,7 @@ func (opts *loginOpts) Run(ctx context.Context) error {
9593
if err := opts.oauthFlow(ctx); err != nil {
9694
return err
9795
}
98-
opts.SetUpAccess()
96+
opts.SetOAuthUpAccess()
9997
s, err := opts.config.AccessTokenSubject()
10098
if err != nil {
10199
return err
@@ -172,12 +170,12 @@ Your code will expire after %.0f minutes.
172170

173171
func (opts *loginOpts) askOrg() error {
174172
oMap, oSlice, err := opts.Orgs()
175-
var orgID string
176173
if err != nil || len(oSlice) == 0 {
177174
return errors.New("no orgs")
178175
}
179176

180177
p := prompt.NewOrgSelect(oSlice)
178+
var orgID string
181179
if err := survey.AskOne(p, &orgID); err != nil {
182180
return err
183181
}
@@ -187,12 +185,12 @@ func (opts *loginOpts) askOrg() error {
187185

188186
func (opts *loginOpts) askProject() error {
189187
pMap, pSlice, err := opts.Projects()
190-
var projectID string
191188
if err != nil || len(pSlice) == 0 {
192189
return errors.New("no projects")
193190
}
194191

195192
p := prompt.NewProjectSelect(pSlice)
193+
var projectID string
196194
if err := survey.AskOne(p, &projectID); err != nil {
197195
return err
198196
}

internal/cli/auth/login_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ func Test_loginOpts_Run(t *testing.T) {
6060
opts := &loginOpts{
6161
flow: mockFlow,
6262
config: mockConfig,
63-
OutWriter: buf,
6463
noBrowser: true,
6564
loginOnly: true,
6665
}
66+
opts.OutWriter = buf
6767
opts.Store = mockStore
6868
expectedCode := &auth.DeviceCode{
6969
UserCode: "12345678",

internal/cli/config/config.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,11 @@ import (
2929
)
3030

3131
type opts struct {
32-
cli.ConfigOpts
33-
OpsManagerURL string
34-
}
35-
36-
func (opts *opts) SetUpAccess() {
37-
opts.SetUpServiceAndKeys()
38-
if opts.OpsManagerURL != "" {
39-
config.SetOpsManagerURL(opts.OpsManagerURL)
40-
}
32+
cli.DigestConfigOpts
4133
}
4234

4335
func (opts *opts) Run(ctx context.Context) error {
44-
fmt.Printf(`You are configuring a profile for %s.
36+
_, _ = fmt.Fprintf(opts.OutWriter, `You are configuring a profile for %s.
4537
4638
All values are optional and you can use environment variables (MCLI_*) instead.
4739
@@ -53,7 +45,7 @@ Enter [?] on any option to get help.
5345
if err := survey.Ask(q, opts); err != nil {
5446
return err
5547
}
56-
opts.SetUpAccess()
48+
opts.SetUpDigestAccess()
5749

5850
if err := opts.InitStore(ctx); err != nil {
5951
return err
@@ -85,16 +77,16 @@ Enter [?] on any option to get help.
8577
return err
8678
}
8779

88-
fmt.Printf("\nYour profile is now configured.\n")
80+
_, _ = fmt.Fprintf(opts.OutWriter, "\nYour profile is now configured.\n")
8981
if config.Name() != config.DefaultProfile {
90-
fmt.Printf("To use this profile, you must set the flag [-%s %s] for every command.\n", flag.ProfileShort, config.Name())
82+
_, _ = fmt.Fprintf(opts.OutWriter, "To use this profile, you must set the flag [-%s %s] for every command.\n", flag.ProfileShort, config.Name())
9183
}
92-
fmt.Printf("You can use [%s config set] to change these settings at a later time.\n", config.ToolName)
84+
_, _ = fmt.Fprintf(opts.OutWriter, "You can use [%s config set] to change these settings at a later time.\n", config.ToolName)
9385
return nil
9486
}
9587

9688
func Builder() *cobra.Command {
97-
opts := &opts{}
89+
opt := &opts{}
9890
cmd := &cobra.Command{
9991
Use: "config",
10092
Short: "Configure a profile to store access settings for your MongoDB deployment.",
@@ -117,15 +109,18 @@ To find out more, see the documentation: https://docs.mongodb.com/mongocli/stabl
117109
To configure the tool to work with Ops Manager
118110
$ mongocli config --service ops-manager
119111
`,
112+
PreRun: func(cmd *cobra.Command, args []string) {
113+
opt.OutWriter = cmd.OutOrStdout()
114+
},
120115
RunE: func(cmd *cobra.Command, args []string) error {
121-
return opts.Run(cmd.Context())
116+
return opt.Run(cmd.Context())
122117
},
123118
Annotations: map[string]string{
124119
"toc": "true",
125120
},
126121
Args: require.NoArgs,
127122
}
128-
cmd.Flags().StringVar(&opts.Service, flag.Service, config.CloudService, usage.Service)
123+
cmd.Flags().StringVar(&opt.Service, flag.Service, config.CloudService, usage.Service)
129124
cmd.AddCommand(
130125
SetBuilder(),
131126
ListBuilder(),

internal/cli/config/config_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2022 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//go:build unit
16+
// +build unit
17+
18+
package config
19+
20+
import (
21+
"testing"
22+
23+
"github.com/mongodb/mongocli/internal/flag"
24+
"github.com/mongodb/mongocli/internal/test"
25+
)
26+
27+
func TestBuilder(t *testing.T) {
28+
test.CmdValidator(
29+
t,
30+
Builder(),
31+
5,
32+
[]string{flag.Service},
33+
)
34+
}

internal/cli/config_opts.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ import (
2121
"github.com/mongodb/mongocli/internal/validate"
2222
)
2323

24-
type ConfigOpts struct {
24+
type DigestConfigOpts struct {
2525
DefaultSetterOpts
2626
PublicAPIKey string
2727
PrivateAPIKey string
2828
}
2929

30-
func (opts *ConfigOpts) SetUpServiceAndKeys() {
30+
func (opts *DigestConfigOpts) SetUpServiceAndKeys() {
3131
config.SetService(opts.Service)
3232
if opts.PublicAPIKey != "" {
3333
config.SetPublicAPIKey(opts.PublicAPIKey)
@@ -37,17 +37,24 @@ func (opts *ConfigOpts) SetUpServiceAndKeys() {
3737
}
3838
}
3939

40+
func (opts *DigestConfigOpts) SetUpDigestAccess() {
41+
opts.SetUpServiceAndKeys()
42+
if opts.OpsManagerURL != "" {
43+
config.SetOpsManagerURL(opts.OpsManagerURL)
44+
}
45+
}
46+
4047
// AskProject will try to construct a select based on fetched projects.
4148
// If it fails or there are no projects to show we fallback to ask for project by ID.
42-
func (opts *ConfigOpts) AskProject() error {
49+
func (opts *DigestConfigOpts) AskProject() error {
4350
pMap, pSlice, err := opts.Projects()
44-
var projectID string
4551
if err != nil || len(pSlice) == 0 {
4652
p := prompt.NewProjectIDInput()
4753
return survey.AskOne(p, &opts.ProjectID, survey.WithValidator(validate.OptionalObjectID))
4854
}
4955

5056
p := prompt.NewProjectSelect(pSlice)
57+
var projectID string
5158
if err := survey.AskOne(p, &projectID); err != nil {
5259
return err
5360
}
@@ -57,15 +64,15 @@ func (opts *ConfigOpts) AskProject() error {
5764

5865
// AskOrg will try to construct a select based on fetched organizations.
5966
// If it fails or there are no organizations to show we fallback to ask for org by ID.
60-
func (opts *ConfigOpts) AskOrg() error {
67+
func (opts *DigestConfigOpts) AskOrg() error {
6168
oMap, oSlice, err := opts.Orgs()
62-
var orgID string
6369
if err != nil || len(oSlice) == 0 {
6470
p := prompt.NewOrgIDInput()
6571
return survey.AskOne(p, &opts.OrgID, survey.WithValidator(validate.OptionalObjectID))
6672
}
6773

6874
p := prompt.NewOrgSelect(oSlice)
75+
var orgID string
6976
if err := survey.AskOne(p, &orgID); err != nil {
7077
return err
7178
}

internal/cli/default_setter_opts.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package cli
1717
import (
1818
"context"
1919
"fmt"
20+
"io"
2021
"os"
2122

2223
"github.com/AlecAivazis/survey/v2"
@@ -43,6 +44,7 @@ type DefaultSetterOpts struct {
4344
MongoShellPath string
4445
Output string
4546
Store ProjectOrgsLister
47+
OutWriter io.Writer
4648
}
4749

4850
func (opts *DefaultSetterOpts) InitStore(ctx context.Context) error {

internal/test/test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func CmdValidator(t *testing.T, subject *cobra.Command, nSubCommands int, flags
2727
t.Helper()
2828
a := assert.New(t)
2929
if len(subject.Commands()) != nSubCommands {
30-
t.Errorf("Sub command count mismatch. Expected %d, got %d. Check the CmdValidator for your command.\n", len(subject.Commands()), nSubCommands)
30+
t.Errorf("Sub command count mismatch. Expected %d, got %d. Check the CmdValidator for your command.\n", nSubCommands, len(subject.Commands()))
3131
}
3232
if len(flags) == 0 {
3333
a.False(subject.HasAvailableFlags(), "expected command to not have flags but it does")

0 commit comments

Comments
 (0)