Skip to content

Commit

Permalink
topaz cli add --format for versioned commands (#170)
Browse files Browse the repository at this point in the history
* topaz cli add --format for versioned commands

* upd deps
  • Loading branch information
gertd authored Nov 6, 2023
1 parent fa4e090 commit 6cdca83
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 30 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ require (
github.com/aserto-dev/go-aserto v0.30.0
github.com/aserto-dev/go-authorizer v0.20.3
github.com/aserto-dev/go-directory v0.30.2
github.com/aserto-dev/go-directory-cli v0.30.0
github.com/aserto-dev/go-edge-ds v0.30.0
github.com/aserto-dev/go-directory-cli v0.30.1
github.com/aserto-dev/go-edge-ds v0.30.1
github.com/aserto-dev/go-grpc v0.8.58
github.com/aserto-dev/header v0.0.5
github.com/aserto-dev/logger v0.0.4
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,10 @@ github.com/aserto-dev/go-decision-logs v0.0.4 h1:beu/mhqZ92ovhSIPOv2f4q0Ci7HWNLl
github.com/aserto-dev/go-decision-logs v0.0.4/go.mod h1:W50DNu4HPCk+iyI39cP3+KBytdrQYVieSPXh9StuRzA=
github.com/aserto-dev/go-directory v0.30.2 h1:cUkvzma8paV2YFaCIvjfy1a9DMNCx5qV3EjcLo68RuA=
github.com/aserto-dev/go-directory v0.30.2/go.mod h1:jHn6ckERuci6nWz9vCzAbYoF2UnbOwEzl++sA1L3nyc=
github.com/aserto-dev/go-directory-cli v0.30.0 h1:TNQSza1MjDnZJDqqhFj2V4lK16Fm0YQawRW+Jf7R5zg=
github.com/aserto-dev/go-directory-cli v0.30.0/go.mod h1:WrKLhSCksEs0hG2CMaItu6HsAbQMAq00Bq6Kqw5WkcI=
github.com/aserto-dev/go-edge-ds v0.30.0 h1:2o1ixUEDEQMEj/5Tk6HcXgirXKhi9GoG470YO4n6eDY=
github.com/aserto-dev/go-edge-ds v0.30.0/go.mod h1:yKjKFm2u7zz3u/MmrOYq1mugsC2trE3diA+WkQrkvvc=
github.com/aserto-dev/go-directory-cli v0.30.1 h1:5tEDG7uj//NXEzKkF4cbQbsvPuZzn/3hrhTA0GuG/7A=
github.com/aserto-dev/go-directory-cli v0.30.1/go.mod h1:EvTAQdfREB8Wq5aRJxk3krkBz6/4LUwJjfbc0MjVUl8=
github.com/aserto-dev/go-edge-ds v0.30.1 h1:Y8GfQMB/ePoMD54uiWVA3Q8BqlFkRyNu+jGlTbgBYuA=
github.com/aserto-dev/go-edge-ds v0.30.1/go.mod h1:yKjKFm2u7zz3u/MmrOYq1mugsC2trE3diA+WkQrkvvc=
github.com/aserto-dev/go-grpc v0.8.58 h1:uoNDm6ddJQHR94fIq+v6gHWBZTRTQCvRnNvGxsansrU=
github.com/aserto-dev/go-grpc v0.8.58/go.mod h1:2FcclMDheTsEvYCY72o+EwTaoO0ti3/it4G6xIlzNIM=
github.com/aserto-dev/go-http-metrics v0.10.1-20221024-1 h1:nONd24V5nyJ0IIw8QE+OKv30YuHOTNbJ4FsvczLaM8o=
Expand Down
9 changes: 7 additions & 2 deletions pkg/cli/cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
)

type BackupCmd struct {
File string `arg:"" default:"backup.tar.gz" help:"absolute file path to make backup to"`
File string `arg:"" default:"backup.tar.gz" help:"absolute file path to make backup to"`
Format FormatVersion `flag:"" short:"f" enum:"3,2" name:"format" default:"3" help:"format of json data"`
clients.Config
}

Expand All @@ -35,5 +36,9 @@ func (cmd *BackupCmd) Run(c *cc.CommonCtx) error {
}

color.Green(">>> backup to %s", cmd.File)
return dirClient.Backup(c.Context, cmd.File)

if cmd.Format == V2 {
return dirClient.V2.Backup(c.Context, cmd.File)
}
return dirClient.V3.Backup(c.Context, cmd.File)
}
13 changes: 10 additions & 3 deletions pkg/cli/cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ import (

var ErrNotRunning = errors.New("topaz is not running, use 'topaz start' or 'topaz run' to start")

type FormatVersion int

const (
V2 FormatVersion = 2
V3 FormatVersion = 3
)

type CLI struct {
Start StartCmd `cmd:"" help:"start topaz in daemon mode"`
Stop StopCmd `cmd:"" help:"stop topaz instance"`
Status StatusCmd `cmd:"" help:"status of topaz daemon process"`
Run RunCmd `cmd:"" help:"run topaz in console mode"`
Manifest ManifestCmd `cmd:"" help:"manifest commands"`
Load LoadCmd `cmd:"" help:"load manifest from file"`
Save SaveCmd `cmd:"" help:"save manifest to file"`
Load LoadCmd `cmd:"" help:"load manifest from file (DEPRECATED)"`
Save SaveCmd `cmd:"" help:"save manifest to file (DEPRECATED)"`
Import ImportCmd `cmd:"" help:"import directory objects"`
Export ExportCmd `cmd:"" help:"export directory objects"`
Backup BackupCmd `cmd:"" help:"backup directory data"`
Expand All @@ -31,7 +38,7 @@ type CLI struct {
Uninstall UninstallCmd `cmd:"" help:"uninstall topaz container"`
Version VersionCmd `cmd:"" help:"version information"`
Console ConsoleCmd `cmd:"" help:"opens the console in the browser"`
NoCheck bool `name:"no-check" env:"TOPAZ_NO_CHECK" help:"disable local container status check"`
NoCheck bool `name:"no-check" short:"N" env:"TOPAZ_NO_CHECK" help:"disable local container status check"`
}

type VersionCmd struct{}
Expand Down
8 changes: 6 additions & 2 deletions pkg/cli/cmd/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
)

type ExportCmd struct {
Directory string `short:"d" required:"" help:"directory to write .json data"`
Directory string `short:"d" required:"" help:"directory to write .json data"`
Format FormatVersion `flag:"" short:"f" enum:"3,2" name:"format" default:"3" help:"format of json data"`
clients.Config
}

Expand All @@ -30,5 +31,8 @@ func (cmd *ExportCmd) Run(c *cc.CommonCtx) error {
return err
}

return dirClient.Export(c.Context, objectsFile, relationsFile)
if cmd.Format == V2 {
return dirClient.V2.Export(c.Context, objectsFile, relationsFile)
}
return dirClient.V3.Export(c.Context, objectsFile, relationsFile)
}
8 changes: 6 additions & 2 deletions pkg/cli/cmd/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
)

type ImportCmd struct {
Directory string `short:"d" required:"" help:"directory containing .json data"`
Directory string `short:"d" required:"" help:"directory containing .json data"`
Format FormatVersion `flag:"" short:"f" enum:"3,2" name:"format" default:"3" help:"format of json data"`
clients.Config
}

Expand All @@ -32,5 +33,8 @@ func (cmd *ImportCmd) Run(c *cc.CommonCtx) error {
return err
}

return dirClient.Import(c.Context, files)
if cmd.Format == V2 {
return dirClient.V2.Import(c.Context, files)
}
return dirClient.V3.Import(c.Context, files)
}
6 changes: 3 additions & 3 deletions pkg/cli/cmd/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (cmd *GetManifestCmd) Run(c *cc.CommonCtx) error {

color.Green(">>> get manifest to %s", cmd.Path)

r, err := dirClient.GetManifest(c.Context)
r, err := dirClient.V3.GetManifest(c.Context)
if err != nil {
return err
}
Expand Down Expand Up @@ -85,7 +85,7 @@ func (cmd *SetManifestCmd) Run(c *cc.CommonCtx) error {

color.Green(">>> set manifest from %s", cmd.Path)

return dirClient.SetManifest(c.Context, r)
return dirClient.V3.SetManifest(c.Context, r)
}

func (cmd *DeleteManifestCmd) Run(c *cc.CommonCtx) error {
Expand All @@ -96,5 +96,5 @@ func (cmd *DeleteManifestCmd) Run(c *cc.CommonCtx) error {

color.Green(">>> delete manifest")

return dirClient.DeleteManifest(c.Context)
return dirClient.V3.DeleteManifest(c.Context)
}
8 changes: 6 additions & 2 deletions pkg/cli/cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
)

type RestoreCmd struct {
File string `arg:"" default:"backup.tar.gz" help:"absolute file path to local backup tarball"`
File string `arg:"" default:"backup.tar.gz" help:"absolute file path to local backup tarball"`
Format FormatVersion `flag:"" short:"f" enum:"3,2" name:"format" default:"3" help:"format of json data"`
clients.Config
}

Expand All @@ -36,5 +37,8 @@ func (cmd *RestoreCmd) Run(c *cc.CommonCtx) error {
}

color.Green(">>> restore from %s", cmd.File)
return dirClient.Restore(c.Context, cmd.File)
if cmd.Format == V2 {
return dirClient.V2.Restore(c.Context, cmd.File)
}
return dirClient.V3.Restore(c.Context, cmd.File)
}
21 changes: 11 additions & 10 deletions pkg/cli/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
dsr2 "github.com/aserto-dev/go-directory/aserto/directory/reader/v2"
dsr3 "github.com/aserto-dev/go-directory/aserto/directory/reader/v3"

"github.com/aserto-dev/go-directory-cli/client"
v2 "github.com/aserto-dev/go-directory-cli/client/v2"
client "github.com/aserto-dev/go-directory-cli/client/v3"
"github.com/aserto-dev/topaz/pkg/cli/cc"
"github.com/aserto-dev/topaz/pkg/cli/clients"

Expand Down Expand Up @@ -117,15 +118,15 @@ func (cmd *TestExecCmd) Run(c *cc.CommonCtx) error {

switch {
case checkType == Check && reqVersion == 3:
result = checkV3(c.Context, dsc, msg.Fields[checkTypeMapStr[checkType]])
result = checkV3(c.Context, dsc.V3, msg.Fields[checkTypeMapStr[checkType]])
case checkType == CheckPermission && reqVersion == 3:
result = checkPermissionV3(c.Context, dsc, msg.Fields[checkTypeMapStr[checkType]])
result = checkPermissionV3(c.Context, dsc.V3, msg.Fields[checkTypeMapStr[checkType]])
case checkType == CheckRelation && reqVersion == 3:
result = checkRelationV3(c.Context, dsc, msg.Fields[checkTypeMapStr[checkType]])
result = checkRelationV3(c.Context, dsc.V3, msg.Fields[checkTypeMapStr[checkType]])
case checkType == CheckPermission && reqVersion == 2:
result = checkPermissionV2(c.Context, dsc, msg.Fields[checkTypeMapStr[checkType]])
result = checkPermissionV2(c.Context, dsc.V2, msg.Fields[checkTypeMapStr[checkType]])
case checkType == CheckRelation && reqVersion == 2:
result = checkRelationV2(c.Context, dsc, msg.Fields[checkTypeMapStr[checkType]])
result = checkRelationV2(c.Context, dsc.V2, msg.Fields[checkTypeMapStr[checkType]])
case checkType == CheckDecision:
result = checkDecisionV2(c.Context, azc, msg.Fields[checkTypeMapStr[checkType]])
}
Expand Down Expand Up @@ -308,15 +309,15 @@ func checkRelationV3(ctx context.Context, c *client.Client, msg *structpb.Value)
}
}

func checkPermissionV2(ctx context.Context, c *client.Client, msg *structpb.Value) *checkResult {
func checkPermissionV2(ctx context.Context, c *v2.Client, msg *structpb.Value) *checkResult {
var req dsr2.CheckPermissionRequest
if err := unmarshalReq(msg, &req); err != nil {
return &checkResult{Err: err}
}

start := time.Now()

resp, err := c.Reader2.CheckPermission(ctx, &req)
resp, err := c.Reader.CheckPermission(ctx, &req)

duration := time.Since(start)

Expand All @@ -328,15 +329,15 @@ func checkPermissionV2(ctx context.Context, c *client.Client, msg *structpb.Valu
}
}

func checkRelationV2(ctx context.Context, c *client.Client, msg *structpb.Value) *checkResult {
func checkRelationV2(ctx context.Context, c *v2.Client, msg *structpb.Value) *checkResult {
var req dsr2.CheckRelationRequest
if err := unmarshalReq(msg, &req); err != nil {
return &checkResult{Err: err}
}

start := time.Now()

resp, err := c.Reader2.CheckRelation(ctx, &req)
resp, err := c.Reader.CheckRelation(ctx, &req)

duration := time.Since(start)

Expand Down

0 comments on commit 6cdca83

Please sign in to comment.