File tree Expand file tree Collapse file tree 1 file changed +21
-2
lines changed Expand file tree Collapse file tree 1 file changed +21
-2
lines changed Original file line number Diff line number Diff line change 55package cmd
66
77import (
8+ "bufio"
89 "encoding/pem"
910 "fmt"
1011 "os"
12+ "strings"
1113
1214 "code.gitea.io/gitea/modules/generate"
1315
@@ -114,8 +116,19 @@ func runGenerateSecretKey(c *cli.Context) error {
114116}
115117
116118func runGenerateKeyPair (c * cli.Context ) error {
117- keytype := c .String ("type" )
118119 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" )
119132 bits := c .Int ("bits" )
120133 // provide defaults for bits, ed25519 ignores bit length so it's omitted
121134 if bits == 0 {
@@ -139,5 +152,11 @@ func runGenerateKeyPair(c *cli.Context) error {
139152 if err != nil {
140153 return err
141154 }
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
143162}
You can’t perform that action at this time.
0 commit comments