Browse Source

feat: key 命令

runningwater 6 months ago
parent
commit
b9a2cc13c3
4 changed files with 42 additions and 1 deletions
  1. 1 0
      CHANGELOG.md
  2. 22 0
      app/cmd/key.go
  3. 1 1
      main.go
  4. 18 0
      pkg/helpers/helpers.go

+ 1 - 0
CHANGELOG.md

@@ -7,6 +7,7 @@
 ### ⚙️ 杂项任务
 
 - Console 包
+- Git-cliff generator changelog files
 
 ### 🐛 Bug 修复
 

+ 22 - 0
app/cmd/key.go

@@ -0,0 +1,22 @@
+package cmd
+
+import (
+	"github.com/runningwater/gohub/pkg/console"
+	"github.com/runningwater/gohub/pkg/helpers"
+	"github.com/spf13/cobra"
+)
+
+var CmdKey = &cobra.Command{
+	Use:   "key",
+	Short: "Generate App key, will print the generated key",
+	Run:   runKeyGenerate,
+	Args:  cobra.NoArgs,
+}
+
+func runKeyGenerate(cmd *cobra.Command, args []string) {
+	console.Success("---")
+	console.Success("App Key:")
+	console.Success(helpers.RandomString(32))
+	console.Success("---")
+	console.Warning("please go to .env file to change the APP_KEY option")
+}

+ 1 - 1
main.go

@@ -46,7 +46,7 @@ func main() {
 	// 注册子命令
 	rootCmd.AddCommand(
 		cmd.CmdServe,
-		// cmd.CmdKey,
+		cmd.CmdKey,
 		// cmd.CmdPlay,
 	)
 

+ 18 - 0
pkg/helpers/helpers.go

@@ -5,10 +5,17 @@ import (
 	"crypto/rand"
 	"fmt"
 	"io"
+	mathrand "math/rand/v2"
 	"reflect"
 	"time"
 )
 
+type helper struct{}
+
+func (h helper) Uint64() uint64 {
+	return uint64(time.Now().UnixNano())
+}
+
 // Empty 类似于 PHP 的 empty() 函数
 func Empty(val any) bool {
 	if val == nil {
@@ -55,6 +62,17 @@ func RandomNumber(length int) string {
 	return string(b)
 }
 
+// RandomString 生成长度为 length 的随机字符串
+func RandomString(length int) string {
+	mathrand.New(&helper{})
+	letters := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
+	b := make([]byte, length)
+	for i := range b {
+		b[i] = letters[mathrand.IntN(len(letters))]
+	}
+	return string(b)
+}
+
 // FirstElement 获取 args[0],避免 panic
 func FirstElement(args []string) string {
 	if len(args) > 0 {