Commit 11bce6c

Karn Wong <[email protected]>
2025-07-28 04:09:29
refactor(generateSSHKey): move to internal
1 parent 17bf1a8
Changed files (2)
cmd
internal
generate
cmd/generate/generate_ssh_key.go
@@ -1,99 +1,16 @@
 package generate
 
 import (
-	"crypto"
-	"crypto/ed25519"
-	"encoding/base64"
-	"encoding/pem"
-	"fmt"
-	"os"
-	"path/filepath"
-
-	"github.com/rs/zerolog/log"
+	"github.com/kahnwong/swissknife/internal/generate"
 	"github.com/spf13/cobra"
-	"golang.org/x/crypto/ssh"
 )
 
-// helpers
-func writeStringToFile(filePath string, data string, permission os.FileMode) {
-	file, err := os.Create(filePath)
-	if err != nil {
-		log.Fatal().Msgf("Failed to create file %s", filePath)
-	}
-
-	_, err = file.WriteString(data)
-	if err != nil {
-		log.Fatal().Msgf("Failed to write to file %s", filePath)
-	}
-
-	err = file.Chmod(permission)
-	if err != nil {
-		log.Fatal().Msgf("Failed to change file permissions")
-	}
-}
-
-func returnKeyPath(fileName string) string {
-	currentDir, err := os.Getwd()
-	if err != nil {
-		log.Fatal().Msgf("Failed to get current directory")
-	}
-
-	keyPath := filepath.Join(currentDir, fileName)
-	keyPath = fmt.Sprintf("%s.pem", keyPath)
-
-	return keyPath
-}
-
-// main
-func generateSSHKeyEDSA() (string, string) {
-	// Generate a new Ed25519 private key
-	//// If rand is nil, crypto/rand.Reader will be used
-	public, private, err := ed25519.GenerateKey(nil)
-	if err != nil {
-		log.Fatal().Msgf("Failed to generate ed25519 key")
-	}
-
-	// public key
-	publicKey, err := ssh.NewPublicKey(public)
-	if err != nil {
-		log.Fatal().Msgf("Failed to create public key")
-	}
-	publicKeyString := fmt.Sprintf("ssh-ed25519 %s", base64.StdEncoding.EncodeToString(publicKey.Marshal()))
-
-	// private key
-	//// p stands for pem
-	p, err := ssh.MarshalPrivateKey(crypto.PrivateKey(private), "")
-	if err != nil {
-		log.Fatal().Msgf("Failed to marshal private key")
-	}
-	privateKeyPem := pem.EncodeToMemory(p)
-	privateKeyString := string(privateKeyPem)
-
-	return publicKeyString, privateKeyString
-}
-
 var generateSSHKeyCmd = &cobra.Command{
 	Use:   "ssh-key",
 	Short: "Create SSH key",
 	Long:  `Create SSH key`,
 	Run: func(cmd *cobra.Command, args []string) {
-		//init
-		if len(args) == 0 {
-			fmt.Println("Please specify key name")
-			os.Exit(1)
-		}
-
-		// main
-		publicKeyString, privateKeyString := generateSSHKeyEDSA()
-
-		// write key to file
-		publicKeyFilename := fmt.Sprintf("%s.pub", args[0])
-		writeStringToFile(publicKeyFilename, publicKeyString, 0644)
-
-		privateKeyFilename := fmt.Sprintf("%s.pem", args[0])
-		writeStringToFile(privateKeyFilename, privateKeyString, 0600)
-
-		fmt.Printf("SSH key created at: %s\n", returnKeyPath(args[0]))
+		generate.SSHKey(args)
 	},
 }
 
internal/generate/ssh_key.go
@@ -0,0 +1,92 @@
+package generate
+
+import (
+	"crypto"
+	"crypto/ed25519"
+	"encoding/base64"
+	"encoding/pem"
+	"fmt"
+	"os"
+	"path/filepath"
+
+	"github.com/rs/zerolog/log"
+	"golang.org/x/crypto/ssh"
+)
+
+// helpers
+func writeStringToFile(filePath string, data string, permission os.FileMode) {
+	file, err := os.Create(filePath)
+	if err != nil {
+		log.Fatal().Msgf("Failed to create file %s", filePath)
+	}
+
+	_, err = file.WriteString(data)
+	if err != nil {
+		log.Fatal().Msgf("Failed to write to file %s", filePath)
+	}
+
+	err = file.Chmod(permission)
+	if err != nil {
+		log.Fatal().Msgf("Failed to change file permissions")
+	}
+}
+
+func returnKeyPath(fileName string) string {
+	currentDir, err := os.Getwd()
+	if err != nil {
+		log.Fatal().Msgf("Failed to get current directory")
+	}
+
+	keyPath := filepath.Join(currentDir, fileName)
+	keyPath = fmt.Sprintf("%s.pem", keyPath)
+
+	return keyPath
+}
+
+// main
+func generateSSHKeyEDSA() (string, string) {
+	// Generate a new Ed25519 private key
+	//// If rand is nil, crypto/rand.Reader will be used
+	public, private, err := ed25519.GenerateKey(nil)
+	if err != nil {
+		log.Fatal().Msgf("Failed to generate ed25519 key")
+	}
+
+	// public key
+	publicKey, err := ssh.NewPublicKey(public)
+	if err != nil {
+		log.Fatal().Msgf("Failed to create public key")
+	}
+	publicKeyString := fmt.Sprintf("ssh-ed25519 %s", base64.StdEncoding.EncodeToString(publicKey.Marshal()))
+
+	// private key
+	//// p stands for pem
+	p, err := ssh.MarshalPrivateKey(crypto.PrivateKey(private), "")
+	if err != nil {
+		log.Fatal().Msgf("Failed to marshal private key")
+	}
+	privateKeyPem := pem.EncodeToMemory(p)
+	privateKeyString := string(privateKeyPem)
+
+	return publicKeyString, privateKeyString
+}
+
+func SSHKey(args []string) {
+	//init
+	if len(args) == 0 {
+		fmt.Println("Please specify key name")
+		os.Exit(1)
+	}
+
+	// main
+	publicKeyString, privateKeyString := generateSSHKeyEDSA()
+
+	// write key to file
+	publicKeyFilename := fmt.Sprintf("%s.pub", args[0])
+	writeStringToFile(publicKeyFilename, publicKeyString, 0644)
+
+	privateKeyFilename := fmt.Sprintf("%s.pem", args[0])
+	writeStringToFile(privateKeyFilename, privateKeyString, 0600)
+
+	fmt.Printf("SSH key created at: %s\n", returnKeyPath(args[0]))
+}