ZRY 1 month ago
parent
commit
80805d123d
10 changed files with 104 additions and 0 deletions
  1. 1 0
      .gitignore
  2. 8 0
      .idea/.gitignore
  3. 11 0
      .idea/base32-guid-gen.iml
  4. 7 0
      .idea/misc.xml
  5. 8 0
      .idea/modules.xml
  6. 6 0
      .idea/vcs.xml
  7. 5 0
      go.mod
  8. 2 0
      go.sum
  9. 13 0
      justfile
  10. 43 0
      main.go

+ 1 - 0
.gitignore

@@ -24,3 +24,4 @@ _testmain.go
 *.test
 *.prof
 
+/dist/

+ 8 - 0
.idea/.gitignore

@@ -0,0 +1,8 @@
+# 默认忽略的文件
+/shelf/
+/workspace.xml
+# 基于编辑器的 HTTP 客户端请求
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml

+ 11 - 0
.idea/base32-guid-gen.iml

@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="WEB_MODULE" version="4">
+  <component name="Go" enabled="true" />
+  <component name="NewModuleRootManager">
+    <content url="file://$MODULE_DIR$">
+      <excludeFolder url="file://$MODULE_DIR$/dist" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>

+ 7 - 0
.idea/misc.xml

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="XMakeProjectSettings">
+    <option name="currentArchitecture" value="x86" />
+    <option name="workingDirectory" value="$PROJECT_DIR$" />
+  </component>
+</project>

+ 8 - 0
.idea/modules.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/base32-guid-gen.iml" filepath="$PROJECT_DIR$/.idea/base32-guid-gen.iml" />
+    </modules>
+  </component>
+</project>

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="" vcs="Git" />
+  </component>
+</project>

+ 5 - 0
go.mod

@@ -0,0 +1,5 @@
+module git.swzry.com/ProjectNagae/base32-guid-gen
+
+go 1.21.6
+
+require github.com/google/uuid v1.6.0

+ 2 - 0
go.sum

@@ -0,0 +1,2 @@
+github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
+github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=

+ 13 - 0
justfile

@@ -0,0 +1,13 @@
+#!/usr/bin/env just --justfile
+
+jfdir := replace(justfile_directory(), "\\", "/")
+dist := jfdir / "dist"
+
+default:
+	just --list
+
+build:
+	go build -o {{ dist / "b32guid.exe" }}
+
+run:
+	{{ dist / "b32guid.exe" }}

+ 43 - 0
main.go

@@ -0,0 +1,43 @@
+package main
+
+import (
+	"encoding/base32"
+	"flag"
+	"fmt"
+	"github.com/google/uuid"
+)
+
+var flagHelp bool
+var flagPrefix string
+var flagLowerCase bool
+
+const UpperCaseEncoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
+const LowerCaseEncoding = "abcdefghijklmnopqrstuvwxyz234567"
+
+func main() {
+	flag.BoolVar(&flagHelp, "h", false, "show help")
+	flag.StringVar(&flagPrefix, "p", "", "add prefix for generated uuid")
+	flag.BoolVar(&flagLowerCase, "l", false, "lowercase encoding")
+	flag.Parse()
+	if flagHelp {
+		flag.PrintDefaults()
+		return
+	}
+	id := uuid.New()
+	b, err := id.MarshalBinary()
+	if err != nil {
+		fmt.Println("failed marshal uuid to []byte: ", err)
+		return
+	}
+	var encStr string
+	if flagLowerCase {
+		encStr = LowerCaseEncoding
+	} else {
+		encStr = UpperCaseEncoding
+	}
+	b32Encoding := base32.NewEncoding(encStr)
+	b32Encoding = b32Encoding.WithPadding('9')
+	b32Str := b32Encoding.EncodeToString(b)
+	out := fmt.Sprintf("%s%s", flagPrefix, b32Str)
+	fmt.Println(out)
+}