Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/spf13/viper v1.10.1
github.com/stretchr/testify v1.7.0
github.com/tangzero/inflector v1.0.0
go.mongodb.org/atlas v0.14.1-0.20220126105350-5816bca2f88c
go.mongodb.org/atlas v0.14.1-0.20220202080947-4a7d97e77246
go.mongodb.org/ops-manager v0.34.0
gopkg.in/yaml.v2 v2.4.0
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,8 @@ go.etcd.io/etcd/api/v3 v3.5.1/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQc
go.etcd.io/etcd/client/pkg/v3 v3.5.1/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g=
go.etcd.io/etcd/client/v2 v2.305.1/go.mod h1:pMEacxZW7o8pg4CrFE7pquyCJJzZvkvdD2RibOCCCGs=
go.mongodb.org/atlas v0.14.0/go.mod h1:lQhRHIxc6jQHEK3/q9WLu/SdBkPj2fQYhjLGUF6Z3U8=
go.mongodb.org/atlas v0.14.1-0.20220126105350-5816bca2f88c h1:K3Q2ggsg1XtJuurBbJspNRzmkPtIZl5lD1oICR6lW28=
go.mongodb.org/atlas v0.14.1-0.20220126105350-5816bca2f88c/go.mod h1:lQhRHIxc6jQHEK3/q9WLu/SdBkPj2fQYhjLGUF6Z3U8=
go.mongodb.org/atlas v0.14.1-0.20220202080947-4a7d97e77246 h1:BkSHgSH3o6KawlMdsnR+XpZ/pCDbWK5secDmcUmytL0=
go.mongodb.org/atlas v0.14.1-0.20220202080947-4a7d97e77246/go.mod h1:lQhRHIxc6jQHEK3/q9WLu/SdBkPj2fQYhjLGUF6Z3U8=
go.mongodb.org/ops-manager v0.34.0 h1:d8TgpJpPFeVLr+6HrAbbbYnKkdk+2JW6E20OBdgqXg8=
go.mongodb.org/ops-manager v0.34.0/go.mod h1:85LPPdME1TFJ/Eau/IgOmy37YWGw1p/S8PBSME8ukXs=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
Expand Down
1 change: 1 addition & 0 deletions internal/cli/auth/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ func Builder() *cobra.Command {
}
cmd.AddCommand(
LoginBuilder(),
LogoutBuilder(),
)

return cmd
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/auth/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestBuilder(t *testing.T) {
test.CmdValidator(
t,
Builder(),
1,
2,
[]string{},
)
}
Expand Down
95 changes: 95 additions & 0 deletions internal/cli/auth/logout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2022 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 auth

import (
"context"
"errors"
"io"

"github.com/mongodb/mongocli/internal/cli"
"github.com/mongodb/mongocli/internal/cli/require"
"github.com/mongodb/mongocli/internal/config"
"github.com/mongodb/mongocli/internal/flag"
"github.com/mongodb/mongocli/internal/oauth"
"github.com/mongodb/mongocli/internal/usage"
"github.com/spf13/cobra"
atlas "go.mongodb.org/atlas/mongodbatlas"
)

type logoutOpts struct {
*cli.DeleteOpts
OutWriter io.Writer
config ConfigDeleter
flow Revoker
}

//go:generate mockgen -destination=../../mocks/mock_logout.go -package=mocks github.com/mongodb/mongocli/internal/cli/auth Revoker,ConfigDeleter

type ConfigDeleter interface {
Delete() error
}

type Revoker interface {
Revoke(context.Context, string, string) (*atlas.Response, error)
}

func (opts *logoutOpts) initFlow() error {
var err error
opts.flow, err = oauth.FlowWithConfig(config.Default())
return err
}

func (opts *logoutOpts) Run(ctx context.Context) error {
// revoking a refresh token revokes the access token
if _, err := opts.flow.Revoke(ctx, config.RefreshToken(), "refresh_token"); err != nil {
return err
}

return opts.Delete(opts.config.Delete)
}

func LogoutBuilder() *cobra.Command {
opts := &logoutOpts{
DeleteOpts: cli.NewDeleteOpts("Successfully logged out\n", " "),
}

cmd := &cobra.Command{
Use: "logout",
Short: "Log out the CLI.",
Example: ` To log out from the CLI:
$ mongocli auth logout
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
opts.OutWriter = cmd.OutOrStdout()
opts.config = config.Default()
return opts.initFlow()
},
RunE: func(cmd *cobra.Command, args []string) error {
if config.RefreshToken() == "" {
return errors.New("not logged in")
}
if err := opts.PromptWithMessage("Are you sure you want to log out?"); err != nil {
return err
}
return opts.Run(cmd.Context())
},
Args: require.NoArgs,
}

cmd.Flags().BoolVar(&opts.Confirm, flag.Force, false, usage.Force)

return cmd
}
69 changes: 69 additions & 0 deletions internal/cli/auth/logout_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2022 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.

//go:build unit
// +build unit

package auth

import (
"bytes"
"context"
"testing"

"github.com/golang/mock/gomock"
"github.com/mongodb/mongocli/internal/cli"
"github.com/mongodb/mongocli/internal/flag"
"github.com/mongodb/mongocli/internal/mocks"
"github.com/mongodb/mongocli/internal/test"
"github.com/stretchr/testify/require"
)

func TestLogoutBuilder(t *testing.T) {
test.CmdValidator(
t,
LogoutBuilder(),
0,
[]string{flag.Force},
)
}

func Test_logoutOpts_Run(t *testing.T) {
ctrl := gomock.NewController(t)
mockFlow := mocks.NewMockRevoker(ctrl)
mockConfig := mocks.NewMockConfigDeleter(ctrl)
defer ctrl.Finish()
buf := new(bytes.Buffer)

opts := logoutOpts{
OutWriter: buf,
config: mockConfig,
flow: mockFlow,
DeleteOpts: &cli.DeleteOpts{
Confirm: true,
},
}
ctx := context.TODO()
mockFlow.
EXPECT().
Revoke(ctx, gomock.Any(), gomock.Any()).
Return(nil, nil).
Times(1)
mockConfig.
EXPECT().
Delete().
Return(nil).
Times(1)
require.NoError(t, opts.Run(ctx))
}
15 changes: 12 additions & 3 deletions internal/cli/delete_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ func (opts *DeleteOpts) Delete(d interface{}, a ...string) error {

var err error
switch f := d.(type) {
case func() error:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding support for a no "entry" case

err = f()
case func(string) error:
err = f(opts.Entry)
case func(string, string) error:
Expand All @@ -69,8 +71,11 @@ func (opts *DeleteOpts) Delete(d interface{}, a ...string) error {
if err != nil {
return err
}

fmt.Printf(opts.SuccessMessage(), opts.Entry)
if opts.Entry == "" {
fmt.Print(opts.SuccessMessage())
} else {
fmt.Printf(opts.SuccessMessage(), opts.Entry)
}

return nil
}
Expand All @@ -91,7 +96,11 @@ func (opts *DeleteOpts) PromptWithMessage(message string) error {
return nil
}

p := prompt.NewConfirm(fmt.Sprintf(message, opts.Entry))
m := message
if opts.Entry != "" {
m = fmt.Sprintf(message, opts.Entry)
}
p := prompt.NewConfirm(m)
return survey.AskOne(p, &opts.Confirm)
}

Expand Down
88 changes: 88 additions & 0 deletions internal/mocks/mock_logout.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.