Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7ba6449

Browse files
authored
Improve CLI logout flow (#1692)
* Improve CLI logout flow* Fix lint error* Make notLoggedInMessage a const* successful logout with a msg when cfg files are absent* use require, os.remove, show only one message, add prompt
1 parent33e2e40 commit7ba6449

File tree

4 files changed

+185
-26
lines changed

4 files changed

+185
-26
lines changed

‎cli/logout.go

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,64 @@ import (
66

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

1113
funclogout()*cobra.Command {
12-
return&cobra.Command{
14+
cmd:=&cobra.Command{
1315
Use:"logout",
14-
Short:"Remove localautheticated session",
16+
Short:"Removethelocalauthenticated session",
1517
RunE:func(cmd*cobra.Command,args []string)error {
18+
varisLoggedOutbool
19+
1620
config:=createConfig(cmd)
17-
err:=os.RemoveAll(string(config))
21+
22+
_,err:=cliui.Prompt(cmd, cliui.PromptOptions{
23+
Text:"Are you sure you want to logout?",
24+
IsConfirm:true,
25+
Default:"yes",
26+
})
1827
iferr!=nil {
19-
returnxerrors.Errorf("remove files at %s: %w",config,err)
28+
returnerr
2029
}
2130

22-
_,_=fmt.Fprintf(cmd.OutOrStdout(),caret+"Successfully logged out.\n")
31+
err=config.URL().Delete()
32+
iferr!=nil {
33+
// Only throw error if the URL configuration file is present,
34+
// otherwise the user is already logged out, and we proceed
35+
if!os.IsNotExist(err) {
36+
returnxerrors.Errorf("remove URL file: %w",err)
37+
}
38+
isLoggedOut=true
39+
}
40+
41+
err=config.Session().Delete()
42+
iferr!=nil {
43+
// Only throw error if the session configuration file is present,
44+
// otherwise the user is already logged out, and we proceed
45+
if!os.IsNotExist(err) {
46+
returnxerrors.Errorf("remove session file: %w",err)
47+
}
48+
isLoggedOut=true
49+
}
50+
51+
err=config.Organization().Delete()
52+
// If the organization configuration file is absent, we still proceed
53+
iferr!=nil&&!os.IsNotExist(err) {
54+
returnxerrors.Errorf("remove organization file: %w",err)
55+
}
56+
57+
// If the user was already logged out, we show them a different message
58+
ifisLoggedOut {
59+
_,_=fmt.Fprintf(cmd.OutOrStdout(),notLoggedInMessage+"\n")
60+
}else {
61+
_,_=fmt.Fprintf(cmd.OutOrStdout(),caret+"Successfully logged out.\n")
62+
}
2363
returnnil
2464
},
2565
}
66+
67+
cliui.AllowSkipPrompt(cmd)
68+
returncmd
2669
}

‎cli/logout_test.go

Lines changed: 126 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,148 @@
11
package cli_test
22

33
import (
4+
"os"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
78
"github.com/stretchr/testify/require"
89

910
"github.com/coder/coder/cli/clitest"
11+
"github.com/coder/coder/cli/config"
1012
"github.com/coder/coder/coderd/coderdtest"
1113
"github.com/coder/coder/pty/ptytest"
1214
)
1315

1416
funcTestLogout(t*testing.T) {
1517
t.Parallel()
18+
t.Run("Logout",func(t*testing.T) {
19+
t.Parallel()
20+
21+
pty:=ptytest.New(t)
22+
config:=login(t,pty)
23+
24+
// ensure session files exist
25+
require.FileExists(t,string(config.URL()))
26+
require.FileExists(t,string(config.Session()))
27+
28+
logoutChan:=make(chanstruct{})
29+
logout,_:=clitest.New(t,"logout","--global-config",string(config))
30+
logout.SetIn(pty.Input())
31+
logout.SetOut(pty.Output())
32+
33+
gofunc() {
34+
deferclose(logoutChan)
35+
err:=logout.Execute()
36+
assert.NoError(t,err)
37+
assert.NoFileExists(t,string(config.URL()))
38+
assert.NoFileExists(t,string(config.Session()))
39+
}()
40+
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(chanstruct{})
57+
logout,_:=clitest.New(t,"logout","--global-config",string(config),"-y")
58+
logout.SetIn(pty.Input())
59+
logout.SetOut(pty.Output())
60+
61+
gofunc() {
62+
deferclose(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+
69+
pty.ExpectMatch("Successfully logged out")
70+
<-logoutChan
71+
})
72+
t.Run("NoURLFile",func(t*testing.T) {
73+
t.Parallel()
74+
75+
pty:=ptytest.New(t)
76+
config:=login(t,pty)
77+
78+
// ensure session files exist
79+
require.FileExists(t,string(config.URL()))
80+
require.FileExists(t,string(config.Session()))
81+
82+
err:=os.Remove(string(config.URL()))
83+
require.NoError(t,err)
84+
85+
logoutChan:=make(chanstruct{})
86+
logout,_:=clitest.New(t,"logout","--global-config",string(config))
87+
88+
logout.SetIn(pty.Input())
89+
logout.SetOut(pty.Output())
90+
91+
gofunc() {
92+
deferclose(logoutChan)
93+
err:=logout.Execute()
94+
assert.NoError(t,err)
95+
assert.NoFileExists(t,string(config.URL()))
96+
assert.NoFileExists(t,string(config.Session()))
97+
}()
98+
99+
pty.ExpectMatch("Are you sure you want to logout?")
100+
pty.WriteLine("yes")
101+
pty.ExpectMatch("You are not logged in. Try logging in using 'coder login <url>'.")
102+
<-logoutChan
103+
})
104+
t.Run("NoSessionFile",func(t*testing.T) {
105+
t.Parallel()
106+
107+
pty:=ptytest.New(t)
108+
config:=login(t,pty)
109+
110+
// ensure session files exist
111+
require.FileExists(t,string(config.URL()))
112+
require.FileExists(t,string(config.Session()))
113+
114+
err:=os.Remove(string(config.Session()))
115+
require.NoError(t,err)
116+
117+
logoutChan:=make(chanstruct{})
118+
logout,_:=clitest.New(t,"logout","--global-config",string(config))
119+
120+
logout.SetIn(pty.Input())
121+
logout.SetOut(pty.Output())
122+
123+
gofunc() {
124+
deferclose(logoutChan)
125+
err=logout.Execute()
126+
assert.NoError(t,err)
127+
assert.NoFileExists(t,string(config.URL()))
128+
assert.NoFileExists(t,string(config.Session()))
129+
}()
130+
131+
pty.ExpectMatch("Are you sure you want to logout?")
132+
pty.WriteLine("yes")
133+
pty.ExpectMatch("You are not logged in. Try logging in using 'coder login <url>'.")
134+
<-logoutChan
135+
})
136+
}
137+
138+
funclogin(t*testing.T,pty*ptytest.PTY) config.Root {
139+
t.Helper()
16140

17-
// login
18141
client:=coderdtest.New(t,nil)
19142
coderdtest.CreateFirstUser(t,client)
20143

21144
doneChan:=make(chanstruct{})
22-
root,config:=clitest.New(t,"login","--force-tty",client.URL.String(),"--no-open")
23-
pty:=ptytest.New(t)
145+
root,cfg:=clitest.New(t,"login","--force-tty",client.URL.String(),"--no-open")
24146
root.SetIn(pty.Input())
25147
root.SetOut(pty.Output())
26148
gofunc() {
@@ -34,13 +156,5 @@ func TestLogout(t *testing.T) {
34156
pty.ExpectMatch("Welcome to Coder")
35157
<-doneChan
36158

37-
// ensure session files exist
38-
require.FileExists(t,string(config.URL()))
39-
require.FileExists(t,string(config.Session()))
40-
41-
logout,_:=clitest.New(t,"logout","--global-config",string(config))
42-
err:=logout.Execute()
43-
require.NoError(t,err)
44-
require.NoFileExists(t,string(config.URL()))
45-
require.NoFileExists(t,string(config.Session()))
159+
returncfg
46160
}

‎cli/root.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ var (
3131
)
3232

3333
const (
34-
varURL="url"
35-
varToken="token"
36-
varAgentToken="agent-token"
37-
varAgentURL="agent-url"
38-
varGlobalConfig="global-config"
39-
varNoOpen="no-open"
40-
varForceTty="force-tty"
34+
varURL="url"
35+
varToken="token"
36+
varAgentToken="agent-token"
37+
varAgentURL="agent-url"
38+
varGlobalConfig="global-config"
39+
varNoOpen="no-open"
40+
varForceTty="force-tty"
41+
notLoggedInMessage="You are not logged in. Try logging in using 'coder login <url>'."
4142
)
4243

4344
funcinit() {
@@ -117,7 +118,7 @@ func createClient(cmd *cobra.Command) (*codersdk.Client, error) {
117118
iferr!=nil {
118119
// If the configuration files are absent, the user is logged out
119120
ifos.IsNotExist(err) {
120-
returnnil,xerrors.New("You are not logged in. Try logging in using 'coder login <url>'.")
121+
returnnil,xerrors.New(notLoggedInMessage)
121122
}
122123
returnnil,err
123124
}
@@ -132,7 +133,7 @@ func createClient(cmd *cobra.Command) (*codersdk.Client, error) {
132133
iferr!=nil {
133134
// If the configuration files are absent, the user is logged out
134135
ifos.IsNotExist(err) {
135-
returnnil,xerrors.New("You are not logged in. Try logging in using 'coder login <url>'.")
136+
returnnil,xerrors.New(notLoggedInMessage)
136137
}
137138
returnnil,err
138139
}

‎cli/userlist_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
)
1515

1616
funcTestUserList(t*testing.T) {
17+
t.Parallel()
1718
t.Run("List",func(t*testing.T) {
1819
t.Parallel()
1920
client:=coderdtest.New(t,nil)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp