Skip to content

feat: add coder logout command #1609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2022
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
26 changes: 26 additions & 0 deletions cli/logout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cli

import (
"fmt"
"os"

"github.com/spf13/cobra"
"golang.org/x/xerrors"
)

func logout() *cobra.Command {
return &cobra.Command{
Use: "logout",
Short: "Remove local autheticated session",
Copy link
Contributor

Choose a reason for hiding this comment

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

typo: s/autheticated/authenticated/

RunE: func(cmd *cobra.Command, args []string) error {
config := createConfig(cmd)
err := os.RemoveAll(string(config))
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe I'm just paranoid, but I think this should only remove config.Session(), config.URL() and config.Organization(), and not any other files that happen to have ended up in the directory (at least not without confirmation). I would be worried that a misconfigured CODER_CONFIG_DIR could lead to somebody accidentally wiping out their home directory.

See e.g. ValveSoftware/steam-for-linux#3671

Copy link
Member

Choose a reason for hiding this comment

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

I totally agree, we shouldn't delete the folder. I wouldn't put it past that some users are storing a url.bak or session.bak. Or any other related material really that they've cooked up themselves.

if err != nil {
return xerrors.Errorf("remove files at %s: %w", config, err)
}

_, _ = fmt.Fprintf(cmd.OutOrStdout(), caret+"Successfully logged out.\n")
return nil
},
}
}
44 changes: 44 additions & 0 deletions cli/logout_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cli_test

import (
"testing"

"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/pty/ptytest"
"github.com/stretchr/testify/require"
)

func TestLogout(t *testing.T) {
t.Parallel()

// login
client := coderdtest.New(t, nil)
coderdtest.CreateFirstUser(t, client)

doneChan := make(chan struct{})
root, config := clitest.New(t, "login", "--force-tty", client.URL.String(), "--no-open")
pty := ptytest.New(t)
root.SetIn(pty.Input())
root.SetOut(pty.Output())
go func() {
defer close(doneChan)
err := root.Execute()
require.NoError(t, err)
}()

pty.ExpectMatch("Paste your token here:")
pty.WriteLine(client.SessionToken)
pty.ExpectMatch("Welcome to Coder")
<-doneChan

// ensure session files exist
require.FileExists(t, string(config.URL()))
require.FileExists(t, string(config.Session()))

logout, _ := clitest.New(t, "logout", "--global-config", string(config))
err := logout.Execute()
require.NoError(t, err)
require.NoFileExists(t, string(config.URL()))
require.NoFileExists(t, string(config.Session()))
}
1 change: 1 addition & 0 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func Root() *cobra.Command {
gitssh(),
list(),
login(),
logout(),
publickey(),
resetPassword(),
server(),
Expand Down