@@ -85,6 +85,8 @@ func EditServer(server *config.Server, sess *ssh.Session) (*config.Server, error
85
85
// AddServerSSHUser add server ssh user
86
86
func AddServerSSHUser (serverKey string , sess * ssh.Session ) (* string , * config.SSHUser , error ) {
87
87
logger .Logger .Info ("Add a server ssh user." )
88
+ var runningErr * error
89
+
88
90
stdio := utils .SessIO (sess )
89
91
usernamePui := sshUserNamePrompt ("" , stdio )
90
92
server := (* config .Conf .Servers )[serverKey ]
@@ -124,9 +126,10 @@ func AddServerSSHUser(serverKey string, sess *ssh.Session) (*string, *config.SSH
124
126
return nil , nil , err
125
127
}
126
128
} else {
129
+ defaultPath := fmt .Sprintf ("~/.ssh/id_%s" , strings .ReplaceAll (server .Host , "." , "_" ))
127
130
identityFilePui = identityFilePrompt (
128
131
"Enter new identity file path" ,
129
- "" ,
132
+ defaultPath ,
130
133
stdio ,
131
134
FileNotExited ("Identity File" ),
132
135
IsNotDir (),
@@ -158,14 +161,27 @@ func AddServerSSHUser(serverKey string, sess *ssh.Session) (*string, *config.SSH
158
161
if err != nil {
159
162
return nil , nil , err
160
163
}
164
+
165
+ runningErr = & err
166
+ defer func () {
167
+ if * runningErr != nil && identityFile != "" {
168
+ if utils .FileExited (identityFile ) {
169
+ os .Remove (utils .FilePath (identityFile ))
170
+ }
171
+ }
172
+ }()
173
+
161
174
_ , pubKeyFile , err := sshd .GenKey (identityFile )
162
175
if err != nil {
163
176
return nil , nil , err
164
177
}
178
+
165
179
times := 0
166
180
for {
167
181
if times > 2 {
168
- return nil , nil , errors .New ("Failed to copy public key to the server. " )
182
+ err = errors .New ("Failed to copy public key to the server. " )
183
+ runningErr = & err
184
+ return nil , nil , err
169
185
}
170
186
serverPasswdPui := promptui.Prompt {
171
187
Label : "Server password (use to send your public key to the server). " ,
@@ -181,7 +197,7 @@ func AddServerSSHUser(serverKey string, sess *ssh.Session) (*string, *config.SSH
181
197
continue
182
198
}
183
199
184
- err = sshd .CopyID (
200
+ _ , err = sshd .CopyID (
185
201
username ,
186
202
server .Host ,
187
203
server .Port ,
@@ -201,12 +217,18 @@ func AddServerSSHUser(serverKey string, sess *ssh.Session) (*string, *config.SSH
201
217
allowAllUserPui := allowAllUserPrompt ("" , stdio )
202
218
203
219
allAllUser , err := allowAllUserPui .Run ()
220
+ if err != nil {
221
+ runningErr = & err
222
+ return nil , nil , err
223
+ }
224
+
204
225
allowUsersStr := ""
205
226
if allAllUser == "no" {
206
227
allowUsersPui := allowUsersPrompt ("" , stdio )
207
228
208
229
allowUsersStr , err = allowUsersPui .Run ()
209
230
if err != nil {
231
+ runningErr = & err
210
232
return nil , nil , err
211
233
}
212
234
}
@@ -317,6 +339,8 @@ func GetEditedServersMenu(
317
339
// EditSSHUser EditSSHUser
318
340
func EditSSHUser (server * config.Server , sshUser * config.SSHUser , sess * ssh.Session ) (* config.SSHUser , error ) {
319
341
logger .Logger .Info ("Delete ssh user" )
342
+ var runningErr * error
343
+
320
344
stdio := utils .SessIO (sess )
321
345
usernamePui := sshUserNamePrompt (sshUser .SSHUsername , stdio )
322
346
@@ -353,9 +377,10 @@ func EditSSHUser(server *config.Server, sshUser *config.SSHUser, sess *ssh.Sessi
353
377
return nil , err
354
378
}
355
379
} else {
380
+ defaultPath := fmt .Sprintf ("~/.ssh/id_%s" , strings .ReplaceAll (server .Host , "." , "_" ))
356
381
identityFilePui = identityFilePrompt (
357
382
"Enter new identity file path" ,
358
- "" ,
383
+ defaultPath ,
359
384
stdio ,
360
385
FileNotExited ("Identity File" ),
361
386
IsNotDir (),
@@ -387,14 +412,27 @@ func EditSSHUser(server *config.Server, sshUser *config.SSHUser, sess *ssh.Sessi
387
412
if err != nil {
388
413
return nil , err
389
414
}
415
+
416
+ runningErr = & err
417
+ defer func () {
418
+ if * runningErr != nil && identityFile != "" {
419
+ if utils .FileExited (identityFile ) {
420
+ os .Remove (utils .FilePath (identityFile ))
421
+ }
422
+ }
423
+ }()
424
+
390
425
_ , pubKeyFile , err := sshd .GenKey (identityFile )
391
426
if err != nil {
392
427
return nil , err
393
428
}
429
+
394
430
times := 0
395
431
for {
396
432
if times > 2 {
397
- return nil , errors .New ("Failed to copy public key to the server. " )
433
+ err = errors .New ("Failed to copy public key to the server. " )
434
+ runningErr = & err
435
+ return nil , err
398
436
}
399
437
serverPasswdPui := promptui.Prompt {
400
438
Label : "Server password (use to send your public key to the server). " ,
@@ -410,7 +448,7 @@ func EditSSHUser(server *config.Server, sshUser *config.SSHUser, sess *ssh.Sessi
410
448
continue
411
449
}
412
450
413
- err = sshd .CopyID (
451
+ _ , err = sshd .CopyID (
414
452
username ,
415
453
server .Host ,
416
454
server .Port ,
@@ -433,11 +471,21 @@ func EditSSHUser(server *config.Server, sshUser *config.SSHUser, sess *ssh.Sessi
433
471
)
434
472
435
473
allAllUser , err := allowAllUserPui .Run ()
474
+ if err != nil {
475
+ runningErr = & err
476
+ return nil , err
477
+ }
478
+
436
479
allowUsersStr := ""
437
480
if allAllUser == "no" {
438
- allowUsersPui := allowUsersPrompt (strings .Join (* sshUser .AllowUsers , "," ), stdio )
481
+ defaultUsers := ""
482
+ if sshUser .AllowUsers != nil {
483
+ defaultUsers = strings .Join (* sshUser .AllowUsers , "," )
484
+ }
485
+ allowUsersPui := allowUsersPrompt (defaultUsers , stdio )
439
486
allowUsersStr , err = allowUsersPui .Run ()
440
487
if err != nil {
488
+ runningErr = & err
441
489
return nil , err
442
490
}
443
491
}
@@ -455,7 +503,7 @@ func EditSSHUser(server *config.Server, sshUser *config.SSHUser, sess *ssh.Sessi
455
503
AllowUsers : allowUsers ,
456
504
}
457
505
458
- return newSSHUser , nil
506
+ return newSSHUser , err
459
507
}
460
508
461
509
// DelSSHUser DelSSHUser
0 commit comments