Skip to content

Commit

Permalink
CLOUDP-69793: [mongocli] Improve output handling (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
gssbzn committed Aug 21, 2020
1 parent 568bc47 commit 7a48ec1
Show file tree
Hide file tree
Showing 118 changed files with 971 additions and 444 deletions.
4 changes: 1 addition & 3 deletions cmd/mongocli/mongocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ func Execute() {

var (
profile string
output string
)

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

rootCmd.PersistentFlags().StringVarP(&profile, flag.Profile, flag.ProfileShort, "", usage.Profile)
rootCmd.PersistentFlags().StringVarP(&output, flag.Output, flag.OutputShort, "", usage.FormatOut)

cobra.OnInitialize(initConfig)
}

Expand All @@ -117,7 +116,6 @@ func initConfig() {
} else if len(availableProfiles) == 1 {
config.SetName(availableProfiles[0])
}
config.SetOutput(output)
}

func main() {
Expand Down
2 changes: 1 addition & 1 deletion e2e/atlas/clusters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestClustersFlags(t *testing.T) {
clustersEntity,
"watch",
clusterName,
"-o=json")
)
cmd.Env = os.Environ()
resp, err := cmd.CombinedOutput()

Expand Down
5 changes: 3 additions & 2 deletions e2e/cloud_manager/dbusers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"testing"

"github.com/mongodb/mongocli/e2e"
"go.mongodb.org/atlas/mongodbatlas"
"go.mongodb.org/ops-manager/opsmngr"
)

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

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

Expand Down
1 change: 1 addition & 0 deletions e2e/cloud_manager/deploy_replica_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func TestDeployReplicaSet(t *testing.T) {
}
var clusters []*convert.ClusterConfig
if err := json.Unmarshal(resp, &clusters); err != nil {
t.Log(string(resp))
t.Fatalf("unexpected error: %v", err)
}

Expand Down
1 change: 0 additions & 1 deletion e2e/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ func TestConfig(t *testing.T) {
t.Fatalf("unexpected error: %v, resp: %v", err, string(resp))
}
const expected = `org_id = 5e429e7706822c6eac4d5971
output =
public_api_key = redacted
service = cloud
`
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/go-test/deep v1.0.7
github.com/golang/mock v1.4.4
github.com/mattn/go-isatty v0.0.8
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/mapstructure v1.3.1 // indirect
github.com/mongodb-forks/digest v1.0.1
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
Expand Down
13 changes: 9 additions & 4 deletions internal/cli/alerts/acknowledge.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/mongodb/mongocli/internal/cli"
"github.com/mongodb/mongocli/internal/config"
"github.com/mongodb/mongocli/internal/flag"
"github.com/mongodb/mongocli/internal/output"
"github.com/mongodb/mongocli/internal/store"
"github.com/mongodb/mongocli/internal/usage"
"github.com/spf13/cobra"
Expand All @@ -30,6 +29,7 @@ import (

type AcknowledgeOpts struct {
cli.GlobalOpts
cli.OutputOpts
alertID string
until string
comment string
Expand All @@ -52,7 +52,7 @@ func (opts *AcknowledgeOpts) Run() error {
return err
}

return output.Print(config.Default(), ackTemplate, r)
return opts.Print(r)
}

func (opts *AcknowledgeOpts) newAcknowledgeRequest() *atlas.AcknowledgeRequest {
Expand All @@ -71,6 +71,7 @@ func (opts *AcknowledgeOpts) newAcknowledgeRequest() *atlas.AcknowledgeRequest {
// mongocli atlas alerts acknowledge <ID> --projectId projectId --forever --comment comment --until until
func AcknowledgeBuilder() *cobra.Command {
opts := new(AcknowledgeOpts)
opts.Template = ackTemplate
cmd := &cobra.Command{
Use: "acknowledge <ID>",
Short: acknowledgeAlerts,
Expand All @@ -80,19 +81,23 @@ func AcknowledgeBuilder() *cobra.Command {
if opts.forever && opts.until != "" {
return fmt.Errorf("--%s and --%s are exclusive", flag.Forever, flag.Until)
}
return opts.PreRunE(opts.initStore)
return opts.PreRunE(
opts.initStore,
opts.InitOutput(cmd.OutOrStdout(), ackTemplate),
)
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.alertID = args[0]
return opts.Run()
},
}

cmd.OutOrStdout()
cmd.Flags().BoolVarP(&opts.forever, flag.Forever, flag.ForeverShort, false, usage.Forever)
cmd.Flags().StringVar(&opts.until, flag.Until, "", usage.Until)
cmd.Flags().StringVar(&opts.comment, flag.Comment, "", usage.Comment)

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

return cmd
}
10 changes: 7 additions & 3 deletions internal/cli/alerts/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import (
"github.com/mongodb/mongocli/internal/cli"
"github.com/mongodb/mongocli/internal/config"
"github.com/mongodb/mongocli/internal/flag"
"github.com/mongodb/mongocli/internal/output"
"github.com/mongodb/mongocli/internal/store"
"github.com/mongodb/mongocli/internal/usage"
"github.com/spf13/cobra"
)

type DescribeOpts struct {
cli.GlobalOpts
cli.OutputOpts
alertID string
store store.AlertDescriber
}
Expand All @@ -47,7 +47,7 @@ func (opts *DescribeOpts) Run() error {
return err
}

return output.Print(config.Default(), describeTemplate, r)
return opts.Print(r)
}

// mongocli atlas alerts describe <ID> --projectId projectId
Expand All @@ -58,7 +58,10 @@ func DescribeBuilder() *cobra.Command {
Short: describeAlert,
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.PreRunE(opts.initStore)
return opts.PreRunE(
opts.initStore,
opts.InitOutput(cmd.OutOrStdout(), describeTemplate),
)
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.alertID = args[0]
Expand All @@ -67,6 +70,7 @@ func DescribeBuilder() *cobra.Command {
}

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

return cmd
}
8 changes: 6 additions & 2 deletions internal/cli/alerts/global_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/mongodb/mongocli/internal/cli"
"github.com/mongodb/mongocli/internal/config"
"github.com/mongodb/mongocli/internal/flag"
"github.com/mongodb/mongocli/internal/output"
"github.com/mongodb/mongocli/internal/store"
"github.com/mongodb/mongocli/internal/usage"
"github.com/spf13/cobra"
Expand All @@ -27,6 +26,7 @@ import (

type GlobalListOpts struct {
cli.ListOpts
cli.OutputOpts
status string
store store.GlobalAlertLister
}
Expand All @@ -44,7 +44,7 @@ func (opts *GlobalListOpts) Run() error {
return err
}

return output.Print(config.Default(), listTemplate, r)
return opts.Print(r)
}

func (opts *GlobalListOpts) newAlertsListOptions() *atlas.AlertsListOptions {
Expand All @@ -57,12 +57,14 @@ func (opts *GlobalListOpts) newAlertsListOptions() *atlas.AlertsListOptions {
// mongocli om|cm alert(s) global list [--status status]
func GlobalListBuilder() *cobra.Command {
opts := &GlobalListOpts{}
opts.Template = listTemplate
cmd := &cobra.Command{
Use: "list",
Short: listGlobalAlerts,
Aliases: []string{"ls"},
Args: cobra.NoArgs,
PreRunE: func(cmd *cobra.Command, args []string) error {
opts.OutWriter = cmd.OutOrStdout()
return opts.init()
},
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -74,5 +76,7 @@ func GlobalListBuilder() *cobra.Command {
cmd.Flags().IntVar(&opts.ItemsPerPage, flag.Limit, 0, usage.Limit)
cmd.Flags().StringVar(&opts.status, flag.Status, "", usage.Status)

cmd.Flags().StringVarP(&opts.Output, flag.Output, flag.OutputShort, "", usage.FormatOut)

return cmd
}
10 changes: 7 additions & 3 deletions internal/cli/alerts/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/mongodb/mongocli/internal/cli"
"github.com/mongodb/mongocli/internal/config"
"github.com/mongodb/mongocli/internal/flag"
"github.com/mongodb/mongocli/internal/output"
"github.com/mongodb/mongocli/internal/store"
"github.com/mongodb/mongocli/internal/usage"
"github.com/spf13/cobra"
Expand All @@ -27,6 +26,7 @@ import (

type ListOpts struct {
cli.GlobalOpts
cli.OutputOpts
cli.ListOpts
status string
store store.AlertLister
Expand All @@ -50,7 +50,7 @@ func (opts *ListOpts) Run() error {
return err
}

return output.Print(config.Default(), listTemplate, r)
return opts.Print(r)
}

func (opts *ListOpts) newAlertsListOptions() *atlas.AlertsListOptions {
Expand All @@ -71,7 +71,10 @@ func ListBuilder() *cobra.Command {
Aliases: []string{"ls"},
Args: cobra.NoArgs,
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.PreRunE(opts.initStore)
return opts.PreRunE(
opts.initStore,
opts.InitOutput(cmd.OutOrStdout(), listTemplate),
)
},
RunE: func(cmd *cobra.Command, args []string) error {
return opts.Run()
Expand All @@ -83,6 +86,7 @@ func ListBuilder() *cobra.Command {
cmd.Flags().StringVar(&opts.status, flag.Status, "", usage.Status)

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

return cmd
}
11 changes: 8 additions & 3 deletions internal/cli/alerts/settings/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import (
"github.com/mongodb/mongocli/internal/cli"
"github.com/mongodb/mongocli/internal/config"
"github.com/mongodb/mongocli/internal/flag"
"github.com/mongodb/mongocli/internal/output"
"github.com/mongodb/mongocli/internal/store"
"github.com/mongodb/mongocli/internal/usage"
"github.com/spf13/cobra"
)

type CreateOpts struct {
cli.GlobalOpts
cli.OutputOpts
ConfigOpts
store store.AlertConfigurationCreator
}
Expand All @@ -45,7 +45,7 @@ func (opts *CreateOpts) Run() error {
return err
}

return output.Print(config.Default(), createTemplate, r)
return opts.Print(r)
}

// mongocli atlas alerts config(s) create [--event event] [--enabled enabled][--matcherField fieldName --matcherOperator operator --matcherValue value]
Expand All @@ -54,12 +54,16 @@ func (opts *CreateOpts) Run() error {
// [--projectId projectId]
func CreateBuilder() *cobra.Command {
opts := new(CreateOpts)
opts.Template = createTemplate
cmd := &cobra.Command{
Use: "create",
Short: createAlertsConfig,
Args: cobra.NoArgs,
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.PreRunE(opts.initStore)
return opts.PreRunE(
opts.initStore,
opts.InitOutput(cmd.OutOrStdout(), createTemplate),
)
},
RunE: func(cmd *cobra.Command, args []string) error {
return opts.Run()
Expand Down Expand Up @@ -95,6 +99,7 @@ func CreateBuilder() *cobra.Command {
cmd.Flags().StringVar(&opts.notificationVictorOpsRoutingKey, flag.NotificationVictorOpsRoutingKey, "", usage.NotificationVictorOpsRoutingKey)

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

return cmd
}
11 changes: 9 additions & 2 deletions internal/cli/alerts/settings/fields_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
package settings

import (
"github.com/mongodb/mongocli/internal/cli"
"github.com/mongodb/mongocli/internal/config"
"github.com/mongodb/mongocli/internal/output"
"github.com/mongodb/mongocli/internal/flag"
"github.com/mongodb/mongocli/internal/store"
"github.com/mongodb/mongocli/internal/usage"
"github.com/spf13/cobra"
)

type FieldsTypeOpts struct {
cli.OutputOpts
store store.MatcherFieldsLister
}

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

return output.Print(config.Default(), matcherFieldsTemplate, r)
return opts.Print(r)
}

// mongocli atlas alerts config(s) fields type
func FieldsTypeBuilder() *cobra.Command {
opts := &FieldsTypeOpts{}
opts.Template = matcherFieldsTemplate
cmd := &cobra.Command{
Use: "type",
Short: configFieldsType,
Aliases: []string{"types"},
Args: cobra.NoArgs,
PreRunE: func(cmd *cobra.Command, args []string) error {
opts.OutWriter = cmd.OutOrStdout()
return opts.init()
},
RunE: func(cmd *cobra.Command, args []string) error {
return opts.Run()
},
}

cmd.Flags().StringVarP(&opts.Output, flag.Output, flag.OutputShort, "", usage.FormatOut)

return cmd
}
Loading

0 comments on commit 7a48ec1

Please sign in to comment.