fix: register draft key on the root command with a working --size flag

This commit is contained in:
FernandoJVideira committed 2026-08-25 21:33:06 +01:00
1 parent 29f94539e0
commit a514521c6e
1 file changed
+12 -18
+12 -18
View File
@@ -6,32 +6,26 @@ import (
"github.com/spf13/cobra"
)
const defaultKeySize = 32
var keySize int
func init() {
newCmd.AddCommand(keyCmd)
rootCmd.AddCommand(keyCmd)
keyCmd.Flags().IntVar(&keySize, "size", defaultKeySize, "Length of the generated key")
}
var size int
var defaultSize = 32
var keyCmd = &cobra.Command{
Use: "key [size]",
Use: "key",
Short: "Generate a new cryptographic key",
Long: `Generate a new cryptographic key for use in Draft projects.`,
Run: func(cmd *cobra.Command, args []string) {
var keyLength int
if cmd.Flags().Lookup("size").Changed {
keyLength = size
} else {
keyLength = defaultSize
if cmd.Flags().Changed("size") && keySize < defaultKeySize {
fmt.Printf("❌ Key size must be at least %d for adequate security.\n", defaultKeySize)
return
}
if keyLength <= 32 {
fmt.Println("Warning: It is recommended to use a key size greater than 32 for better security.")
fmt.Println("Generating a key of default size:", defaultSize)
keyLength = defaultSize
}
key := randomString(keyLength)
println("Generated Key:", key)
key := randomString(keySize)
fmt.Println("Generated Key:", key)
},
}