Skip to content

Commit 0372c4b

Browse files
committed
add overwrite protetion and prompt
1 parent f8e81df commit 0372c4b

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

Diff for: cmd/generate.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
package cmd
66

77
import (
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

116118
func 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.\nOverwrite (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
}

0 commit comments

Comments
 (0)