-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement typeid cli, and add FromX constructors (#25)
This is the last typeid PR before I start sharing it publicly. It implements a `typeid` cli with three commands `new`, `encode`, and `decode`. Which let you generate a new typeid, encode a uuid as a typeid, and decode a typeid as a uuid. In the process add some `FromX` constructors in the `go` version of the library (which brings it to parity with our internal typescript version)
- Loading branch information
Showing
8 changed files
with
195 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package cli | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"go.jetpack.io/typeid" | ||
) | ||
|
||
func DecodeCmd() *cobra.Command { | ||
command := &cobra.Command{ | ||
Use: "decode <type_id>", | ||
Args: cobra.ExactArgs(1), | ||
Short: "Decode the given TypeID into a UUID", | ||
RunE: decodeCmd, | ||
SilenceErrors: true, | ||
SilenceUsage: true, | ||
} | ||
|
||
return command | ||
} | ||
|
||
func decodeCmd(cmd *cobra.Command, args []string) error { | ||
tid, err := typeid.FromString(args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
cmd.Printf("type: %s\n", tid.Type()) | ||
cmd.Printf("uuid: %s\n", tid.UUID()) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package cli | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"go.jetpack.io/typeid" | ||
) | ||
|
||
func EncodeCmd() *cobra.Command { | ||
command := &cobra.Command{ | ||
Use: "encode [<type_prefix>] <uuid>", | ||
Args: cobra.RangeArgs(1, 2), | ||
Short: "Encode the given UUID into a TypeID using the given type prefix", | ||
RunE: encodeCmd, | ||
SilenceErrors: true, | ||
SilenceUsage: true, | ||
} | ||
|
||
return command | ||
} | ||
|
||
func encodeCmd(cmd *cobra.Command, args []string) error { | ||
prefix := "" | ||
uuid := "" | ||
if len(args) == 1 { | ||
uuid = args[0] | ||
} else { | ||
prefix = args[0] | ||
uuid = args[1] | ||
} | ||
tid, err := typeid.FromUUID(prefix, uuid) | ||
if err != nil { | ||
return err | ||
} | ||
cmd.Println(tid) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package cli | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"go.jetpack.io/typeid" | ||
) | ||
|
||
func NewCmd() *cobra.Command { | ||
command := &cobra.Command{ | ||
Use: "new [<type_prefix>]", | ||
Args: cobra.MaximumNArgs(1), | ||
Short: "Generate a new TypeID using the given type prefix", | ||
RunE: newCmd, | ||
SilenceErrors: true, | ||
SilenceUsage: true, | ||
} | ||
|
||
return command | ||
} | ||
|
||
func newCmd(cmd *cobra.Command, args []string) error { | ||
prefix := "" | ||
if len(args) > 0 { | ||
prefix = strings.ToLower(args[0]) | ||
} | ||
tid, err := typeid.New(prefix) | ||
if err != nil { | ||
return err | ||
} | ||
cmd.Println(tid) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func RootCmd() *cobra.Command { | ||
command := &cobra.Command{ | ||
Use: "typeid", | ||
Short: "Type-safe, K-sortable, globally unique identifiers", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return cmd.Help() | ||
}, | ||
SilenceErrors: true, | ||
SilenceUsage: true, | ||
} | ||
command.AddCommand(NewCmd()) | ||
command.AddCommand(EncodeCmd()) | ||
command.AddCommand(DecodeCmd()) | ||
|
||
return command | ||
} | ||
|
||
func Execute(ctx context.Context, args []string) int { | ||
cmd := RootCmd() | ||
cmd.SetArgs(args) | ||
err := cmd.ExecuteContext(ctx) | ||
if err != nil { | ||
fmt.Printf("[Error] %v\n", err) | ||
return 1 | ||
} | ||
return 0 | ||
} | ||
|
||
func Main() { | ||
code := Execute(context.Background(), os.Args[1:]) | ||
os.Exit(code) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
module go.jetpack.io/typeid-cli | ||
|
||
go 1.20 | ||
|
||
require github.com/spf13/cobra v1.7.0 | ||
|
||
require ( | ||
github.com/inconshreveable/mousetrap v1.1.0 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= | ||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= | ||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= | ||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= | ||
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= | ||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,7 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"go.jetpack.io/typeid" | ||
) | ||
import "go.jetpack.io/typeid-cli/cli" | ||
|
||
func main() { | ||
if len(os.Args) != 2 { | ||
fmt.Println("Usage: typeid [<type_prefix>]") | ||
os.Exit(1) | ||
} | ||
|
||
prefix := os.Args[1] | ||
tid := typeid.Must(typeid.New(prefix)) | ||
fmt.Println(tid) | ||
cli.Main() | ||
} |