@@ -23,6 +23,7 @@ type LogoutOptions struct {
23
23
StartingKubeConfig * kclientcmdapi.Config
24
24
Config * restclient.Config
25
25
Out io.Writer
26
+ ErrOut io.Writer
26
27
27
28
PathOptions * kclientcmd.PathOptions
28
29
}
47
48
)
48
49
49
50
// NewCmdLogout implements the OpenShift cli logout command
50
- func NewCmdLogout (name , fullName , ocLoginFullCommand string , f * osclientcmd.Factory , reader io.Reader , out io.Writer ) * cobra.Command {
51
+ func NewCmdLogout (name , fullName , ocLoginFullCommand string , f * osclientcmd.Factory , reader io.Reader , out , errOut io.Writer ) * cobra.Command {
51
52
options := & LogoutOptions {
52
53
Out : out ,
54
+ ErrOut : errOut ,
53
55
}
54
56
55
57
cmds := & cobra.Command {
@@ -124,42 +126,38 @@ func (o LogoutOptions) RunLogout() error {
124
126
return err
125
127
}
126
128
127
- if err := client .OAuthAccessTokens ().Delete (token ); err != nil {
128
- errs := []error {}
129
-
130
- configErr := o .deleteTokenFromConfig ()
131
- if configErr != nil {
132
- errs = append (errs , configErr )
133
- } else {
134
- fmt .Printf ("Successfully removed user token from local config.\n \n " )
135
- }
129
+ errs := []error {}
130
+ serverErr := client .OAuthAccessTokens ().Delete (token )
131
+ if serverErr != nil {
132
+ errs = append (errs , serverErr )
133
+ }
136
134
137
- // append error from server
135
+ if err := deleteTokenFromConfig ( * o . StartingKubeConfig , o . PathOptions , token ); err != nil {
138
136
errs = append (errs , err )
139
- return kutilerrors .Flatten (kutilerrors .NewAggregate (errs ))
137
+ } else if serverErr != nil {
138
+ // if token was not able to be deleted using api but was successfully removed
139
+ // from client config, print brief explanation that token was still removed locally
140
+ fmt .Fprintf (o .ErrOut , "%s\n \n " , "An error ocurred deleting the token on the server. The token has been removed from your local configuration." )
140
141
}
141
142
142
- if err := o . deleteTokenFromConfig (); err != nil {
143
- return err
143
+ if len ( errs ) > 0 {
144
+ return kutilerrors . Flatten ( kutilerrors . NewAggregate ( errs ))
144
145
}
145
146
146
147
fmt .Fprintf (o .Out , "Logged %q out on %q\n " , userInfo .Name , o .Config .Host )
147
-
148
148
return nil
149
149
}
150
150
151
- func (o LogoutOptions ) deleteTokenFromConfig () error {
152
- newConfig := * o .StartingKubeConfig
153
-
154
- for key , value := range newConfig .AuthInfos {
155
- if value .Token == o .Config .BearerToken {
151
+ func deleteTokenFromConfig (config kclientcmdapi.Config , pathOptions * kclientcmd.PathOptions , bearerToken string ) error {
152
+ for key , value := range config .AuthInfos {
153
+ if value .Token == bearerToken {
156
154
value .Token = ""
157
- newConfig .AuthInfos [key ] = value
155
+ config .AuthInfos [key ] = value
158
156
// don't break, its possible that more than one user stanza has the same token.
159
157
}
160
158
}
161
159
162
- if err := kclientcmd .ModifyConfig (o . PathOptions , newConfig , true ); err != nil {
160
+ if err := kclientcmd .ModifyConfig (pathOptions , config , true ); err != nil {
163
161
return err
164
162
}
165
163
0 commit comments