Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion otdfctl/cmd/policy/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ func listAttributes(cmd *cobra.Command, args []string) {
state := cli.GetState(cmd)
limit := c.Flags.GetRequiredInt32("limit")
offset := c.Flags.GetRequiredInt32("offset")
sort := getSortOption(c)

resp, err := h.ListAttributes(cmd.Context(), state, limit, offset)
resp, err := h.ListAttributes(cmd.Context(), state, limit, offset, sort)
if err != nil {
cli.ExitWithError("Failed to list attributes", err)
}
Expand Down Expand Up @@ -411,6 +412,7 @@ func initAttributesCommands() {
listDoc.GetDocFlag("state").Description,
)
injectListPaginationFlags(listDoc)
injectListSortFlags(listDoc)

// Update an attribute
updateDoc := man.Docs.GetCommand("policy/attributes/update",
Expand Down
4 changes: 3 additions & 1 deletion otdfctl/cmd/policy/kasKeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,14 +413,15 @@ func policyListKasKeys(cmd *cobra.Command, args []string) {
if err != nil {
cli.ExitWithError("Invalid legacy flag", err)
}
sort := getSortOption(c)

kasLookup, err := resolveKasIdentifier(kasIdentifier)
if err != nil {
cli.ExitWithError("Invalid kas identifier", err)
}

// Get the list of keys.
resp, err := h.ListKasKeys(c.Context(), limit, offset, alg, kasLookup, legacy)
resp, err := h.ListKasKeys(c.Context(), limit, offset, alg, kasLookup, legacy, sort)
if err != nil {
cli.ExitWithError("Failed to list kas keys", err)
}
Expand Down Expand Up @@ -930,6 +931,7 @@ func initKASKeysCommands() {
listDoc.GetDocFlag("legacy").Description,
)
injectListPaginationFlags(listDoc)
injectListSortFlags(listDoc)

// Rotate Kas Key
rotateDoc := man.Docs.GetCommand("policy/kas-registry/key/rotate",
Expand Down
4 changes: 3 additions & 1 deletion otdfctl/cmd/policy/kasRegistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ func listKeyAccessRegistries(cmd *cobra.Command, args []string) {

limit := c.Flags.GetRequiredInt32("limit")
offset := c.Flags.GetRequiredInt32("offset")
sort := getSortOption(c)

resp, err := h.ListKasRegistryEntries(cmd.Context(), limit, offset)
resp, err := h.ListKasRegistryEntries(cmd.Context(), limit, offset, sort)
if err != nil {
cli.ExitWithError("Failed to list Registered KAS entries", err)
}
Expand Down Expand Up @@ -226,6 +227,7 @@ func initKASRegistryCommands() {
man.WithRun(listKeyAccessRegistries),
)
injectListPaginationFlags(listDoc)
injectListSortFlags(listDoc)

createDoc := man.Docs.GetCommand("policy/kas-registry/create",
man.WithRun(createKeyAccessRegistry),
Expand Down
144 changes: 60 additions & 84 deletions otdfctl/cmd/policy/namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ func listAttributeNamespaces(cmd *cobra.Command, args []string) {
state := cli.GetState(cmd)
limit := c.Flags.GetRequiredInt32("limit")
offset := c.Flags.GetRequiredInt32("offset")
sort := getSortOption(c)

resp, err := h.ListNamespaces(cmd.Context(), state, limit, offset)
resp, err := h.ListNamespaces(cmd.Context(), state, limit, offset, sort)
if err != nil {
cli.ExitWithError("Failed to list namespaces", err)
}
Expand Down Expand Up @@ -316,176 +317,151 @@ func policyRemoveKeyFromNamespace(cmd *cobra.Command, args []string) {
common.HandleSuccess(cmd, namespace, t, nil)
}

// newCommandFromDoc creates an independent cobra.Command with metadata copied from a Doc.
// This allows registering the same logical command under multiple parents.
func newCommandFromDoc(doc *man.Doc, run func(*cobra.Command, []string)) *cobra.Command {
cmd := &cobra.Command{
Use: doc.Use,
Short: doc.Short,
Long: doc.Long,
Args: doc.Args,
Aliases: doc.Aliases,
Hidden: doc.Hidden,
Run: run,
}
return cmd
}

// buildNamespacesCommandTree creates a full namespaces command tree with all subcommands and flags.
// Each call returns an independent *cobra.Command so it can be parented under multiple commands.
func buildNamespacesCommandTree() *cobra.Command {
nsDoc := man.Docs.GetDoc("policy/namespaces")
nsCmd := newCommandFromDoc(nsDoc, nil)
func initNamespacesCommands() {
nsDoc := man.Docs.GetCommand("policy/namespaces")

getDoc := man.Docs.GetDoc("policy/namespaces/get")
getCmd := newCommandFromDoc(getDoc, getAttributeNamespace)
getCmd.Flags().StringP(
getDoc := man.Docs.GetCommand("policy/namespaces/get",
man.WithRun(getAttributeNamespace),
)
getDoc.Flags().StringP(
getDoc.GetDocFlag("id").Name,
getDoc.GetDocFlag("id").Shorthand,
getDoc.GetDocFlag("id").Default,
getDoc.GetDocFlag("id").Description,
)

listDoc := man.Docs.GetDoc("policy/namespaces/list")
listCmd := newCommandFromDoc(listDoc, listAttributeNamespaces)
listCmd.Flags().StringP(
listDoc := man.Docs.GetCommand("policy/namespaces/list",
man.WithRun(listAttributeNamespaces),
)
listDoc.Flags().StringP(
listDoc.GetDocFlag("state").Name,
listDoc.GetDocFlag("state").Shorthand,
listDoc.GetDocFlag("state").Default,
listDoc.GetDocFlag("state").Description,
)
listCmd.Flags().Int32P(
listDoc.GetDocFlag("limit").Name,
listDoc.GetDocFlag("limit").Shorthand,
defaultListFlagLimit,
listDoc.GetDocFlag("limit").Description,
)
listCmd.Flags().Int32P(
listDoc.GetDocFlag("offset").Name,
listDoc.GetDocFlag("offset").Shorthand,
defaultListFlagOffset,
listDoc.GetDocFlag("offset").Description,
)
injectListPaginationFlags(listDoc)
injectListSortFlags(listDoc)

createDoc := man.Docs.GetDoc("policy/namespaces/create")
createCmd := newCommandFromDoc(createDoc, createAttributeNamespace)
createCmd.Flags().StringP(
createDoc := man.Docs.GetCommand("policy/namespaces/create",
man.WithRun(createAttributeNamespace),
)
createDoc.Flags().StringP(
createDoc.GetDocFlag("name").Name,
createDoc.GetDocFlag("name").Shorthand,
createDoc.GetDocFlag("name").Default,
createDoc.GetDocFlag("name").Description,
)
injectLabelFlags(createCmd, false)
injectLabelFlags(&createDoc.Command, false)

updateDoc := man.Docs.GetDoc("policy/namespaces/update")
updateCmd := newCommandFromDoc(updateDoc, updateAttributeNamespace)
updateCmd.Flags().StringP(
updateDoc := man.Docs.GetCommand("policy/namespaces/update",
man.WithRun(updateAttributeNamespace),
)
updateDoc.Flags().StringP(
updateDoc.GetDocFlag("id").Name,
updateDoc.GetDocFlag("id").Shorthand,
updateDoc.GetDocFlag("id").Default,
updateDoc.GetDocFlag("id").Description,
)
injectLabelFlags(updateCmd, true)
injectLabelFlags(&updateDoc.Command, true)

deactivateDoc := man.Docs.GetDoc("policy/namespaces/deactivate")
deactivateCmd := newCommandFromDoc(deactivateDoc, deactivateAttributeNamespace)
deactivateCmd.Flags().StringP(
deactivateDoc := man.Docs.GetCommand("policy/namespaces/deactivate",
man.WithRun(deactivateAttributeNamespace),
)
deactivateDoc.Flags().StringP(
deactivateDoc.GetDocFlag("id").Name,
deactivateDoc.GetDocFlag("id").Shorthand,
deactivateDoc.GetDocFlag("id").Default,
deactivateDoc.GetDocFlag("id").Description,
)
deactivateCmd.Flags().Bool(
deactivateDoc.Flags().Bool(
deactivateDoc.GetDocFlag("force").Name,
false,
deactivateDoc.GetDocFlag("force").Description,
)

// unsafe
unsafeDoc := man.Docs.GetDoc("policy/namespaces/unsafe")
unsafeCmd := newCommandFromDoc(unsafeDoc, nil)
unsafeCmd.PersistentFlags().BoolVar(
unsafeDoc := man.Docs.GetCommand("policy/namespaces/unsafe")
unsafeDoc.PersistentFlags().BoolVar(
&forceUnsafe,
unsafeDoc.GetDocFlag("force").Name,
false,
unsafeDoc.GetDocFlag("force").Description,
)

deleteDoc := man.Docs.GetDoc("policy/namespaces/unsafe/delete")
deleteCmd := newCommandFromDoc(deleteDoc, unsafeDeleteAttributeNamespace)
deleteCmd.Flags().StringP(
deleteDoc := man.Docs.GetCommand("policy/namespaces/unsafe/delete",
man.WithRun(unsafeDeleteAttributeNamespace),
)
deleteDoc.Flags().StringP(
deleteDoc.GetDocFlag("id").Name,
deleteDoc.GetDocFlag("id").Shorthand,
deleteDoc.GetDocFlag("id").Default,
deleteDoc.GetDocFlag("id").Description,
)

reactivateDoc := man.Docs.GetDoc("policy/namespaces/unsafe/reactivate")
reactivateCmd := newCommandFromDoc(reactivateDoc, unsafeReactivateAttributeNamespace)
reactivateCmd.Flags().StringP(
reactivateDoc := man.Docs.GetCommand("policy/namespaces/unsafe/reactivate",
man.WithRun(unsafeReactivateAttributeNamespace),
)
reactivateDoc.Flags().StringP(
reactivateDoc.GetDocFlag("id").Name,
reactivateDoc.GetDocFlag("id").Shorthand,
reactivateDoc.GetDocFlag("id").Default,
reactivateDoc.GetDocFlag("id").Description,
)

unsafeUpdateDoc := man.Docs.GetDoc("policy/namespaces/unsafe/update")
unsafeUpdateCmd := newCommandFromDoc(unsafeUpdateDoc, unsafeUpdateAttributeNamespace)
unsafeUpdateCmd.Flags().StringP(
unsafeUpdateDoc := man.Docs.GetCommand("policy/namespaces/unsafe/update",
man.WithRun(unsafeUpdateAttributeNamespace),
)
unsafeUpdateDoc.Flags().StringP(
unsafeUpdateDoc.GetDocFlag("id").Name,
unsafeUpdateDoc.GetDocFlag("id").Shorthand,
unsafeUpdateDoc.GetDocFlag("id").Default,
unsafeUpdateDoc.GetDocFlag("id").Description,
)
unsafeUpdateCmd.Flags().StringP(
unsafeUpdateDoc.Flags().StringP(
unsafeUpdateDoc.GetDocFlag("name").Name,
unsafeUpdateDoc.GetDocFlag("name").Shorthand,
unsafeUpdateDoc.GetDocFlag("name").Default,
unsafeUpdateDoc.GetDocFlag("name").Description,
)

// key
keyDoc := man.Docs.GetDoc("policy/namespaces/key")
keyCmd := newCommandFromDoc(keyDoc, nil)
keyDoc := man.Docs.GetCommand("policy/namespaces/key")

assignDoc := man.Docs.GetDoc("policy/namespaces/key/assign")
assignCmd := newCommandFromDoc(assignDoc, policyAssignKeyToNamespace)
assignCmd.Flags().StringP(
assignDoc := man.Docs.GetCommand("policy/namespaces/key/assign",
man.WithRun(policyAssignKeyToNamespace),
)
assignDoc.Flags().StringP(
assignDoc.GetDocFlag("namespace").Name,
assignDoc.GetDocFlag("namespace").Shorthand,
assignDoc.GetDocFlag("namespace").Default,
assignDoc.GetDocFlag("namespace").Description,
)
assignCmd.Flags().StringP(
assignDoc.Flags().StringP(
assignDoc.GetDocFlag("key-id").Name,
assignDoc.GetDocFlag("key-id").Shorthand,
assignDoc.GetDocFlag("key-id").Default,
assignDoc.GetDocFlag("key-id").Description,
)

removeDoc := man.Docs.GetDoc("policy/namespaces/key/remove")
removeCmd := newCommandFromDoc(removeDoc, policyRemoveKeyFromNamespace)
removeCmd.Flags().StringP(
removeDoc := man.Docs.GetCommand("policy/namespaces/key/remove",
man.WithRun(policyRemoveKeyFromNamespace),
)
removeDoc.Flags().StringP(
removeDoc.GetDocFlag("namespace").Name,
removeDoc.GetDocFlag("namespace").Shorthand,
removeDoc.GetDocFlag("namespace").Default,
removeDoc.GetDocFlag("namespace").Description,
)
removeCmd.Flags().StringP(
removeDoc.Flags().StringP(
removeDoc.GetDocFlag("key-id").Name,
removeDoc.GetDocFlag("key-id").Shorthand,
removeDoc.GetDocFlag("key-id").Default,
removeDoc.GetDocFlag("key-id").Description,
)

keyCmd.AddCommand(assignCmd, removeCmd)
unsafeCmd.AddCommand(deleteCmd, reactivateCmd, unsafeUpdateCmd)
nsCmd.AddCommand(getCmd, listCmd, createCmd, updateCmd, deactivateCmd, unsafeCmd, keyCmd)

return nsCmd
}

func initNamespacesCommands() {
Cmd.AddCommand(buildNamespacesCommandTree())
AttributesCmd.AddCommand(buildNamespacesCommandTree())
keyDoc.AddSubcommands(assignDoc, removeDoc)
unsafeDoc.AddSubcommands(deleteDoc, reactivateDoc, unsafeUpdateDoc)
nsDoc.AddSubcommands(getDoc, listDoc, createDoc, updateDoc, deactivateDoc, unsafeDoc, keyDoc)
AttributesCmd.AddCommand(&nsDoc.Command)
Cmd.AddCommand(&nsDoc.Command)
}
4 changes: 3 additions & 1 deletion otdfctl/cmd/policy/obligations.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ func policyListObligations(cmd *cobra.Command, args []string) {
namespace := c.Flags.GetOptionalString("namespace")
limit := c.Flags.GetRequiredInt32("limit")
offset := c.Flags.GetRequiredInt32("offset")
sort := getSortOption(c)

resp, err := h.ListObligations(cmd.Context(), limit, offset, namespace)
resp, err := h.ListObligations(cmd.Context(), limit, offset, namespace, sort)
if err != nil {
cli.ExitWithError("Failed to list obligations", err)
}
Expand Down Expand Up @@ -525,6 +526,7 @@ func initObligationsCommands() {
listDoc.GetDocFlag("namespace").Description,
)
injectListPaginationFlags(listDoc)
injectListSortFlags(listDoc)

createDoc := man.Docs.GetCommand("policy/obligations/create",
man.WithRun(policyCreateObligation),
Expand Down
17 changes: 17 additions & 0 deletions otdfctl/cmd/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"

"github.com/opentdf/platform/otdfctl/pkg/cli"
"github.com/opentdf/platform/otdfctl/pkg/handlers"
"github.com/opentdf/platform/otdfctl/pkg/man"
"github.com/opentdf/platform/protocol/go/common"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -85,6 +86,22 @@ func injectListPaginationFlags(listDoc *man.Doc) {
)
}

func injectListSortFlags(listDoc *man.Doc) {
sortFlag := listDoc.GetDocFlag("sort")
listDoc.Flags().String(sortFlag.Name, sortFlag.Default, sortFlag.Description)

orderFlag := listDoc.GetDocFlag("order")
listDoc.Flags().String(orderFlag.Name, orderFlag.Default, orderFlag.Description)
}

func getSortOption(c *cli.Cli) handlers.SortOption {
sort, err := handlers.NewSortOption(c.Flags.GetOptionalString("sort"), c.Flags.GetOptionalString("order"))
if err != nil {
cli.ExitWithError("Invalid sort order", err)
}
return sort
}

func InitCommands() {
initActionsCommands()
initAttributesCommands()
Expand Down
4 changes: 3 additions & 1 deletion otdfctl/cmd/policy/registeredResources.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ func policyListRegisteredResources(cmd *cobra.Command, args []string) {
namespace := c.Flags.GetOptionalString("namespace")
limit := c.Flags.GetRequiredInt32("limit")
offset := c.Flags.GetRequiredInt32("offset")
sort := getSortOption(c)

resp, err := h.ListRegisteredResources(cmd.Context(), limit, offset, namespace)
resp, err := h.ListRegisteredResources(cmd.Context(), limit, offset, namespace, sort)
if err != nil {
cli.ExitWithError("Failed to list registered resources", err)
}
Expand Down Expand Up @@ -492,6 +493,7 @@ func initRegisteredResourcesCommands() {
listDoc.GetDocFlag("namespace").Description,
)
injectListPaginationFlags(listDoc)
injectListSortFlags(listDoc)

createDoc := man.Docs.GetCommand("policy/registered-resources/create",
man.WithRun(policyCreateRegisteredResource),
Expand Down
4 changes: 3 additions & 1 deletion otdfctl/cmd/policy/subjectConditionSets.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,9 @@ func listSubjectConditionSets(cmd *cobra.Command, args []string) {
limit := c.Flags.GetRequiredInt32("limit")
offset := c.Flags.GetRequiredInt32("offset")
namespace := c.Flags.GetOptionalString("namespace")
sort := getSortOption(c)

resp, err := h.ListSubjectConditionSets(cmd.Context(), limit, offset, namespace)
resp, err := h.ListSubjectConditionSets(cmd.Context(), limit, offset, namespace, sort)
if err != nil {
cli.ExitWithError("Error listing subject condition sets", err)
}
Expand Down Expand Up @@ -360,6 +361,7 @@ func initSubjectConditionSetsCommands() {
man.WithRun(listSubjectConditionSets),
)
injectListPaginationFlags(listDoc)
injectListSortFlags(listDoc)
listDoc.Flags().StringP(
listDoc.GetDocFlag("namespace").Name,
listDoc.GetDocFlag("namespace").Shorthand,
Expand Down
Loading
Loading