From 864c1a3ef4032efe479b9c01712e988276357212 Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Fri, 19 Jan 2024 14:56:00 -0500 Subject: [PATCH 1/2] cmd: disable file completion, add ext completion --- internal/cmd/context.go | 22 ++++++++++++---------- internal/cmd/schema.go | 18 ++++++++++-------- internal/commands/completion.go | 6 ++++++ internal/commands/permission.go | 17 +++++++++-------- internal/commands/relationship.go | 10 +++++----- internal/commands/schema.go | 9 +++++---- 6 files changed, 47 insertions(+), 35 deletions(-) diff --git a/internal/cmd/context.go b/internal/cmd/context.go index e97dc0d2..83eee436 100644 --- a/internal/cmd/context.go +++ b/internal/cmd/context.go @@ -31,33 +31,35 @@ var contextCmd = &cobra.Command{ } var contextListCmd = &cobra.Command{ - Use: "list", - Short: "list all contexts", - Args: cobra.ExactArgs(0), - RunE: contextListCmdFunc, + Use: "list", + Short: "list all contexts", + Args: cobra.ExactArgs(0), + ValidArgsFunction: cobra.NoFileCompletions, + RunE: contextListCmdFunc, } var contextSetCmd = &cobra.Command{ - Use: "set ", - Short: "create or overwrite a context", - Args: cobra.ExactArgs(3), - RunE: contextSetCmdFunc, + Use: "set ", + Short: "create or overwrite a context", + Args: cobra.ExactArgs(3), + ValidArgsFunction: cobra.NoFileCompletions, + RunE: contextSetCmdFunc, } var contextRemoveCmd = &cobra.Command{ Use: "remove ", Short: "remove a context", Args: cobra.ExactArgs(1), - RunE: contextRemoveCmdFunc, ValidArgsFunction: ContextGet, + RunE: contextRemoveCmdFunc, } var contextUseCmd = &cobra.Command{ Use: "use ", Short: "set a context as the current context", Args: cobra.MaximumNArgs(1), - RunE: contextUseCmdFunc, ValidArgsFunction: ContextGet, + RunE: contextUseCmdFunc, } func ContextGet(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { diff --git a/internal/cmd/schema.go b/internal/cmd/schema.go index 75da0379..73fba4d5 100644 --- a/internal/cmd/schema.go +++ b/internal/cmd/schema.go @@ -36,17 +36,19 @@ func registerAdditionalSchemaCmds(schemaCmd *cobra.Command) { } var schemaWriteCmd = &cobra.Command{ - Use: "write ", - Args: cobra.MaximumNArgs(1), - Short: "write a Schema file (or stdin) to the current Permissions System", - RunE: schemaWriteCmdFunc, + Use: "write ", + Args: cobra.MaximumNArgs(1), + Short: "write a schema file (.zed or stdin) to the current permissions system", + ValidArgsFunction: commands.FileExtensionCompletions("zed"), + RunE: schemaWriteCmdFunc, } var schemaCopyCmd = &cobra.Command{ - Use: "copy ", - Args: cobra.ExactArgs(2), - Short: "copy a Schema from one context into another", - RunE: schemaCopyCmdFunc, + Use: "copy ", + Short: "copy a schema from one context into another", + Args: cobra.ExactArgs(2), + ValidArgsFunction: ContextGet, + RunE: schemaCopyCmdFunc, } // TODO(jschorr): support this in the client package diff --git a/internal/commands/completion.go b/internal/commands/completion.go index 83ab8236..8e4504e3 100644 --- a/internal/commands/completion.go +++ b/internal/commands/completion.go @@ -22,6 +22,12 @@ const ( SubjectTypeWithOptionalRelation ) +func FileExtensionCompletions(extension ...string) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + return extension, cobra.ShellCompDirectiveFilterFileExt + } +} + func GetArgs(fields ...CompletionArgumentType) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // Read the current schema, if any. diff --git a/internal/commands/permission.go b/internal/commands/permission.go index 14e6de23..bf4fa061 100644 --- a/internal/commands/permission.go +++ b/internal/commands/permission.go @@ -113,40 +113,41 @@ var checkCmd = &cobra.Command{ Use: "check ", Short: "check that a Permission exists for a Subject", Args: cobra.ExactArgs(3), - RunE: checkCmdFunc, ValidArgsFunction: GetArgs(ResourceID, Permission, SubjectID), + RunE: checkCmdFunc, } var expandCmd = &cobra.Command{ - Use: "expand ", - Short: "expand the structure of a Permission", - Args: cobra.ExactArgs(2), - RunE: expandCmdFunc, + Use: "expand ", + Short: "expand the structure of a Permission", + Args: cobra.ExactArgs(2), + ValidArgsFunction: cobra.NoFileCompletions, + RunE: expandCmdFunc, } var lookupResourcesCmd = &cobra.Command{ Use: "lookup-resources ", Short: "looks up the Resources of a given type for which the Subject has Permission", Args: cobra.ExactArgs(3), - RunE: lookupResourcesCmdFunc, ValidArgsFunction: GetArgs(ResourceType, Permission, SubjectID), + RunE: lookupResourcesCmdFunc, } var lookupCmd = &cobra.Command{ Use: "lookup ", Short: "lookup the Resources of a given type for which the Subject has Permission", Args: cobra.ExactArgs(3), + ValidArgsFunction: GetArgs(ResourceType, Permission, SubjectID), RunE: lookupResourcesCmdFunc, Hidden: true, - ValidArgsFunction: GetArgs(ResourceType, Permission, SubjectID), } var lookupSubjectsCmd = &cobra.Command{ Use: "lookup-subjects ", Short: "lookup the Subjects of a given type for which the Subject has Permission on the Resource", Args: cobra.ExactArgs(3), - RunE: lookupSubjectsCmdFunc, ValidArgsFunction: GetArgs(ResourceID, Permission, SubjectTypeWithOptionalRelation), + RunE: lookupSubjectsCmdFunc, } func checkCmdFunc(cmd *cobra.Command, args []string) error { diff --git a/internal/commands/relationship.go b/internal/commands/relationship.go index feecbe9d..39a56014 100644 --- a/internal/commands/relationship.go +++ b/internal/commands/relationship.go @@ -63,40 +63,40 @@ var createCmd = &cobra.Command{ Use: "create ", Short: "create a Relationship for a Subject", Args: StdinOrExactArgs(3), - RunE: writeRelationshipCmdFunc(v1.RelationshipUpdate_OPERATION_CREATE, os.Stdin), ValidArgsFunction: GetArgs(ResourceID, Permission, SubjectTypeWithOptionalRelation), + RunE: writeRelationshipCmdFunc(v1.RelationshipUpdate_OPERATION_CREATE, os.Stdin), } var touchCmd = &cobra.Command{ Use: "touch ", Short: "idempotently update a Relationship for a Subject", Args: StdinOrExactArgs(3), - RunE: writeRelationshipCmdFunc(v1.RelationshipUpdate_OPERATION_TOUCH, os.Stdin), ValidArgsFunction: GetArgs(ResourceID, Permission, SubjectTypeWithOptionalRelation), + RunE: writeRelationshipCmdFunc(v1.RelationshipUpdate_OPERATION_TOUCH, os.Stdin), } var deleteCmd = &cobra.Command{ Use: "delete ", Short: "delete a Relationship", Args: StdinOrExactArgs(3), - RunE: writeRelationshipCmdFunc(v1.RelationshipUpdate_OPERATION_DELETE, os.Stdin), ValidArgsFunction: GetArgs(ResourceID, Permission, SubjectTypeWithOptionalRelation), + RunE: writeRelationshipCmdFunc(v1.RelationshipUpdate_OPERATION_DELETE, os.Stdin), } var readCmd = &cobra.Command{ Use: "read ", Short: "reads Relationships", Args: cobra.RangeArgs(1, 3), - RunE: readRelationships, ValidArgsFunction: GetArgs(ResourceID, Permission, SubjectTypeWithOptionalRelation), + RunE: readRelationships, } var bulkDeleteCmd = &cobra.Command{ Use: "bulk-delete ", Short: "bulk delete Relationships", Args: cobra.RangeArgs(1, 3), - RunE: bulkDeleteRelationships, ValidArgsFunction: GetArgs(ResourceID, Permission, SubjectTypeWithOptionalRelation), + RunE: bulkDeleteRelationships, } func StdinOrExactArgs(n int) cobra.PositionalArgs { diff --git a/internal/commands/schema.go b/internal/commands/schema.go index 5ed0af2b..0e56234a 100644 --- a/internal/commands/schema.go +++ b/internal/commands/schema.go @@ -31,10 +31,11 @@ var ( } schemaReadCmd = &cobra.Command{ - Use: "read", - Args: cobra.ExactArgs(0), - Short: "read the Schema of current Permissions System", - RunE: schemaReadCmdFunc, + Use: "read", + Short: "read the Schema of current Permissions System", + Args: cobra.ExactArgs(0), + ValidArgsFunction: cobra.NoFileCompletions, + RunE: schemaReadCmdFunc, } ) From 6061f4eaf597e63a003fa8ad7c24e074feaa1296 Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Fri, 19 Jan 2024 17:45:30 -0500 Subject: [PATCH 2/2] cmd: fix help-text casing, more fileext completion --- internal/cmd/backup.go | 4 ++-- internal/cmd/cmd.go | 4 ++-- internal/cmd/context.go | 10 +++++----- internal/cmd/import.go | 2 +- internal/cmd/schema.go | 4 ++-- internal/cmd/validate.go | 8 +++++--- internal/commands/permission.go | 12 ++++++------ internal/commands/relationship.go | 12 ++++++------ internal/commands/schema.go | 4 ++-- internal/commands/watch.go | 2 +- 10 files changed, 32 insertions(+), 30 deletions(-) diff --git a/internal/cmd/backup.go b/internal/cmd/backup.go index d19a35ec..0117a65a 100644 --- a/internal/cmd/backup.go +++ b/internal/cmd/backup.go @@ -32,7 +32,7 @@ import ( var ( backupCmd = &cobra.Command{ Use: "backup ", - Short: "Create, restore, and inspect Permissions System backups", + Short: "Create, restore, and inspect permissions system backups", Args: cobra.ExactArgs(1), // Create used to be on the root, so add it here for back-compat. RunE: backupCreateCmdFunc, @@ -107,7 +107,7 @@ func registerBackupCmd(rootCmd *cobra.Command) { // Restore used to be on the root, so add it there too, but hidden. restoreCmd := &cobra.Command{ Use: "restore ", - Short: "Restore a permission system from a file", + Short: "Restore a permission system from a backup file", Args: cobra.MaximumNArgs(1), RunE: backupRestoreCmdFunc, Hidden: true, diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index c4acff4c..8741e036 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd.go @@ -55,7 +55,7 @@ func Run() { versionCmd := &cobra.Command{ Use: "version", - Short: "display zed version information", + Short: "Display zed and SpiceDB version information", RunE: versionCmdFunc, } cobrautil.RegisterVersionFlags(versionCmd.Flags()) @@ -65,7 +65,7 @@ func Run() { // Register root-level aliases rootCmd.AddCommand(&cobra.Command{ Use: "use ", - Short: "an alias for `zed context use`", + Short: "Alias for `zed context use`", Args: cobra.MaximumNArgs(1), RunE: contextUseCmdFunc, ValidArgsFunction: ContextGet, diff --git a/internal/cmd/context.go b/internal/cmd/context.go index 83eee436..d49757b2 100644 --- a/internal/cmd/context.go +++ b/internal/cmd/context.go @@ -27,12 +27,12 @@ func registerContextCmd(rootCmd *cobra.Command) { var contextCmd = &cobra.Command{ Use: "context ", - Short: "manage your machines Authzed credentials", + Short: "Manage configurations for connecting to SpiceDB deployments", } var contextListCmd = &cobra.Command{ Use: "list", - Short: "list all contexts", + Short: "Lists all available contexts", Args: cobra.ExactArgs(0), ValidArgsFunction: cobra.NoFileCompletions, RunE: contextListCmdFunc, @@ -40,7 +40,7 @@ var contextListCmd = &cobra.Command{ var contextSetCmd = &cobra.Command{ Use: "set ", - Short: "create or overwrite a context", + Short: "Creates or overwrite a context", Args: cobra.ExactArgs(3), ValidArgsFunction: cobra.NoFileCompletions, RunE: contextSetCmdFunc, @@ -48,7 +48,7 @@ var contextSetCmd = &cobra.Command{ var contextRemoveCmd = &cobra.Command{ Use: "remove ", - Short: "remove a context", + Short: "Removes a context", Args: cobra.ExactArgs(1), ValidArgsFunction: ContextGet, RunE: contextRemoveCmdFunc, @@ -56,7 +56,7 @@ var contextRemoveCmd = &cobra.Command{ var contextUseCmd = &cobra.Command{ Use: "use ", - Short: "set a context as the current context", + Short: "Sets a context as the current context", Args: cobra.MaximumNArgs(1), ValidArgsFunction: ContextGet, RunE: contextUseCmdFunc, diff --git a/internal/cmd/import.go b/internal/cmd/import.go index 9a50aa37..76c0c7b0 100644 --- a/internal/cmd/import.go +++ b/internal/cmd/import.go @@ -29,7 +29,7 @@ func registerImportCmd(rootCmd *cobra.Command) { var importCmd = &cobra.Command{ Use: "import ", - Short: "import schema and relationships from a file or url", + Short: "Imports schema and relationships from a file or url", Example: ` From a gist: zed import https://gist.github.com/ecordell/8e3b613a677e3c844742cf24421c08b6 diff --git a/internal/cmd/schema.go b/internal/cmd/schema.go index 73fba4d5..9892f07b 100644 --- a/internal/cmd/schema.go +++ b/internal/cmd/schema.go @@ -38,14 +38,14 @@ func registerAdditionalSchemaCmds(schemaCmd *cobra.Command) { var schemaWriteCmd = &cobra.Command{ Use: "write ", Args: cobra.MaximumNArgs(1), - Short: "write a schema file (.zed or stdin) to the current permissions system", + Short: "Write a schema file (.zed or stdin) to the current permissions system", ValidArgsFunction: commands.FileExtensionCompletions("zed"), RunE: schemaWriteCmdFunc, } var schemaCopyCmd = &cobra.Command{ Use: "copy ", - Short: "copy a schema from one context into another", + Short: "Copy a schema from one context into another", Args: cobra.ExactArgs(2), ValidArgsFunction: ContextGet, RunE: schemaCopyCmdFunc, diff --git a/internal/cmd/validate.go b/internal/cmd/validate.go index 1cb33f27..27141ccd 100644 --- a/internal/cmd/validate.go +++ b/internal/cmd/validate.go @@ -18,6 +18,7 @@ import ( "github.com/authzed/spicedb/pkg/validationfile" "github.com/charmbracelet/lipgloss" + "github.com/authzed/zed/internal/commands" "github.com/authzed/zed/internal/console" "github.com/authzed/zed/internal/decode" "github.com/authzed/zed/internal/printers" @@ -41,7 +42,7 @@ func registerValidateCmd(rootCmd *cobra.Command) { var validateCmd = &cobra.Command{ Use: "validate ", - Short: "validate the given validation or schema file", + Short: "Validates the given validation file (.yaml, .zaml) or schema file (.zed)", Example: ` From a local file (with prefix): zed validate file:///Users/zed/Downloads/authzed-x7izWU8_2Gw3.yaml @@ -60,8 +61,9 @@ var validateCmd = &cobra.Command{ From a devtools instance: zed validate https://localhost:8443/download`, - Args: cobra.ExactArgs(1), - RunE: validateCmdFunc, + Args: cobra.ExactArgs(1), + ValidArgsFunction: commands.FileExtensionCompletions("zed", "yaml", "zaml"), + RunE: validateCmdFunc, } func validateCmdFunc(cmd *cobra.Command, args []string) error { diff --git a/internal/commands/permission.go b/internal/commands/permission.go index bf4fa061..5d81f74c 100644 --- a/internal/commands/permission.go +++ b/internal/commands/permission.go @@ -106,12 +106,12 @@ func RegisterPermissionCmd(rootCmd *cobra.Command) *cobra.Command { var permissionCmd = &cobra.Command{ Use: "permission ", - Short: "perform queries on the Permissions in a Permissions System", + Short: "Query the permissions in a permissions system", } var checkCmd = &cobra.Command{ Use: "check ", - Short: "check that a Permission exists for a Subject", + Short: "Check that a permission exists for a subject", Args: cobra.ExactArgs(3), ValidArgsFunction: GetArgs(ResourceID, Permission, SubjectID), RunE: checkCmdFunc, @@ -119,7 +119,7 @@ var checkCmd = &cobra.Command{ var expandCmd = &cobra.Command{ Use: "expand ", - Short: "expand the structure of a Permission", + Short: "Expand the structure of a permission", Args: cobra.ExactArgs(2), ValidArgsFunction: cobra.NoFileCompletions, RunE: expandCmdFunc, @@ -127,7 +127,7 @@ var expandCmd = &cobra.Command{ var lookupResourcesCmd = &cobra.Command{ Use: "lookup-resources ", - Short: "looks up the Resources of a given type for which the Subject has Permission", + Short: "Enumerates resources of a given type for which the subject has permission", Args: cobra.ExactArgs(3), ValidArgsFunction: GetArgs(ResourceType, Permission, SubjectID), RunE: lookupResourcesCmdFunc, @@ -135,7 +135,7 @@ var lookupResourcesCmd = &cobra.Command{ var lookupCmd = &cobra.Command{ Use: "lookup ", - Short: "lookup the Resources of a given type for which the Subject has Permission", + Short: "Enumerates the resources of a given type for which the subject has permission", Args: cobra.ExactArgs(3), ValidArgsFunction: GetArgs(ResourceType, Permission, SubjectID), RunE: lookupResourcesCmdFunc, @@ -144,7 +144,7 @@ var lookupCmd = &cobra.Command{ var lookupSubjectsCmd = &cobra.Command{ Use: "lookup-subjects ", - Short: "lookup the Subjects of a given type for which the Subject has Permission on the Resource", + Short: "Enumerates the subjects of a given type for which the subject has permission on the resource", Args: cobra.ExactArgs(3), ValidArgsFunction: GetArgs(ResourceID, Permission, SubjectTypeWithOptionalRelation), RunE: lookupSubjectsCmdFunc, diff --git a/internal/commands/relationship.go b/internal/commands/relationship.go index 39a56014..2a1d10ee 100644 --- a/internal/commands/relationship.go +++ b/internal/commands/relationship.go @@ -56,12 +56,12 @@ func RegisterRelationshipCmd(rootCmd *cobra.Command) *cobra.Command { var relationshipCmd = &cobra.Command{ Use: "relationship ", - Short: "perform CRUD operations on the Relationships in a Permissions System", + Short: "Query and mutate the relationships in a permissions system", } var createCmd = &cobra.Command{ Use: "create ", - Short: "create a Relationship for a Subject", + Short: "Create a relationship for a subject", Args: StdinOrExactArgs(3), ValidArgsFunction: GetArgs(ResourceID, Permission, SubjectTypeWithOptionalRelation), RunE: writeRelationshipCmdFunc(v1.RelationshipUpdate_OPERATION_CREATE, os.Stdin), @@ -69,7 +69,7 @@ var createCmd = &cobra.Command{ var touchCmd = &cobra.Command{ Use: "touch ", - Short: "idempotently update a Relationship for a Subject", + Short: "Idempotently updates a relationship for a subject", Args: StdinOrExactArgs(3), ValidArgsFunction: GetArgs(ResourceID, Permission, SubjectTypeWithOptionalRelation), RunE: writeRelationshipCmdFunc(v1.RelationshipUpdate_OPERATION_TOUCH, os.Stdin), @@ -77,7 +77,7 @@ var touchCmd = &cobra.Command{ var deleteCmd = &cobra.Command{ Use: "delete ", - Short: "delete a Relationship", + Short: "Deletes a relationship", Args: StdinOrExactArgs(3), ValidArgsFunction: GetArgs(ResourceID, Permission, SubjectTypeWithOptionalRelation), RunE: writeRelationshipCmdFunc(v1.RelationshipUpdate_OPERATION_DELETE, os.Stdin), @@ -85,7 +85,7 @@ var deleteCmd = &cobra.Command{ var readCmd = &cobra.Command{ Use: "read ", - Short: "reads Relationships", + Short: "Enumerates relationships matching the provided pattern", Args: cobra.RangeArgs(1, 3), ValidArgsFunction: GetArgs(ResourceID, Permission, SubjectTypeWithOptionalRelation), RunE: readRelationships, @@ -93,7 +93,7 @@ var readCmd = &cobra.Command{ var bulkDeleteCmd = &cobra.Command{ Use: "bulk-delete ", - Short: "bulk delete Relationships", + Short: "Deletes relationships matching the provided pattern en masse", Args: cobra.RangeArgs(1, 3), ValidArgsFunction: GetArgs(ResourceID, Permission, SubjectTypeWithOptionalRelation), RunE: bulkDeleteRelationships, diff --git a/internal/commands/schema.go b/internal/commands/schema.go index 0e56234a..f6c034fc 100644 --- a/internal/commands/schema.go +++ b/internal/commands/schema.go @@ -27,12 +27,12 @@ func RegisterSchemaCmd(rootCmd *cobra.Command) *cobra.Command { var ( schemaCmd = &cobra.Command{ Use: "schema ", - Short: "manages Schema for a Permissions System", + Short: "Manage schema for a permissions system", } schemaReadCmd = &cobra.Command{ Use: "read", - Short: "read the Schema of current Permissions System", + Short: "Read the schema of a permissions system", Args: cobra.ExactArgs(0), ValidArgsFunction: cobra.NoFileCompletions, RunE: schemaReadCmdFunc, diff --git a/internal/commands/watch.go b/internal/commands/watch.go index 57d09cc7..9e715cb8 100644 --- a/internal/commands/watch.go +++ b/internal/commands/watch.go @@ -31,7 +31,7 @@ func RegisterWatchCmd(rootCmd *cobra.Command) *cobra.Command { var watchCmd = &cobra.Command{ Use: "watch [object_types, ...] [start_cursor]", - Short: "watches the stream of relationship updates from the server", + Short: "Watches the stream of relationship updates from the server", Args: cobra.RangeArgs(0, 2), RunE: watchCmdFunc, }