Skip to content

Commit 7a48ec1

Browse files
authored
CLOUDP-69793: [mongocli] Improve output handling (#379)
1 parent 568bc47 commit 7a48ec1

File tree

118 files changed

+971
-444
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+971
-444
lines changed

cmd/mongocli/mongocli.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ func Execute() {
8181

8282
var (
8383
profile string
84-
output string
8584
)
8685

8786
func init() { //nolint:gochecknoinits // This is the cobra way
@@ -101,7 +100,7 @@ func init() { //nolint:gochecknoinits // This is the cobra way
101100
cobra.EnableCommandSorting = false
102101

103102
rootCmd.PersistentFlags().StringVarP(&profile, flag.Profile, flag.ProfileShort, "", usage.Profile)
104-
rootCmd.PersistentFlags().StringVarP(&output, flag.Output, flag.OutputShort, "", usage.FormatOut)
103+
105104
cobra.OnInitialize(initConfig)
106105
}
107106

@@ -117,7 +116,6 @@ func initConfig() {
117116
} else if len(availableProfiles) == 1 {
118117
config.SetName(availableProfiles[0])
119118
}
120-
config.SetOutput(output)
121119
}
122120

123121
func main() {

e2e/atlas/clusters_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func TestClustersFlags(t *testing.T) {
7070
clustersEntity,
7171
"watch",
7272
clusterName,
73-
"-o=json")
73+
)
7474
cmd.Env = os.Environ()
7575
resp, err := cmd.CombinedOutput()
7676

e2e/cloud_manager/dbusers_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"testing"
2727

2828
"github.com/mongodb/mongocli/e2e"
29-
"go.mongodb.org/atlas/mongodbatlas"
29+
"go.mongodb.org/ops-manager/opsmngr"
3030
)
3131

3232
func TestDBUsers(t *testing.T) {
@@ -77,8 +77,9 @@ func TestDBUsers(t *testing.T) {
7777
t.Fatalf("unexpected error: %v, resp: %v", err, string(resp))
7878
}
7979

80-
var users []mongodbatlas.DatabaseUser
80+
var users []opsmngr.MongoDBUser
8181
if err := json.Unmarshal(resp, &users); err != nil {
82+
t.Log(string(resp))
8283
t.Fatalf("unexpected error: %v", err)
8384
}
8485

e2e/cloud_manager/deploy_replica_set_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ func TestDeployReplicaSet(t *testing.T) {
9696
}
9797
var clusters []*convert.ClusterConfig
9898
if err := json.Unmarshal(resp, &clusters); err != nil {
99+
t.Log(string(resp))
99100
t.Fatalf("unexpected error: %v", err)
100101
}
101102

e2e/config/config_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ func TestConfig(t *testing.T) {
5454
t.Fatalf("unexpected error: %v, resp: %v", err, string(resp))
5555
}
5656
const expected = `org_id = 5e429e7706822c6eac4d5971
57-
output =
5857
public_api_key = redacted
5958
service = cloud
6059
`

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/fsnotify/fsnotify v1.4.9 // indirect
99
github.com/go-test/deep v1.0.7
1010
github.com/golang/mock v1.4.4
11+
github.com/mattn/go-isatty v0.0.8
1112
github.com/mitchellh/go-homedir v1.1.0
1213
github.com/mitchellh/mapstructure v1.3.1 // indirect
1314
github.com/mongodb-forks/digest v1.0.1

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
4848
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4949
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
5050
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
51+
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
5152
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
5253
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
5354
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=

internal/cli/alerts/acknowledge.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/mongodb/mongocli/internal/cli"
2222
"github.com/mongodb/mongocli/internal/config"
2323
"github.com/mongodb/mongocli/internal/flag"
24-
"github.com/mongodb/mongocli/internal/output"
2524
"github.com/mongodb/mongocli/internal/store"
2625
"github.com/mongodb/mongocli/internal/usage"
2726
"github.com/spf13/cobra"
@@ -30,6 +29,7 @@ import (
3029

3130
type AcknowledgeOpts struct {
3231
cli.GlobalOpts
32+
cli.OutputOpts
3333
alertID string
3434
until string
3535
comment string
@@ -52,7 +52,7 @@ func (opts *AcknowledgeOpts) Run() error {
5252
return err
5353
}
5454

55-
return output.Print(config.Default(), ackTemplate, r)
55+
return opts.Print(r)
5656
}
5757

5858
func (opts *AcknowledgeOpts) newAcknowledgeRequest() *atlas.AcknowledgeRequest {
@@ -71,6 +71,7 @@ func (opts *AcknowledgeOpts) newAcknowledgeRequest() *atlas.AcknowledgeRequest {
7171
// mongocli atlas alerts acknowledge <ID> --projectId projectId --forever --comment comment --until until
7272
func AcknowledgeBuilder() *cobra.Command {
7373
opts := new(AcknowledgeOpts)
74+
opts.Template = ackTemplate
7475
cmd := &cobra.Command{
7576
Use: "acknowledge <ID>",
7677
Short: acknowledgeAlerts,
@@ -80,19 +81,23 @@ func AcknowledgeBuilder() *cobra.Command {
8081
if opts.forever && opts.until != "" {
8182
return fmt.Errorf("--%s and --%s are exclusive", flag.Forever, flag.Until)
8283
}
83-
return opts.PreRunE(opts.initStore)
84+
return opts.PreRunE(
85+
opts.initStore,
86+
opts.InitOutput(cmd.OutOrStdout(), ackTemplate),
87+
)
8488
},
8589
RunE: func(cmd *cobra.Command, args []string) error {
8690
opts.alertID = args[0]
8791
return opts.Run()
8892
},
8993
}
90-
94+
cmd.OutOrStdout()
9195
cmd.Flags().BoolVarP(&opts.forever, flag.Forever, flag.ForeverShort, false, usage.Forever)
9296
cmd.Flags().StringVar(&opts.until, flag.Until, "", usage.Until)
9397
cmd.Flags().StringVar(&opts.comment, flag.Comment, "", usage.Comment)
9498

9599
cmd.Flags().StringVar(&opts.ProjectID, flag.ProjectID, "", usage.ProjectID)
100+
cmd.Flags().StringVarP(&opts.Output, flag.Output, flag.OutputShort, "", usage.FormatOut)
96101

97102
return cmd
98103
}

internal/cli/alerts/describe.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ import (
1818
"github.com/mongodb/mongocli/internal/cli"
1919
"github.com/mongodb/mongocli/internal/config"
2020
"github.com/mongodb/mongocli/internal/flag"
21-
"github.com/mongodb/mongocli/internal/output"
2221
"github.com/mongodb/mongocli/internal/store"
2322
"github.com/mongodb/mongocli/internal/usage"
2423
"github.com/spf13/cobra"
2524
)
2625

2726
type DescribeOpts struct {
2827
cli.GlobalOpts
28+
cli.OutputOpts
2929
alertID string
3030
store store.AlertDescriber
3131
}
@@ -47,7 +47,7 @@ func (opts *DescribeOpts) Run() error {
4747
return err
4848
}
4949

50-
return output.Print(config.Default(), describeTemplate, r)
50+
return opts.Print(r)
5151
}
5252

5353
// mongocli atlas alerts describe <ID> --projectId projectId
@@ -58,7 +58,10 @@ func DescribeBuilder() *cobra.Command {
5858
Short: describeAlert,
5959
Args: cobra.ExactArgs(1),
6060
PreRunE: func(cmd *cobra.Command, args []string) error {
61-
return opts.PreRunE(opts.initStore)
61+
return opts.PreRunE(
62+
opts.initStore,
63+
opts.InitOutput(cmd.OutOrStdout(), describeTemplate),
64+
)
6265
},
6366
RunE: func(cmd *cobra.Command, args []string) error {
6467
opts.alertID = args[0]
@@ -67,6 +70,7 @@ func DescribeBuilder() *cobra.Command {
6770
}
6871

6972
cmd.Flags().StringVar(&opts.ProjectID, flag.ProjectID, "", usage.ProjectID)
73+
cmd.Flags().StringVarP(&opts.Output, flag.Output, flag.OutputShort, "", usage.FormatOut)
7074

7175
return cmd
7276
}

internal/cli/alerts/global_list.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"github.com/mongodb/mongocli/internal/cli"
1919
"github.com/mongodb/mongocli/internal/config"
2020
"github.com/mongodb/mongocli/internal/flag"
21-
"github.com/mongodb/mongocli/internal/output"
2221
"github.com/mongodb/mongocli/internal/store"
2322
"github.com/mongodb/mongocli/internal/usage"
2423
"github.com/spf13/cobra"
@@ -27,6 +26,7 @@ import (
2726

2827
type GlobalListOpts struct {
2928
cli.ListOpts
29+
cli.OutputOpts
3030
status string
3131
store store.GlobalAlertLister
3232
}
@@ -44,7 +44,7 @@ func (opts *GlobalListOpts) Run() error {
4444
return err
4545
}
4646

47-
return output.Print(config.Default(), listTemplate, r)
47+
return opts.Print(r)
4848
}
4949

5050
func (opts *GlobalListOpts) newAlertsListOptions() *atlas.AlertsListOptions {
@@ -57,12 +57,14 @@ func (opts *GlobalListOpts) newAlertsListOptions() *atlas.AlertsListOptions {
5757
// mongocli om|cm alert(s) global list [--status status]
5858
func GlobalListBuilder() *cobra.Command {
5959
opts := &GlobalListOpts{}
60+
opts.Template = listTemplate
6061
cmd := &cobra.Command{
6162
Use: "list",
6263
Short: listGlobalAlerts,
6364
Aliases: []string{"ls"},
6465
Args: cobra.NoArgs,
6566
PreRunE: func(cmd *cobra.Command, args []string) error {
67+
opts.OutWriter = cmd.OutOrStdout()
6668
return opts.init()
6769
},
6870
RunE: func(cmd *cobra.Command, args []string) error {
@@ -74,5 +76,7 @@ func GlobalListBuilder() *cobra.Command {
7476
cmd.Flags().IntVar(&opts.ItemsPerPage, flag.Limit, 0, usage.Limit)
7577
cmd.Flags().StringVar(&opts.status, flag.Status, "", usage.Status)
7678

79+
cmd.Flags().StringVarP(&opts.Output, flag.Output, flag.OutputShort, "", usage.FormatOut)
80+
7781
return cmd
7882
}

internal/cli/alerts/list.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"github.com/mongodb/mongocli/internal/cli"
1919
"github.com/mongodb/mongocli/internal/config"
2020
"github.com/mongodb/mongocli/internal/flag"
21-
"github.com/mongodb/mongocli/internal/output"
2221
"github.com/mongodb/mongocli/internal/store"
2322
"github.com/mongodb/mongocli/internal/usage"
2423
"github.com/spf13/cobra"
@@ -27,6 +26,7 @@ import (
2726

2827
type ListOpts struct {
2928
cli.GlobalOpts
29+
cli.OutputOpts
3030
cli.ListOpts
3131
status string
3232
store store.AlertLister
@@ -50,7 +50,7 @@ func (opts *ListOpts) Run() error {
5050
return err
5151
}
5252

53-
return output.Print(config.Default(), listTemplate, r)
53+
return opts.Print(r)
5454
}
5555

5656
func (opts *ListOpts) newAlertsListOptions() *atlas.AlertsListOptions {
@@ -71,7 +71,10 @@ func ListBuilder() *cobra.Command {
7171
Aliases: []string{"ls"},
7272
Args: cobra.NoArgs,
7373
PreRunE: func(cmd *cobra.Command, args []string) error {
74-
return opts.PreRunE(opts.initStore)
74+
return opts.PreRunE(
75+
opts.initStore,
76+
opts.InitOutput(cmd.OutOrStdout(), listTemplate),
77+
)
7578
},
7679
RunE: func(cmd *cobra.Command, args []string) error {
7780
return opts.Run()
@@ -83,6 +86,7 @@ func ListBuilder() *cobra.Command {
8386
cmd.Flags().StringVar(&opts.status, flag.Status, "", usage.Status)
8487

8588
cmd.Flags().StringVar(&opts.ProjectID, flag.ProjectID, "", usage.ProjectID)
89+
cmd.Flags().StringVarP(&opts.Output, flag.Output, flag.OutputShort, "", usage.FormatOut)
8690

8791
return cmd
8892
}

internal/cli/alerts/settings/create.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ import (
1818
"github.com/mongodb/mongocli/internal/cli"
1919
"github.com/mongodb/mongocli/internal/config"
2020
"github.com/mongodb/mongocli/internal/flag"
21-
"github.com/mongodb/mongocli/internal/output"
2221
"github.com/mongodb/mongocli/internal/store"
2322
"github.com/mongodb/mongocli/internal/usage"
2423
"github.com/spf13/cobra"
2524
)
2625

2726
type CreateOpts struct {
2827
cli.GlobalOpts
28+
cli.OutputOpts
2929
ConfigOpts
3030
store store.AlertConfigurationCreator
3131
}
@@ -45,7 +45,7 @@ func (opts *CreateOpts) Run() error {
4545
return err
4646
}
4747

48-
return output.Print(config.Default(), createTemplate, r)
48+
return opts.Print(r)
4949
}
5050

5151
// mongocli atlas alerts config(s) create [--event event] [--enabled enabled][--matcherField fieldName --matcherOperator operator --matcherValue value]
@@ -54,12 +54,16 @@ func (opts *CreateOpts) Run() error {
5454
// [--projectId projectId]
5555
func CreateBuilder() *cobra.Command {
5656
opts := new(CreateOpts)
57+
opts.Template = createTemplate
5758
cmd := &cobra.Command{
5859
Use: "create",
5960
Short: createAlertsConfig,
6061
Args: cobra.NoArgs,
6162
PreRunE: func(cmd *cobra.Command, args []string) error {
62-
return opts.PreRunE(opts.initStore)
63+
return opts.PreRunE(
64+
opts.initStore,
65+
opts.InitOutput(cmd.OutOrStdout(), createTemplate),
66+
)
6367
},
6468
RunE: func(cmd *cobra.Command, args []string) error {
6569
return opts.Run()
@@ -95,6 +99,7 @@ func CreateBuilder() *cobra.Command {
9599
cmd.Flags().StringVar(&opts.notificationVictorOpsRoutingKey, flag.NotificationVictorOpsRoutingKey, "", usage.NotificationVictorOpsRoutingKey)
96100

97101
cmd.Flags().StringVar(&opts.ProjectID, flag.ProjectID, "", usage.ProjectID)
102+
cmd.Flags().StringVarP(&opts.Output, flag.Output, flag.OutputShort, "", usage.FormatOut)
98103

99104
return cmd
100105
}

internal/cli/alerts/settings/fields_type.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515
package settings
1616

1717
import (
18+
"github.com/mongodb/mongocli/internal/cli"
1819
"github.com/mongodb/mongocli/internal/config"
19-
"github.com/mongodb/mongocli/internal/output"
20+
"github.com/mongodb/mongocli/internal/flag"
2021
"github.com/mongodb/mongocli/internal/store"
22+
"github.com/mongodb/mongocli/internal/usage"
2123
"github.com/spf13/cobra"
2224
)
2325

2426
type FieldsTypeOpts struct {
27+
cli.OutputOpts
2528
store store.MatcherFieldsLister
2629
}
2730

@@ -39,24 +42,28 @@ func (opts *FieldsTypeOpts) Run() error {
3942
return err
4043
}
4144

42-
return output.Print(config.Default(), matcherFieldsTemplate, r)
45+
return opts.Print(r)
4346
}
4447

4548
// mongocli atlas alerts config(s) fields type
4649
func FieldsTypeBuilder() *cobra.Command {
4750
opts := &FieldsTypeOpts{}
51+
opts.Template = matcherFieldsTemplate
4852
cmd := &cobra.Command{
4953
Use: "type",
5054
Short: configFieldsType,
5155
Aliases: []string{"types"},
5256
Args: cobra.NoArgs,
5357
PreRunE: func(cmd *cobra.Command, args []string) error {
58+
opts.OutWriter = cmd.OutOrStdout()
5459
return opts.init()
5560
},
5661
RunE: func(cmd *cobra.Command, args []string) error {
5762
return opts.Run()
5863
},
5964
}
6065

66+
cmd.Flags().StringVarP(&opts.Output, flag.Output, flag.OutputShort, "", usage.FormatOut)
67+
6168
return cmd
6269
}

0 commit comments

Comments
 (0)