Skip to content

Commit

Permalink
Replace github.com/ccoVeille/go-safecast
Browse files Browse the repository at this point in the history
...with our own implementation that is more typesafe and more
convenient.
  • Loading branch information
c4rlo committed Jan 25, 2025
1 parent c40ce5c commit 5f08852
Show file tree
Hide file tree
Showing 29 changed files with 198 additions and 99 deletions.
16 changes: 8 additions & 8 deletions cmd/spire-server/cli/entry/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"flag"
"fmt"

"github.com/ccoveille/go-safecast"
"github.com/mitchellh/cli"
entryv1 "github.com/spiffe/spire-api-sdk/proto/spire/api/server/entry/v1"
"github.com/spiffe/spire-api-sdk/proto/spire/api/types"
"github.com/spiffe/spire/cmd/spire-server/util"
serverutil "github.com/spiffe/spire/cmd/spire-server/util"
commoncli "github.com/spiffe/spire/pkg/common/cli"
"github.com/spiffe/spire/pkg/common/cliprinter"
"github.com/spiffe/spire/pkg/common/idutil"
"github.com/spiffe/spire/pkg/common/util"
"google.golang.org/grpc/codes"
)

Expand All @@ -23,7 +23,7 @@ func NewCreateCommand() cli.Command {
}

func newCreateCommand(env *commoncli.Env) cli.Command {
return util.AdaptCommand(env, &createCommand{env: env})
return serverutil.AdaptCommand(env, &createCommand{env: env})
}

type createCommand struct {
Expand Down Expand Up @@ -106,7 +106,7 @@ func (c *createCommand) AppendFlags(f *flag.FlagSet) {
cliprinter.AppendFlagWithCustomPretty(&c.printer, f, c.env, prettyPrintCreate)
}

func (c *createCommand) Run(ctx context.Context, _ *commoncli.Env, serverClient util.ServerClient) error {
func (c *createCommand) Run(ctx context.Context, _ *commoncli.Env, serverClient serverutil.ServerClient) error {
if err := c.validate(); err != nil {
return err
}
Expand Down Expand Up @@ -177,12 +177,12 @@ func (c *createCommand) parseConfig() ([]*types.Entry, error) {
return nil, err
}

x509SvidTTL, err := safecast.ToInt32(c.x509SVIDTTL)
x509SvidTTL, err := util.CheckedCast[int32](c.x509SVIDTTL)
if err != nil {
return nil, fmt.Errorf("X509 SVID TTL: %w", err)
}

jwtSvidTTL, err := safecast.ToInt32(c.jwtSVIDTTL)
jwtSvidTTL, err := util.CheckedCast[int32](c.jwtSVIDTTL)
if err != nil {
return nil, fmt.Errorf("JWT SVID TTL: %w", err)
}
Expand All @@ -202,7 +202,7 @@ func (c *createCommand) parseConfig() ([]*types.Entry, error) {

selectors := []*types.Selector{}
for _, s := range c.selectors {
cs, err := util.ParseSelector(s)
cs, err := serverutil.ParseSelector(s)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -266,7 +266,7 @@ func prettyPrintCreate(env *commoncli.Env, results ...any) error {

for _, r := range failed {
env.ErrPrintf("Failed to create the following entry (code: %s, msg: %q):\n",
codes.Code(safecast.MustConvert[uint32](r.Status.Code)),
util.MustCast[codes.Code](r.Status.Code),
r.Status.Message)
printEntry(r.Entry, env.ErrPrintf)
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/spire-server/cli/entry/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"io"
"os"

"github.com/ccoveille/go-safecast"
"github.com/mitchellh/cli"
entryv1 "github.com/spiffe/spire-api-sdk/proto/spire/api/server/entry/v1"
"github.com/spiffe/spire/cmd/spire-server/util"
serverutil "github.com/spiffe/spire/cmd/spire-server/util"
commoncli "github.com/spiffe/spire/pkg/common/cli"
"github.com/spiffe/spire/pkg/common/cliprinter"
"github.com/spiffe/spire/pkg/common/util"
"google.golang.org/grpc/codes"
)

Expand All @@ -23,7 +23,7 @@ func NewDeleteCommand() cli.Command {
}

func newDeleteCommand(env *commoncli.Env) cli.Command {
return util.AdaptCommand(env, &deleteCommand{env: env})
return serverutil.AdaptCommand(env, &deleteCommand{env: env})
}

type deleteCommand struct {
Expand Down Expand Up @@ -71,7 +71,7 @@ func parseEntryDeleteJSON(path string) ([]string, error) {
return batchDeleteEntryRequest.Ids, nil
}

func (c *deleteCommand) Run(ctx context.Context, _ *commoncli.Env, serverClient util.ServerClient) error {
func (c *deleteCommand) Run(ctx context.Context, _ *commoncli.Env, serverClient serverutil.ServerClient) error {
if err := c.validate(); err != nil {
return err
}
Expand Down Expand Up @@ -136,7 +136,7 @@ func (c *deleteCommand) prettyPrintDelete(env *commoncli.Env, results ...any) er
for _, result := range failed {
env.ErrPrintf("Failed to delete entry with ID %s (code: %s, msg: %q)\n",
result.Id,
codes.Code(safecast.MustConvert[uint32](result.Status.Code)),
util.MustCast[codes.Code](result.Status.Code),
result.Status.Message)
}

Expand Down
16 changes: 8 additions & 8 deletions cmd/spire-server/cli/entry/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"flag"
"fmt"

"github.com/ccoveille/go-safecast"
"github.com/mitchellh/cli"
entryv1 "github.com/spiffe/spire-api-sdk/proto/spire/api/server/entry/v1"
"github.com/spiffe/spire-api-sdk/proto/spire/api/types"
"github.com/spiffe/spire/cmd/spire-server/util"
serverutil "github.com/spiffe/spire/cmd/spire-server/util"
commoncli "github.com/spiffe/spire/pkg/common/cli"
"github.com/spiffe/spire/pkg/common/cliprinter"
"github.com/spiffe/spire/pkg/common/util"
"google.golang.org/grpc/codes"
)

Expand All @@ -22,7 +22,7 @@ func NewUpdateCommand() cli.Command {
}

func newUpdateCommand(env *commoncli.Env) cli.Command {
return util.AdaptCommand(env, &updateCommand{env: env})
return serverutil.AdaptCommand(env, &updateCommand{env: env})
}

type updateCommand struct {
Expand Down Expand Up @@ -101,7 +101,7 @@ func (c *updateCommand) AppendFlags(f *flag.FlagSet) {
cliprinter.AppendFlagWithCustomPretty(&c.printer, f, c.env, prettyPrintUpdate)
}

func (c *updateCommand) Run(ctx context.Context, _ *commoncli.Env, serverClient util.ServerClient) error {
func (c *updateCommand) Run(ctx context.Context, _ *commoncli.Env, serverClient serverutil.ServerClient) error {
if err := c.validate(); err != nil {
return err
}
Expand Down Expand Up @@ -171,12 +171,12 @@ func (c *updateCommand) parseConfig() ([]*types.Entry, error) {
return nil, err
}

x509SvidTTL, err := safecast.ToInt32(c.x509SvidTTL)
x509SvidTTL, err := util.CheckedCast[int32](c.x509SvidTTL)
if err != nil {
return nil, fmt.Errorf("X509 SVID TTL: %w", err)
}

jwtSvidTTL, err := safecast.ToInt32(c.jwtSvidTTL)
jwtSvidTTL, err := util.CheckedCast[int32](c.jwtSvidTTL)
if err != nil {
return nil, fmt.Errorf("JWT SVID TTL: %w", err)
}
Expand All @@ -195,7 +195,7 @@ func (c *updateCommand) parseConfig() ([]*types.Entry, error) {

selectors := []*types.Selector{}
for _, s := range c.selectors {
cs, err := util.ParseSelector(s)
cs, err := serverutil.ParseSelector(s)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -252,7 +252,7 @@ func prettyPrintUpdate(env *commoncli.Env, results ...any) error {
// Print entries that failed to be updated
for _, r := range failed {
env.ErrPrintf("Failed to update the following entry (code: %s, msg: %q):\n",
codes.Code(safecast.MustConvert[uint32](r.Status.Code)),
util.MustCast[codes.Code](r.Status.Code),
r.Status.Message)
printEntry(r.Entry, env.ErrPrintf)
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/spire-server/cli/federation/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"flag"
"fmt"

"github.com/ccoveille/go-safecast"
"github.com/mitchellh/cli"
trustdomainv1 "github.com/spiffe/spire-api-sdk/proto/spire/api/server/trustdomain/v1"
"github.com/spiffe/spire-api-sdk/proto/spire/api/types"
"github.com/spiffe/spire/cmd/spire-server/util"
serverutil "github.com/spiffe/spire/cmd/spire-server/util"
commoncli "github.com/spiffe/spire/pkg/common/cli"
"github.com/spiffe/spire/pkg/common/cliprinter"
"github.com/spiffe/spire/pkg/common/util"
"google.golang.org/grpc/codes"
)

Expand All @@ -27,7 +27,7 @@ func NewCreateCommand() cli.Command {
}

func newCreateCommand(env *commoncli.Env) cli.Command {
return util.AdaptCommand(env, &createCommand{env: env})
return serverutil.AdaptCommand(env, &createCommand{env: env})
}

type createCommand struct {
Expand All @@ -53,7 +53,7 @@ func (c *createCommand) AppendFlags(f *flag.FlagSet) {
cliprinter.AppendFlagWithCustomPretty(&c.printer, f, c.env, c.prettyPrintCreate)
}

func (c *createCommand) Run(ctx context.Context, _ *commoncli.Env, serverClient util.ServerClient) error {
func (c *createCommand) Run(ctx context.Context, _ *commoncli.Env, serverClient serverutil.ServerClient) error {
federationRelationships, err := getRelationships(c.config, c.path)
if err != nil {
return err
Expand Down Expand Up @@ -102,7 +102,7 @@ func (c *createCommand) prettyPrintCreate(env *commoncli.Env, results ...any) er
for _, r := range failed {
env.Println()
env.ErrPrintf("Failed to create the following federation relationship (code: %s, msg: %q):\n",
codes.Code(safecast.MustConvert[uint32](r.Status.Code)),
util.MustCast[codes.Code](r.Status.Code),
r.Status.Message)
printFederationRelationship(r.FederationRelationship, env.ErrPrintf)
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/spire-server/cli/federation/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"flag"
"fmt"

"github.com/ccoveille/go-safecast"
"github.com/mitchellh/cli"
trustdomainv1 "github.com/spiffe/spire-api-sdk/proto/spire/api/server/trustdomain/v1"
"github.com/spiffe/spire-api-sdk/proto/spire/api/types"
"github.com/spiffe/spire/cmd/spire-server/util"
serverutil "github.com/spiffe/spire/cmd/spire-server/util"
commoncli "github.com/spiffe/spire/pkg/common/cli"
"github.com/spiffe/spire/pkg/common/cliprinter"
"github.com/spiffe/spire/pkg/common/util"
"google.golang.org/grpc/codes"
)

Expand All @@ -22,7 +22,7 @@ func NewUpdateCommand() cli.Command {
}

func newUpdateCommand(env *commoncli.Env) cli.Command {
return util.AdaptCommand(env, &updateCommand{env: env})
return serverutil.AdaptCommand(env, &updateCommand{env: env})
}

type updateCommand struct {
Expand All @@ -48,7 +48,7 @@ func (c *updateCommand) AppendFlags(f *flag.FlagSet) {
cliprinter.AppendFlagWithCustomPretty(&c.printer, f, c.env, c.prettyPrintUpdate)
}

func (c *updateCommand) Run(ctx context.Context, _ *commoncli.Env, serverClient util.ServerClient) error {
func (c *updateCommand) Run(ctx context.Context, _ *commoncli.Env, serverClient serverutil.ServerClient) error {
federationRelationships, err := getRelationships(c.config, c.path)
if err != nil {
return err
Expand Down Expand Up @@ -98,7 +98,7 @@ func (c *updateCommand) prettyPrintUpdate(env *commoncli.Env, results ...any) er
for _, r := range failed {
env.Println()
env.ErrPrintf("Failed to update the following federation relationship (code: %s, msg: %q):\n",
codes.Code(safecast.MustConvert[uint32](r.Status.Code)),
util.MustCast[codes.Code](r.Status.Code),
r.Status.Message)
printFederationRelationship(r.FederationRelationship, env.ErrPrintf)
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/spire-server/cli/jwt/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@ import (
"fmt"
"time"

"github.com/ccoveille/go-safecast"
"github.com/go-jose/go-jose/v4/jwt"
"github.com/mitchellh/cli"
"github.com/spiffe/go-spiffe/v2/spiffeid"
svidv1 "github.com/spiffe/spire-api-sdk/proto/spire/api/server/svid/v1"
"github.com/spiffe/spire-api-sdk/proto/spire/api/types"
"github.com/spiffe/spire/cmd/spire-server/util"
serverutil "github.com/spiffe/spire/cmd/spire-server/util"
commoncli "github.com/spiffe/spire/pkg/common/cli"
"github.com/spiffe/spire/pkg/common/cliprinter"
"github.com/spiffe/spire/pkg/common/diskutil"
"github.com/spiffe/spire/pkg/common/jwtsvid"
"github.com/spiffe/spire/pkg/common/util"
)

func NewMintCommand() cli.Command {
return newMintCommand(commoncli.DefaultEnv)
}

func newMintCommand(env *commoncli.Env) cli.Command {
return util.AdaptCommand(env, &mintCommand{env: env})
return serverutil.AdaptCommand(env, &mintCommand{env: env})
}

type mintCommand struct {
Expand Down Expand Up @@ -53,7 +53,7 @@ func (c *mintCommand) AppendFlags(fs *flag.FlagSet) {
cliprinter.AppendFlagWithCustomPretty(&c.printer, fs, c.env, prettyPrintMint)
}

func (c *mintCommand) Run(ctx context.Context, env *commoncli.Env, serverClient util.ServerClient) error {
func (c *mintCommand) Run(ctx context.Context, env *commoncli.Env, serverClient serverutil.ServerClient) error {
if c.spiffeID == "" {
return errors.New("spiffeID must be specified")
}
Expand Down Expand Up @@ -138,7 +138,7 @@ func getJWTSVIDEndOfLife(token string) (time.Time, error) {
// ttlToSeconds returns the number of seconds in a duration, rounded up to
// the nearest second
func ttlToSeconds(ttl time.Duration) (int32, error) {
return safecast.ToInt32(int64((ttl + time.Second - 1) / time.Second))
return util.CheckedCast[int32]((ttl + time.Second - 1) / time.Second)
}

func prettyPrintMint(env *commoncli.Env, results ...any) error {
Expand Down
10 changes: 5 additions & 5 deletions cmd/spire-server/cli/token/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ import (
"flag"
"fmt"

"github.com/ccoveille/go-safecast"
"github.com/mitchellh/cli"
"github.com/spiffe/go-spiffe/v2/spiffeid"
agentv1 "github.com/spiffe/spire-api-sdk/proto/spire/api/server/agent/v1"
prototypes "github.com/spiffe/spire-api-sdk/proto/spire/api/types"
"github.com/spiffe/spire/cmd/spire-server/util"
serverutil "github.com/spiffe/spire/cmd/spire-server/util"
commoncli "github.com/spiffe/spire/pkg/common/cli"
"github.com/spiffe/spire/pkg/common/cliprinter"
"github.com/spiffe/spire/pkg/common/util"
)

func NewGenerateCommand() cli.Command {
return newGenerateCommand(commoncli.DefaultEnv)
}

func newGenerateCommand(env *commoncli.Env) cli.Command {
return util.AdaptCommand(env, &generateCommand{env: env})
return serverutil.AdaptCommand(env, &generateCommand{env: env})
}

type generateCommand struct {
Expand All @@ -41,12 +41,12 @@ func (g *generateCommand) Synopsis() string {
return "Generates a join token"
}

func (g *generateCommand) Run(ctx context.Context, _ *commoncli.Env, serverClient util.ServerClient) error {
func (g *generateCommand) Run(ctx context.Context, _ *commoncli.Env, serverClient serverutil.ServerClient) error {
id, err := getID(g.SpiffeID)
if err != nil {
return err
}
ttl, err := safecast.ToInt32(g.TTL)
ttl, err := util.CheckedCast[int32](g.TTL)
if err != nil {
return fmt.Errorf("TTL: %w", err)
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/spire-server/cli/x509/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import (
"net/url"
"time"

"github.com/ccoveille/go-safecast"
"github.com/mitchellh/cli"
"github.com/spiffe/go-spiffe/v2/spiffeid"
bundlev1 "github.com/spiffe/spire-api-sdk/proto/spire/api/server/bundle/v1"
svidv1 "github.com/spiffe/spire-api-sdk/proto/spire/api/server/svid/v1"
"github.com/spiffe/spire/cmd/spire-server/util"
serverutil "github.com/spiffe/spire/cmd/spire-server/util"
commoncli "github.com/spiffe/spire/pkg/common/cli"
"github.com/spiffe/spire/pkg/common/cliprinter"
"github.com/spiffe/spire/pkg/common/diskutil"
"github.com/spiffe/spire/pkg/common/util"
)

type generateKeyFunc func() (crypto.Signer, error)
Expand All @@ -38,7 +38,7 @@ func newMintCommand(env *commoncli.Env, generateKey generateKeyFunc) cli.Command
return ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
}
}
return util.AdaptCommand(env, &mintCommand{
return serverutil.AdaptCommand(env, &mintCommand{
generateKey: generateKey,
env: env,
})
Expand Down Expand Up @@ -71,7 +71,7 @@ func (c *mintCommand) AppendFlags(fs *flag.FlagSet) {
cliprinter.AppendFlagWithCustomPretty(&c.printer, fs, c.env, c.prettyPrintMint)
}

func (c *mintCommand) Run(ctx context.Context, env *commoncli.Env, serverClient util.ServerClient) error {
func (c *mintCommand) Run(ctx context.Context, env *commoncli.Env, serverClient serverutil.ServerClient) error {
if c.spiffeID == "" {
return errors.New("spiffeID must be specified")
}
Expand Down Expand Up @@ -174,7 +174,7 @@ func (c *mintCommand) Run(ctx context.Context, env *commoncli.Env, serverClient
// ttlToSeconds returns the number of seconds in a duration, rounded up to
// the nearest second
func ttlToSeconds(ttl time.Duration) (int32, error) {
return safecast.ToInt32(int64((ttl + time.Second - 1) / time.Second))
return util.CheckedCast[int32]((ttl + time.Second - 1) / time.Second)
}

type mintResult struct {
Expand Down
Loading

0 comments on commit 5f08852

Please sign in to comment.