@@ -63,37 +63,95 @@ var logoutCmd = &cobra.Command{
63
63
configFilePath = configFile
64
64
}
65
65
66
- // Remove environment variables
67
66
log .Info ().Msg ("Running logout command for environment variables" )
68
67
envLogout ()
69
68
70
- log .Info ().
71
- Str ("configFilePath" , configFilePath ).
72
- Msg ("Attempting to removing config file" )
73
- err := os .Remove (configFilePath )
74
- if err != nil {
75
- if os .IsNotExist (err ) {
69
+ if profile != "" {
70
+ pErr := logoutProfile (profile , configFilePath )
71
+ if pErr != nil {
76
72
log .Error ().
77
- Err (err ).
78
- Msg ( "config file does not exist, unable to logout" )
79
- fmt . Println ( "Config file does not exist, unable to logout. " )
80
- return err
73
+ Err (pErr ).
74
+ Str ( "profile" , profile ).
75
+ Msg ( " unable to logout profile " )
76
+ return pErr
81
77
}
78
+ fmt .Printf (
79
+ "Logged out successfully, removed profile '%s' from config file '%s'." ,
80
+ profile ,
81
+ configFilePath ,
82
+ )
83
+ return nil
84
+ }
85
+
86
+ logoutFileErr := logoutFile (configFilePath )
87
+ if logoutFileErr != nil {
82
88
log .Error ().
83
- Err (err ).
84
- Msg ( "unable to remove config file, logout failed" )
85
- fmt . Println ( "Error removing config file: " , err )
86
- return err
89
+ Err (logoutFileErr ).
90
+ Str ( "configFilePath" , configFilePath ).
91
+ Msg ( "unable to logout" )
92
+ return logoutFileErr
87
93
}
88
- log .Info ().
89
- Str ("configFilePath" , configFilePath ).
90
- Msg ("Config file removed successfully" )
91
- fmt .Println ("Logged out successfully!" )
92
94
return nil
93
95
},
94
96
}
95
97
98
+ // logoutFile removes the config file
99
+ func logoutFile (f string ) error {
100
+ log .Info ().
101
+ Str ("configFilePath" , f ).
102
+ Msg ("Running logout command for config file" )
103
+
104
+ var performLogout bool
105
+ if ! noPrompt {
106
+ performLogout = promptForInteractiveYesNo (
107
+ fmt .Sprintf (
108
+ "Are you sure you want to remove the config file '%s'?" ,
109
+ f ,
110
+ ),
111
+ )
112
+ if ! performLogout {
113
+ log .Info ().Msg ("Logout file cancelled" )
114
+ fmt .Println (fmt .Sprintf ("Logout file '%s' cancelled." , f ))
115
+ return nil
116
+ }
117
+ }
118
+
119
+ log .Debug ().
120
+ Str ("configFilePath" , f ).
121
+ Msg ("Removing config file" )
122
+ err := os .Remove (f )
123
+
124
+ if err != nil {
125
+ if os .IsNotExist (err ) {
126
+ log .Error ().
127
+ Err (err ).
128
+ Msg ("config file does not exist, unable to logout" )
129
+ return err
130
+ }
131
+ log .Error ().
132
+ Err (err ).
133
+ Msg ("unable to remove config file, logout failed" )
134
+ return err
135
+ }
136
+ log .Info ().
137
+ Str ("configFilePath" , f ).
138
+ Msg ("Config file removed successfully" )
139
+ fmt .Println (fmt .Sprintf ("Logged out successfully, removed config file '%s'" , f ))
140
+ return nil
141
+ }
142
+
143
+ // envLogout unsets environment variables
96
144
func envLogout () {
145
+
146
+ if ! noPrompt {
147
+ performLogout := promptForInteractiveYesNo ("Are you sure you want to unset environment variables?" )
148
+ if ! performLogout {
149
+ log .Info ().Msg ("Logout environment variables cancelled" )
150
+ fmt .Println ("Logout environment variables cancelled." )
151
+ return
152
+ }
153
+ }
154
+
97
155
log .Debug ().Msg ("Running logout command for environment variables" )
98
156
99
157
log .Debug ().Msg ("Unsetting base environment variables" )
@@ -164,6 +222,70 @@ func envLogout() {
164
222
165
223
}
166
224
225
+ // logoutProfile removes the profile from the config file
226
+ func logoutProfile (p string , f string ) error {
227
+ log .Info ().
228
+ Str ("profile" , p ).
229
+ Str ("configFilePath" , f ).
230
+ Msg ("Running logout command for profile" )
231
+
232
+ var performLogout bool
233
+ if ! noPrompt {
234
+ performLogout = promptForInteractiveYesNo (
235
+ fmt .Sprintf (
236
+ "Are you sure you want to remove profile '%s' from '%s?" , p , f ,
237
+ ),
238
+ )
239
+ if ! performLogout {
240
+ log .Info ().Msg ("Logout profile cancelled" )
241
+ fmt .Println (fmt .Sprintf ("Logout profile '%s' in '%s' cancelled." , p , f ))
242
+ return nil
243
+ }
244
+ }
245
+
246
+ log .Debug ().
247
+ Str ("configFilePath" , f ).
248
+ Msg ("Reading config file" )
249
+ config , err := auth_providers .ReadConfigFromJSON (f )
250
+ if err != nil {
251
+ log .Error ().
252
+ Err (err ).
253
+ Str ("configFilePath" , f ).
254
+ Str ("profile" , p ).
255
+ Msg ("unable to read config file, logout failed" )
256
+ return err
257
+ } else if config == nil {
258
+ log .Error ().
259
+ Str ("configFilePath" , f ).
260
+ Str ("profile" , p ).
261
+ Msg ("config file is empty, unable to logout" )
262
+ return fmt .Errorf ("config file is empty, unable to logout profile '%s'" , p )
263
+ }
264
+
265
+ // check if profile exists
266
+ if _ , ok := config .Servers [p ]; ! ok {
267
+ log .Error ().
268
+ Str ("profile" , p ).
269
+ Msg ("profile does not exist, unable to logout" )
270
+ return fmt .Errorf ("profile '%s' does not exist, unable to logout" , p )
271
+ }
272
+ delete (config .Servers , p )
273
+ wErr := auth_providers .WriteConfigToJSON (f , config )
274
+ if wErr != nil {
275
+ log .Error ().
276
+ Err (wErr ).
277
+ Str ("configFilePath" , f ).
278
+ Str ("profile" , p ).
279
+ Msg ("unable to write config file, logout failed" )
280
+ return wErr
281
+ }
282
+ log .Info ().
283
+ Str ("configFilePath" , f ).
284
+ Str ("profile" , p ).
285
+ Msg ("Profile removed successfully" )
286
+ return nil
287
+ }
288
+
167
289
func init () {
168
290
RootCmd .AddCommand (logoutCmd )
169
291
}
0 commit comments