Skip to content

Commit

Permalink
Merge pull request #331 from authzed/moar-completion
Browse files Browse the repository at this point in the history
More and less completion
  • Loading branch information
josephschorr authored Jan 20, 2024
2 parents 8a5e178 + 6061f4e commit 31f09d8
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 59 deletions.
4 changes: 2 additions & 2 deletions internal/cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
var (
backupCmd = &cobra.Command{
Use: "backup <filename>",
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,
Expand Down Expand Up @@ -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 <filename>",
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,
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -65,7 +65,7 @@ func Run() {
// Register root-level aliases
rootCmd.AddCommand(&cobra.Command{
Use: "use <context>",
Short: "an alias for `zed context use`",
Short: "Alias for `zed context use`",
Args: cobra.MaximumNArgs(1),
RunE: contextUseCmdFunc,
ValidArgsFunction: ContextGet,
Expand Down
28 changes: 15 additions & 13 deletions internal/cmd/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,39 @@ func registerContextCmd(rootCmd *cobra.Command) {

var contextCmd = &cobra.Command{
Use: "context <subcommand>",
Short: "manage your machines Authzed credentials",
Short: "Manage configurations for connecting to SpiceDB deployments",
}

var contextListCmd = &cobra.Command{
Use: "list",
Short: "list all contexts",
Args: cobra.ExactArgs(0),
RunE: contextListCmdFunc,
Use: "list",
Short: "Lists all available contexts",
Args: cobra.ExactArgs(0),
ValidArgsFunction: cobra.NoFileCompletions,
RunE: contextListCmdFunc,
}

var contextSetCmd = &cobra.Command{
Use: "set <name> <endpoint> <api-token>",
Short: "create or overwrite a context",
Args: cobra.ExactArgs(3),
RunE: contextSetCmdFunc,
Use: "set <name> <endpoint> <api-token>",
Short: "Creates or overwrite a context",
Args: cobra.ExactArgs(3),
ValidArgsFunction: cobra.NoFileCompletions,
RunE: contextSetCmdFunc,
}

var contextRemoveCmd = &cobra.Command{
Use: "remove <system>",
Short: "remove a context",
Short: "Removes a context",
Args: cobra.ExactArgs(1),
RunE: contextRemoveCmdFunc,
ValidArgsFunction: ContextGet,
RunE: contextRemoveCmdFunc,
}

var contextUseCmd = &cobra.Command{
Use: "use <system>",
Short: "set a context as the current context",
Short: "Sets 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) {
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func registerImportCmd(rootCmd *cobra.Command) {

var importCmd = &cobra.Command{
Use: "import <url>",
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
Expand Down
18 changes: 10 additions & 8 deletions internal/cmd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,19 @@ func registerAdditionalSchemaCmds(schemaCmd *cobra.Command) {
}

var schemaWriteCmd = &cobra.Command{
Use: "write <file?>",
Args: cobra.MaximumNArgs(1),
Short: "write a Schema file (or stdin) to the current Permissions System",
RunE: schemaWriteCmdFunc,
Use: "write <file?>",
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 <src context> <dest context>",
Args: cobra.ExactArgs(2),
Short: "copy a Schema from one context into another",
RunE: schemaCopyCmdFunc,
Use: "copy <src context> <dest context>",
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
Expand Down
8 changes: 5 additions & 3 deletions internal/cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -41,7 +42,7 @@ func registerValidateCmd(rootCmd *cobra.Command) {

var validateCmd = &cobra.Command{
Use: "validate <validation_file_or_schema_file>",
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
Expand All @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions internal/commands/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 14 additions & 13 deletions internal/commands/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,47 +106,48 @@ func RegisterPermissionCmd(rootCmd *cobra.Command) *cobra.Command {

var permissionCmd = &cobra.Command{
Use: "permission <subcommand>",
Short: "perform queries on the Permissions in a Permissions System",
Short: "Query the permissions in a permissions system",
}

var checkCmd = &cobra.Command{
Use: "check <resource:id> <permission> <subject:id>",
Short: "check that a Permission exists for a Subject",
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 <permission> <resource:id>",
Short: "expand the structure of a Permission",
Args: cobra.ExactArgs(2),
RunE: expandCmdFunc,
Use: "expand <permission> <resource:id>",
Short: "Expand the structure of a permission",
Args: cobra.ExactArgs(2),
ValidArgsFunction: cobra.NoFileCompletions,
RunE: expandCmdFunc,
}

var lookupResourcesCmd = &cobra.Command{
Use: "lookup-resources <type> <permission> <subject:id>",
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),
RunE: lookupResourcesCmdFunc,
ValidArgsFunction: GetArgs(ResourceType, Permission, SubjectID),
RunE: lookupResourcesCmdFunc,
}

var lookupCmd = &cobra.Command{
Use: "lookup <type> <permission> <subject:id>",
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,
Hidden: true,
ValidArgsFunction: GetArgs(ResourceType, Permission, SubjectID),
}

var lookupSubjectsCmd = &cobra.Command{
Use: "lookup-subjects <resource:id> <permission> <subject_type#optional_subject_relation>",
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),
RunE: lookupSubjectsCmdFunc,
ValidArgsFunction: GetArgs(ResourceID, Permission, SubjectTypeWithOptionalRelation),
RunE: lookupSubjectsCmdFunc,
}

func checkCmdFunc(cmd *cobra.Command, args []string) error {
Expand Down
22 changes: 11 additions & 11 deletions internal/commands/relationship.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,47 +56,47 @@ func RegisterRelationshipCmd(rootCmd *cobra.Command) *cobra.Command {

var relationshipCmd = &cobra.Command{
Use: "relationship <subcommand>",
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 <resource:id> <relation> <subject:id#optional_subject_relation>",
Short: "create a Relationship for a Subject",
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 <resource:id> <relation> <subject:id#optional_subject_relation>",
Short: "idempotently update a Relationship for a Subject",
Short: "Idempotently updates 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 <resource:id> <relation> <subject:id#optional_subject_relation>",
Short: "delete a Relationship",
Short: "Deletes 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 <resource_type:optional_resource_id> <optional_relation> <optional_subject_type:optional_subject_id#optional_subject_relation>",
Short: "reads Relationships",
Short: "Enumerates relationships matching the provided pattern",
Args: cobra.RangeArgs(1, 3),
RunE: readRelationships,
ValidArgsFunction: GetArgs(ResourceID, Permission, SubjectTypeWithOptionalRelation),
RunE: readRelationships,
}

var bulkDeleteCmd = &cobra.Command{
Use: "bulk-delete <resource_type:optional_resource_id> <optional_relation> <optional_subject_type:optional_subject_id#optional_subject_relation>",
Short: "bulk delete Relationships",
Short: "Deletes relationships matching the provided pattern en masse",
Args: cobra.RangeArgs(1, 3),
RunE: bulkDeleteRelationships,
ValidArgsFunction: GetArgs(ResourceID, Permission, SubjectTypeWithOptionalRelation),
RunE: bulkDeleteRelationships,
}

func StdinOrExactArgs(n int) cobra.PositionalArgs {
Expand Down
11 changes: 6 additions & 5 deletions internal/commands/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ func RegisterSchemaCmd(rootCmd *cobra.Command) *cobra.Command {
var (
schemaCmd = &cobra.Command{
Use: "schema <subcommand>",
Short: "manages Schema for a Permissions System",
Short: "Manage schema for a permissions system",
}

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 a permissions system",
Args: cobra.ExactArgs(0),
ValidArgsFunction: cobra.NoFileCompletions,
RunE: schemaReadCmdFunc,
}
)

Expand Down
2 changes: 1 addition & 1 deletion internal/commands/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down

0 comments on commit 31f09d8

Please sign in to comment.