Skip to content

Commit c80858c

Browse files
committed
use require, os.remove, show only one message, add prompt
1 parent 33adc09 commit c80858c

File tree

2 files changed

+63
-16
lines changed

2 files changed

+63
-16
lines changed

cli/logout.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,29 @@ import (
66

77
"github.com/spf13/cobra"
88
"golang.org/x/xerrors"
9+
10+
"github.com/coder/coder/cli/cliui"
911
)
1012

1113
func logout() *cobra.Command {
12-
return &cobra.Command{
14+
cmd := &cobra.Command{
1315
Use: "logout",
1416
Short: "Remove the local authenticated session",
1517
RunE: func(cmd *cobra.Command, args []string) error {
1618
var isLoggedOut bool
1719

1820
config := createConfig(cmd)
1921

20-
err := config.URL().Delete()
22+
_, err := cliui.Prompt(cmd, cliui.PromptOptions{
23+
Text: "Are you sure you want to logout?",
24+
IsConfirm: true,
25+
Default: "yes",
26+
})
27+
if err != nil {
28+
return err
29+
}
30+
31+
err = config.URL().Delete()
2132
if err != nil {
2233
// Only throw error if the URL configuration file is present,
2334
// otherwise the user is already logged out, and we proceed
@@ -43,13 +54,16 @@ func logout() *cobra.Command {
4354
return xerrors.Errorf("remove organization file: %w", err)
4455
}
4556

46-
// If the user was already logged out, we show them a message
57+
// If the user was already logged out, we show them a different message
4758
if isLoggedOut {
4859
_, _ = fmt.Fprintf(cmd.OutOrStdout(), notLoggedInMessage+"\n")
60+
} else {
61+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), caret+"Successfully logged out.\n")
4962
}
50-
51-
_, _ = fmt.Fprintf(cmd.OutOrStdout(), caret+"Successfully logged out.\n")
5263
return nil
5364
},
5465
}
66+
67+
cliui.AllowSkipPrompt(cmd)
68+
return cmd
5569
}

cli/logout_test.go

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
89

910
"github.com/coder/coder/cli/clitest"
1011
"github.com/coder/coder/cli/config"
@@ -21,8 +22,8 @@ func TestLogout(t *testing.T) {
2122
config := login(t, pty)
2223

2324
// ensure session files exist
24-
assert.FileExists(t, string(config.URL()))
25-
assert.FileExists(t, string(config.Session()))
25+
require.FileExists(t, string(config.URL()))
26+
require.FileExists(t, string(config.Session()))
2627

2728
logoutChan := make(chan struct{})
2829
logout, _ := clitest.New(t, "logout", "--global-config", string(config))
@@ -37,6 +38,34 @@ func TestLogout(t *testing.T) {
3738
assert.NoFileExists(t, string(config.Session()))
3839
}()
3940

41+
pty.ExpectMatch("Are you sure you want to logout?")
42+
pty.WriteLine("yes")
43+
pty.ExpectMatch("Successfully logged out")
44+
<-logoutChan
45+
})
46+
t.Run("SkipPrompt", func(t *testing.T) {
47+
t.Parallel()
48+
49+
pty := ptytest.New(t)
50+
config := login(t, pty)
51+
52+
// ensure session files exist
53+
require.FileExists(t, string(config.URL()))
54+
require.FileExists(t, string(config.Session()))
55+
56+
logoutChan := make(chan struct{})
57+
logout, _ := clitest.New(t, "logout", "--global-config", string(config), "-y")
58+
logout.SetIn(pty.Input())
59+
logout.SetOut(pty.Output())
60+
61+
go func() {
62+
defer close(logoutChan)
63+
err := logout.Execute()
64+
assert.NoError(t, err)
65+
assert.NoFileExists(t, string(config.URL()))
66+
assert.NoFileExists(t, string(config.Session()))
67+
}()
68+
4069
pty.ExpectMatch("Successfully logged out")
4170
<-logoutChan
4271
})
@@ -47,10 +76,11 @@ func TestLogout(t *testing.T) {
4776
config := login(t, pty)
4877

4978
// ensure session files exist
50-
assert.FileExists(t, string(config.URL()))
51-
assert.FileExists(t, string(config.Session()))
79+
require.FileExists(t, string(config.URL()))
80+
require.FileExists(t, string(config.Session()))
5281

53-
os.RemoveAll(string(config.URL()))
82+
err := os.Remove(string(config.URL()))
83+
require.NoError(t, err)
5484

5585
logoutChan := make(chan struct{})
5686
logout, _ := clitest.New(t, "logout", "--global-config", string(config))
@@ -66,8 +96,9 @@ func TestLogout(t *testing.T) {
6696
assert.NoFileExists(t, string(config.Session()))
6797
}()
6898

99+
pty.ExpectMatch("Are you sure you want to logout?")
100+
pty.WriteLine("yes")
69101
pty.ExpectMatch("You are not logged in. Try logging in using 'coder login <url>'.")
70-
pty.ExpectMatch("Successfully logged out")
71102
<-logoutChan
72103
})
73104
t.Run("NoSessionFile", func(t *testing.T) {
@@ -77,10 +108,11 @@ func TestLogout(t *testing.T) {
77108
config := login(t, pty)
78109

79110
// ensure session files exist
80-
assert.FileExists(t, string(config.URL()))
81-
assert.FileExists(t, string(config.Session()))
111+
require.FileExists(t, string(config.URL()))
112+
require.FileExists(t, string(config.Session()))
82113

83-
os.RemoveAll(string(config.Session()))
114+
err := os.Remove(string(config.Session()))
115+
require.NoError(t, err)
84116

85117
logoutChan := make(chan struct{})
86118
logout, _ := clitest.New(t, "logout", "--global-config", string(config))
@@ -90,14 +122,15 @@ func TestLogout(t *testing.T) {
90122

91123
go func() {
92124
defer close(logoutChan)
93-
err := logout.Execute()
125+
err = logout.Execute()
94126
assert.NoError(t, err)
95127
assert.NoFileExists(t, string(config.URL()))
96128
assert.NoFileExists(t, string(config.Session()))
97129
}()
98130

131+
pty.ExpectMatch("Are you sure you want to logout?")
132+
pty.WriteLine("yes")
99133
pty.ExpectMatch("You are not logged in. Try logging in using 'coder login <url>'.")
100-
pty.ExpectMatch("Successfully logged out")
101134
<-logoutChan
102135
})
103136
}

0 commit comments

Comments
 (0)