Skip to content

Commit

Permalink
Merge pull request #1325 from alenkacz/av/rpk-logout
Browse files Browse the repository at this point in the history
rpk cloud logout
  • Loading branch information
0x5d authored May 5, 2021
2 parents e057ecd + b5e2ad2 commit 5c36d71
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/go/rpk/pkg/cli/cmd/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func NewCloudCommand(fs afero.Fs) *cobra.Command {
}

command.AddCommand(cloud.NewLoginCommand(fs))
command.AddCommand(cloud.NewLogoutCommand(fs))

return command
}
2 changes: 1 addition & 1 deletion src/go/rpk/pkg/cli/cmd/cloud/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewLoginCommand(fs afero.Fs) *cobra.Command {
return fmt.Errorf("error reading token from config file: %w", err)
}
auth0Client := vcloud.NewDefaultAuth0Client()
if err == config.ErrConfigFileDoesNotExist {
if err == config.ErrConfigFileDoesNotExist || token == "" {
// no config file, have to log in
return login(auth0Client, configRW)
}
Expand Down
36 changes: 36 additions & 0 deletions src/go/rpk/pkg/cli/cmd/cloud/logout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2021 Vectorized, Inc.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.md
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0

package cloud

import (
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/vectorizedio/redpanda/src/go/rpk/pkg/vcloud/config"
)

func NewLogoutCommand(fs afero.Fs) *cobra.Command {
return &cobra.Command{
Use: "logout",
Short: "Logout from vectorized cloud",
Long: `If there's a token stored locally after you've logged in, it will be wiped out.`,
RunE: func(cmd *cobra.Command, args []string) error {
configRW := config.NewVCloudConfigReaderWriter(fs)
// write an empty string to the config
err := configRW.WriteToken("")
if err != nil {
log.Info("Error while logging out:")
return err
}
log.Info("Logged out!")
return nil
},
}
}
36 changes: 36 additions & 0 deletions src/go/rpk/pkg/cli/cmd/cloud/logout_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2021 Vectorized, Inc.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.md
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0

package cloud_test

import (
"testing"

"github.com/spf13/afero"
"github.com/stretchr/testify/require"
"github.com/vectorizedio/redpanda/src/go/rpk/pkg/cli/cmd/cloud"
"github.com/vectorizedio/redpanda/src/go/rpk/pkg/vcloud/config"
)

func TestLogout(t *testing.T) {
fs := afero.NewMemMapFs()
configRW := config.NewVCloudConfigReaderWriter(fs)
err := configRW.WriteToken("xxx")
require.NoError(t, err)

cmd := cloud.NewLogoutCommand(fs)
err = cmd.Execute()
require.NoError(t, err)

token, err := configRW.ReadToken()
require.NoError(t, err)
if token != "" {
t.Fatalf("Expecting empty token after logout called, got %s", token)
}
}

0 comments on commit 5c36d71

Please sign in to comment.