Commit 82b278b

Karn Wong <[email protected]>
2023-12-29 16:16:46
add security: generate-password
1 parent 8c1230e
cmd/security/security.go
@@ -0,0 +1,16 @@
+package security
+
+import (
+	"fmt"
+
+	"github.com/spf13/cobra"
+)
+
+var Cmd = &cobra.Command{
+	Use:   "security",
+	Short: "Security tools",
+	Long:  `Security tools`,
+	Run: func(cmd *cobra.Command, args []string) {
+		fmt.Println("Please specify subcommand")
+	},
+}
cmd/security/security_generate_password.go
@@ -0,0 +1,39 @@
+package security
+
+import (
+	"fmt"
+	"log"
+
+	"github.com/sethvargo/go-password/password"
+
+	"github.com/fatih/color"
+	"github.com/spf13/cobra"
+)
+
+func generatePassword() string {
+	// Generate a password that is 64 characters long with 10 digits, 10 symbols,
+	// allowing upper and lower case letters, disallowing repeat characters.
+	res, err := password.Generate(32, 10, 10, false, false)
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	return res
+}
+
+var generatePasswordCmd = &cobra.Command{
+	Use:   "generate-password",
+	Short: "Generate password",
+	Long:  `Generate password`,
+	Run: func(cmd *cobra.Command, args []string) {
+		// main
+		color.Green("Security: generate password")
+
+		password := generatePassword()
+		fmt.Printf("%s\n", password)
+	},
+}
+
+func init() {
+	Cmd.AddCommand(generatePasswordCmd)
+}
cmd/root.go
@@ -4,6 +4,7 @@ import (
 	"os"
 
 	"github.com/kahnwong/swissknife/cmd/networking"
+	"github.com/kahnwong/swissknife/cmd/security"
 	"github.com/kahnwong/swissknife/cmd/ssh"
 	"github.com/spf13/cobra"
 )
@@ -25,5 +26,6 @@ func Execute() {
 func init() {
 	rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
 	rootCmd.AddCommand(networking.Cmd)
+	rootCmd.AddCommand(security.Cmd)
 	rootCmd.AddCommand(ssh.Cmd)
 }
go.mod
@@ -4,6 +4,7 @@ go 1.20
 
 require (
 	github.com/fatih/color v1.15.0
+	github.com/sethvargo/go-password v0.2.0
 	github.com/spf13/cobra v1.7.0
 	golang.org/x/crypto v0.17.0
 )
go.sum
@@ -9,6 +9,8 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
 github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
 github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
 github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
+github.com/sethvargo/go-password v0.2.0 h1:BTDl4CC/gjf/axHMaDQtw507ogrXLci6XRiLc7i/UHI=
+github.com/sethvargo/go-password v0.2.0/go.mod h1:Ym4Mr9JXLBycr02MFuVQ/0JHidNetSgbzutTr3zsYXE=
 github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
 github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
 github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=