File tree 1 file changed +21
-2
lines changed
1 file changed +21
-2
lines changed Original file line number Diff line number Diff line change 5
5
package cmd
6
6
7
7
import (
8
+ "bufio"
8
9
"encoding/pem"
9
10
"fmt"
10
11
"os"
12
+ "strings"
11
13
12
14
"code.gitea.io/gitea/modules/generate"
13
15
@@ -114,8 +116,19 @@ func runGenerateSecretKey(c *cli.Context) error {
114
116
}
115
117
116
118
func runGenerateKeyPair (c * cli.Context ) error {
117
- keytype := c .String ("type" )
118
119
file := c .String ("file" )
120
+
121
+ // Check if file exists to prevent overwrites
122
+ if _ , err := os .Stat (file ); err == nil {
123
+ scanner := bufio .NewScanner (os .Stdin )
124
+ fmt .Printf ("%s already exists.\n Overwrite (y/n)? " , file )
125
+ scanner .Scan ()
126
+ if strings .ToLower (strings .TrimSpace (scanner .Text ())) != "y" {
127
+ fmt .Println ("Aborting" )
128
+ return nil
129
+ }
130
+ }
131
+ keytype := c .String ("type" )
119
132
bits := c .Int ("bits" )
120
133
// provide defaults for bits, ed25519 ignores bit length so it's omitted
121
134
if bits == 0 {
@@ -139,5 +152,11 @@ func runGenerateKeyPair(c *cli.Context) error {
139
152
if err != nil {
140
153
return err
141
154
}
142
- return os .WriteFile (file + ".pub" , ssh .MarshalAuthorizedKey (pub ), 0o644 )
155
+ fmt .Printf ("Your identification has been saved in %s\n " , file )
156
+ err = os .WriteFile (file + ".pub" , ssh .MarshalAuthorizedKey (pub ), 0o644 )
157
+ if err != nil {
158
+ return err
159
+ }
160
+ fmt .Printf ("Your public key has been saved in %s" , file + ".pub" )
161
+ return nil
143
162
}
You can’t perform that action at this time.
0 commit comments