From 410b491e2e41cd63915700d168523b4dc5b82098 Mon Sep 17 00:00:00 2001 From: Jonathan Kula Date: Mon, 9 Oct 2023 16:49:42 -0700 Subject: [PATCH] [DEV-172] Add get/list roles --- cmd/alias.go | 2 +- cmd/roles.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ cmd/verbs.go | 3 +++ format/format.go | 2 ++ format/role.go | 18 +++++++++++++++ 5 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 cmd/roles.go create mode 100644 format/role.go diff --git a/cmd/alias.go b/cmd/alias.go index df84c10..e0f2ae0 100644 --- a/cmd/alias.go +++ b/cmd/alias.go @@ -64,7 +64,7 @@ func newGetAliasCmd() *cobra.Command { return formatOne(cmd, alias) }, } - cmd.Flags().StringP(WorkspaceFlag, WorkspaceShortFlag, DefaultWorkspace, "only show views for the selected workspace") + cmd.Flags().StringP(WorkspaceFlag, WorkspaceShortFlag, DefaultWorkspace, "get an alias for the selected workspace") return cmd } diff --git a/cmd/roles.go b/cmd/roles.go new file mode 100644 index 0000000..366e63e --- /dev/null +++ b/cmd/roles.go @@ -0,0 +1,58 @@ +package cmd + +import ( + "github.com/rockset/cli/format" + "github.com/spf13/cobra" +) + +func newListRolesCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "role", + Aliases: []string{"r", "roles"}, + Args: cobra.NoArgs, + Short: "list roles", + Annotations: group("role"), + RunE: func(cmd *cobra.Command, args []string) error { + ctx := cmd.Context() + rs, err := rockClient(cmd) + if err != nil { + return err + } + + aliases, err := rs.ListRoles(ctx) + if err != nil { + return err + } + + return formatList(cmd, format.ToInterfaceArray(aliases)) + }, + } + + return cmd +} + +func newGetRoleCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "role NAME", + Aliases: []string{"r"}, + Args: cobra.ExactArgs(1), + Short: "get role information", + Annotations: group("role"), + RunE: func(cmd *cobra.Command, args []string) error { + ctx := cmd.Context() + rs, err := rockClient(cmd) + if err != nil { + return err + } + + alias, err := rs.GetRole(ctx, args[0]) + if err != nil { + return err + } + + return formatOne(cmd, alias) + }, + } + + return cmd +} diff --git a/cmd/verbs.go b/cmd/verbs.go index bdee432..5680da4 100644 --- a/cmd/verbs.go +++ b/cmd/verbs.go @@ -126,6 +126,9 @@ func addVerbs(root *cobra.Command) { getCmd.AddCommand(newGetAliasCmd()) listCmd.AddCommand(newListAliasesCmd()) + getCmd.AddCommand(newGetRoleCommand()) + listCmd.AddCommand(newListRolesCommand()) + getCmd.AddCommand(newGetAPIKeyCmd()) listCmd.AddCommand(newListAPIKeysCmd()) diff --git a/format/format.go b/format/format.go index 52a0ccd..41228f6 100644 --- a/format/format.go +++ b/format/format.go @@ -92,6 +92,8 @@ func defaultSelectorFor(f any) (DefaultSelector, error) { return ViewDefaultSelector, nil case openapi.VirtualInstance: return VirtualInstanceDefaultSelector, nil + case openapi.Role: + return RoleDefaultSelector, nil default: return DefaultSelector{}, fmt.Errorf("no formatter for %T", t) } diff --git a/format/role.go b/format/role.go new file mode 100644 index 0000000..8a7bb9a --- /dev/null +++ b/format/role.go @@ -0,0 +1,18 @@ +package format + +import "github.com/rockset/rockset-go-client/openapi" + +var RoleDefaultSelector = DefaultSelector{ + Normal: "Name:.role_name,Created By:.created_by", + Wide: "Name:.role_name,Created By:.created_by,Description:.description", +} + +// just to list available fields +var _ = openapi.Role{ + CreatedAt: nil, + CreatedBy: nil, + Description: nil, + OwnerEmail: nil, + Privileges: nil, + RoleName: nil, +}