From 4b808cc15c0a1fda29df3e84c79010ec0dca94a4 Mon Sep 17 00:00:00 2001 From: fabiante Date: Thu, 14 Dec 2023 09:53:22 +0100 Subject: [PATCH 1/5] Add reusable func to generate project id based on domain --- pkg/dexpro/uuid.go | 14 ++++++++++++++ pkg/dexpro/uuid_test.go | 11 +++++++++++ 2 files changed, 25 insertions(+) create mode 100644 pkg/dexpro/uuid.go create mode 100644 pkg/dexpro/uuid_test.go diff --git a/pkg/dexpro/uuid.go b/pkg/dexpro/uuid.go new file mode 100644 index 0000000..cee4adb --- /dev/null +++ b/pkg/dexpro/uuid.go @@ -0,0 +1,14 @@ +package dexpro + +import "github.com/google/uuid" + +// GenerateProjectID generates a UUID from a domain name. +// +// This panics if the hardcoded namespace UUID is not valid. That should never happen. If it does, +// contact a library maintainer. +func GenerateProjectID(domain string) uuid.UUID { + // The namespace is a predefined UUID. This will not change and is thus hardcoded. + namespace := uuid.MustParse("4befef11-1d7c-5958-8343-7aab6779a086") + + return uuid.NewSHA1(namespace, []byte(domain)) +} diff --git a/pkg/dexpro/uuid_test.go b/pkg/dexpro/uuid_test.go new file mode 100644 index 0000000..d8ac313 --- /dev/null +++ b/pkg/dexpro/uuid_test.go @@ -0,0 +1,11 @@ +package dexpro + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGenerateProjectID(t *testing.T) { + assert.Equal(t, "659bc69a-2b8f-5746-99f5-4ba0f0a09238", GenerateProjectID("squeeze.docker.localhost").String()) +} From dfdf183eaff6dab5a4e61d2f4c7dd561601329b9 Mon Sep 17 00:00:00 2001 From: fabiante Date: Thu, 14 Dec 2023 09:37:39 +0100 Subject: [PATCH 2/5] Add 'generate project-id' cmd --- cmd/gen.go | 15 +++++++++++++++ cmd/gen_project_id.go | 29 +++++++++++++++++++++++++++++ cmd/root.go | 2 ++ 3 files changed, 46 insertions(+) create mode 100644 cmd/gen.go create mode 100644 cmd/gen_project_id.go diff --git a/cmd/gen.go b/cmd/gen.go new file mode 100644 index 0000000..5eea00d --- /dev/null +++ b/cmd/gen.go @@ -0,0 +1,15 @@ +package cmd + +import "github.com/spf13/cobra" + +func newGenCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "generate", + Aliases: []string{"gen"}, + Short: "Generate stuff", + } + + cmd.AddCommand(newGenProjectIDCommand()) + + return cmd +} diff --git a/cmd/gen_project_id.go b/cmd/gen_project_id.go new file mode 100644 index 0000000..cb5cf85 --- /dev/null +++ b/cmd/gen_project_id.go @@ -0,0 +1,29 @@ +package cmd + +import ( + "fmt" + + "github.com/DEXPRO-Solutions-GmbH/xd/pkg/dexpro" + "github.com/spf13/cobra" +) + +func newGenProjectIDCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "project-id ", + Short: "Generate a project ID based on a Domain", + Long: `Generate a project ID based on a Domain. Result is printed to stdout. + +The generated ID will always be the same since it is a UUIDv5 derived from a hardcoded namespace UUID.`, + Args: cobra.ExactArgs(1), + } + + cmd.Run = func(cmd *cobra.Command, args []string) { + domain := args[0] + + tenantID := dexpro.GenerateProjectID(domain) + + fmt.Println(tenantID) + } + + return cmd +} diff --git a/cmd/root.go b/cmd/root.go index 62fc022..eb34579 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -8,5 +8,7 @@ func NewRootCmd() *cobra.Command { Short: "DEXPRO Developer CLI with an X", } + cmd.AddCommand(newGenCmd()) + return cmd } From cf0d36c7a60eddcfe4478334cdb4e7bcdcdf99f7 Mon Sep 17 00:00:00 2001 From: fabiante Date: Thu, 14 Dec 2023 09:54:54 +0100 Subject: [PATCH 3/5] Update deps --- go.mod | 5 +++++ go.sum | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/go.mod b/go.mod index 3c4e193..452daac 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,11 @@ require ( ) require ( + github.com/davecgh/go-spew v1.1.1 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/stretchr/objx v0.5.0 // indirect + github.com/stretchr/testify v1.8.4 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 1f380d5..db6c1e9 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,27 @@ github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From cda9019f430d94513087d21c9badb5deef6c1769 Mon Sep 17 00:00:00 2001 From: fabiante Date: Wed, 27 Dec 2023 16:01:04 +0100 Subject: [PATCH 4/5] Add test steps to CI --- .github/workflows/build.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index afe59da..236228d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -27,3 +27,12 @@ jobs: - name: Lint run: go vet -v ./... + + - name: Install locally + run: go install ./cmd/xd + + - name: Test installed binary + run: xd --help + + - name: Test installed binary + run: xd gen project-id customer.squeeze.one From 2c2992e97277d648a173ae3a0aad38d0e3a5e544 Mon Sep 17 00:00:00 2001 From: fabiante Date: Wed, 27 Dec 2023 16:06:19 +0100 Subject: [PATCH 5/5] Update README --- README.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7d68605..607130e 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,22 @@ This project implements developer tools for DEXPRO employees. If you want to use these tools, you can install them from source: -```bash +```shell go install github.com/DEXPRO-Solutions-GmbH/xd/cmd/xd@latest ``` + +Check if your installation worked. Simply run `xd --help` like this: + +```shell +xd --help + +DEXPRO Developer CLI with an X + +Usage: + xd [command] + +Available Commands: + completion Generate the autocompletion script for the specified shell + help Help about any command + ... ... +```