Skip to content

Commit 077f16c

Browse files
authored
feat: add coder logout command (#1609)
1 parent 0c4a65b commit 077f16c

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

cli/logout.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/spf13/cobra"
8+
"golang.org/x/xerrors"
9+
)
10+
11+
func logout() *cobra.Command {
12+
return &cobra.Command{
13+
Use: "logout",
14+
Short: "Remove local autheticated session",
15+
RunE: func(cmd *cobra.Command, args []string) error {
16+
config := createConfig(cmd)
17+
err := os.RemoveAll(string(config))
18+
if err != nil {
19+
return xerrors.Errorf("remove files at %s: %w", config, err)
20+
}
21+
22+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), caret+"Successfully logged out.\n")
23+
return nil
24+
},
25+
}
26+
}

cli/logout_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package cli_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/coder/coder/cli/clitest"
7+
"github.com/coder/coder/coderd/coderdtest"
8+
"github.com/coder/coder/pty/ptytest"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestLogout(t *testing.T) {
13+
t.Parallel()
14+
15+
// login
16+
client := coderdtest.New(t, nil)
17+
coderdtest.CreateFirstUser(t, client)
18+
19+
doneChan := make(chan struct{})
20+
root, config := clitest.New(t, "login", "--force-tty", client.URL.String(), "--no-open")
21+
pty := ptytest.New(t)
22+
root.SetIn(pty.Input())
23+
root.SetOut(pty.Output())
24+
go func() {
25+
defer close(doneChan)
26+
err := root.Execute()
27+
require.NoError(t, err)
28+
}()
29+
30+
pty.ExpectMatch("Paste your token here:")
31+
pty.WriteLine(client.SessionToken)
32+
pty.ExpectMatch("Welcome to Coder")
33+
<-doneChan
34+
35+
// ensure session files exist
36+
require.FileExists(t, string(config.URL()))
37+
require.FileExists(t, string(config.Session()))
38+
39+
logout, _ := clitest.New(t, "logout", "--global-config", string(config))
40+
err := logout.Execute()
41+
require.NoError(t, err)
42+
require.NoFileExists(t, string(config.URL()))
43+
require.NoFileExists(t, string(config.Session()))
44+
}

cli/root.go

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func Root() *cobra.Command {
6969
gitssh(),
7070
list(),
7171
login(),
72+
logout(),
7273
publickey(),
7374
resetPassword(),
7475
server(),

0 commit comments

Comments
 (0)