Skip to content

Commit

Permalink
Merge pull request #379 from DopplerHQ/ENG-2592-autocomplete-flags
Browse files Browse the repository at this point in the history
Add tab complete for flag arguments
  • Loading branch information
Piccirello authored Apr 28, 2023
2 parents 6dd7e4b + 5dd21fb commit c85ebe7
Show file tree
Hide file tree
Showing 19 changed files with 131 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ completions/

# macOS
.DS_Store

# IDEs
.idea/
1 change: 1 addition & 0 deletions pkg/cmd/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func activityLogIDsValidArgs(cmd *cobra.Command, args []string, toComplete strin

func init() {
activityGetCmd.Flags().String("log", "", "activity log id")
activityGetCmd.RegisterFlagCompletionFunc("log", activityLogIDsValidArgs)
activityCmd.AddCommand(activityGetCmd)

activityCmd.Flags().IntP("number", "n", 20, "max number of logs to display")
Expand Down
16 changes: 16 additions & 0 deletions pkg/cmd/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,21 +355,29 @@ func unlockedConfigNamesValidArgs(cmd *cobra.Command, args []string, toComplete

func init() {
configsCmd.Flags().StringP("project", "p", "", "project (e.g. backend)")
configsCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
configsCmd.Flags().StringP("environment", "e", "", "config environment")
configsCmd.RegisterFlagCompletionFunc("environment", configEnvironmentIDsValidArgs)
configsCmd.Flags().IntP("number", "n", 100, "max number of configs to display")
configsCmd.Flags().Int("page", 1, "page to display")

configsGetCmd.Flags().StringP("project", "p", "", "project (e.g. backend)")
configsGetCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
configsGetCmd.Flags().StringP("config", "c", "", "config (e.g. dev)")
configsGetCmd.RegisterFlagCompletionFunc("config", configNamesValidArgs)
configsCmd.AddCommand(configsGetCmd)

configsCreateCmd.Flags().StringP("project", "p", "", "project (e.g. backend)")
configsCreateCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
configsCreateCmd.Flags().String("name", "", "config name")
configsCreateCmd.Flags().StringP("environment", "e", "", "config environment")
configsCreateCmd.RegisterFlagCompletionFunc("environment", configEnvironmentIDsValidArgs)
configsCmd.AddCommand(configsCreateCmd)

configsUpdateCmd.Flags().StringP("project", "p", "", "project (e.g. backend)")
configsUpdateCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
configsUpdateCmd.Flags().StringP("config", "c", "", "config (e.g. dev)")
configsUpdateCmd.RegisterFlagCompletionFunc("config", configNamesValidArgs)
configsUpdateCmd.Flags().String("name", "", "config name")
if err := configsUpdateCmd.MarkFlagRequired("name"); err != nil {
utils.HandleError(err)
Expand All @@ -378,22 +386,30 @@ func init() {
configsCmd.AddCommand(configsUpdateCmd)

configsDeleteCmd.Flags().StringP("project", "p", "", "project (e.g. backend)")
configsDeleteCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
configsDeleteCmd.Flags().StringP("config", "c", "", "config (e.g. dev)")
configsDeleteCmd.RegisterFlagCompletionFunc("config", configNamesValidArgs)
configsDeleteCmd.Flags().BoolP("yes", "y", false, "proceed without confirmation")
configsCmd.AddCommand(configsDeleteCmd)

configsLockCmd.Flags().StringP("project", "p", "", "project (e.g. backend)")
configsLockCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
configsLockCmd.Flags().StringP("config", "c", "", "config (e.g. dev)")
configsLockCmd.RegisterFlagCompletionFunc("config", lockedConfigNamesValidArgs)
configsLockCmd.Flags().BoolP("yes", "y", false, "proceed without confirmation")
configsCmd.AddCommand(configsLockCmd)

configsUnlockCmd.Flags().StringP("project", "p", "", "project (e.g. backend)")
configsUnlockCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
configsUnlockCmd.Flags().StringP("config", "c", "", "config (e.g. dev)")
configsUnlockCmd.RegisterFlagCompletionFunc("config", unlockedConfigNamesValidArgs)
configsUnlockCmd.Flags().BoolP("yes", "y", false, "proceed without confirmation")
configsCmd.AddCommand(configsUnlockCmd)

configsCloneCmd.Flags().StringP("project", "p", "", "project (e.g. backend)")
configsCloneCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
configsCloneCmd.Flags().StringP("config", "c", "", "config (e.g. dev)")
configsCloneCmd.RegisterFlagCompletionFunc("config", configNamesValidArgs)
configsCloneCmd.Flags().String("name", "", "new config name")
configsCmd.AddCommand(configsCloneCmd)

Expand Down
8 changes: 8 additions & 0 deletions pkg/cmd/configs_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,26 @@ func configLogIDsValidArgs(cmd *cobra.Command, args []string, toComplete string)

func init() {
configsLogsCmd.Flags().StringP("project", "p", "", "project (e.g. backend)")
configsLogsCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
configsLogsCmd.Flags().StringP("config", "c", "", "config (e.g. dev)")
configsLogsCmd.RegisterFlagCompletionFunc("config", configNamesValidArgs)
configsLogsCmd.Flags().Int("page", 1, "log page to display")
configsLogsCmd.Flags().IntP("number", "n", 20, "max number of logs to display")
configsCmd.AddCommand(configsLogsCmd)

configsLogsGetCmd.Flags().String("log", "", "audit log id")
configsLogsGetCmd.RegisterFlagCompletionFunc("log", configLogIDsValidArgs)
configsLogsGetCmd.Flags().StringP("project", "p", "", "project (e.g. backend)")
configsLogsGetCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
configsLogsGetCmd.Flags().StringP("config", "c", "", "config (e.g. dev)")
configsLogsGetCmd.RegisterFlagCompletionFunc("config", configNamesValidArgs)
configsLogsCmd.AddCommand(configsLogsGetCmd)

configsLogsRollbackCmd.Flags().String("log", "", "audit log id")
configsLogsRollbackCmd.RegisterFlagCompletionFunc("log", configLogIDsValidArgs)
configsLogsRollbackCmd.Flags().StringP("project", "p", "", "project (e.g. backend)")
configsLogsRollbackCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
configsLogsRollbackCmd.Flags().StringP("config", "c", "", "config (e.g. dev)")
configsLogsRollbackCmd.RegisterFlagCompletionFunc("config", configNamesValidArgs)
configsLogsCmd.AddCommand(configsLogsRollbackCmd)
}
13 changes: 13 additions & 0 deletions pkg/cmd/configs_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,25 +189,38 @@ func configTokenSlugsValidArgs(cmd *cobra.Command, args []string, toComplete str

func init() {
configsTokensCmd.Flags().StringP("project", "p", "", "project (e.g. backend)")
configsTokensCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
configsTokensCmd.Flags().StringP("config", "c", "", "config (e.g. dev)")
configsTokensCmd.RegisterFlagCompletionFunc("config", configNamesValidArgs)
configsCmd.AddCommand(configsTokensCmd)

configsTokensGetCmd.Flags().String("slug", "", "service token slug")
configsTokensGetCmd.RegisterFlagCompletionFunc("slug", configTokenSlugsValidArgs)
configsTokensGetCmd.Flags().StringP("project", "p", "", "project (e.g. backend)")
configsTokensGetCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
configsTokensGetCmd.Flags().StringP("config", "c", "", "config (e.g. dev)")
configsTokensGetCmd.RegisterFlagCompletionFunc("config", configNamesValidArgs)
configsTokensCmd.AddCommand(configsTokensGetCmd)

configsTokensCreateCmd.Flags().String("name", "", "service token name")
configsTokensCreateCmd.Flags().StringP("project", "p", "", "project (e.g. backend)")
configsTokensCreateCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
configsTokensCreateCmd.Flags().StringP("config", "c", "", "config (e.g. dev)")
configsTokensCreateCmd.RegisterFlagCompletionFunc("config", configNamesValidArgs)
configsTokensCreateCmd.Flags().Bool("plain", false, "print only the token, without formatting")
configsTokensCreateCmd.Flags().Bool("copy", false, "copy the token to your clipboard")
configsTokensCreateCmd.Flags().Duration("max-age", 0, "token will expire after specified duration, (e.g. '3h', '15m')")
configsTokensCreateCmd.Flags().String("access", "read", "the token's access. one of [\"read\", \"read/write\"]")
configsTokensCreateCmd.RegisterFlagCompletionFunc("access", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"read", "read/write"}, cobra.ShellCompDirectiveDefault
})
configsTokensCmd.AddCommand(configsTokensCreateCmd)

configsTokensRevokeCmd.Flags().String("slug", "", "service token slug")
configsTokensRevokeCmd.RegisterFlagCompletionFunc("slug", configTokenSlugsValidArgs)
configsTokensRevokeCmd.Flags().StringP("project", "p", "", "project (e.g. backend)")
configsTokensRevokeCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
configsTokensRevokeCmd.Flags().StringP("config", "c", "", "config (e.g. dev)")
configsTokensRevokeCmd.RegisterFlagCompletionFunc("config", configNamesValidArgs)
configsTokensCmd.AddCommand(configsTokensRevokeCmd)
}
14 changes: 14 additions & 0 deletions pkg/cmd/enclave_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,29 @@ var enclaveConfigsUnlockCmd = &cobra.Command{

func init() {
enclaveConfigsCmd.Flags().StringP("project", "p", "", "enclave project (e.g. backend)")
enclaveConfigsCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
enclaveConfigsCmd.Flags().StringP("environment", "e", "", "config environment")
enclaveConfigsCmd.RegisterFlagCompletionFunc("environment", configEnvironmentIDsValidArgs)
enclaveConfigsCmd.Flags().IntP("number", "n", 100, "max number of configs to display")
enclaveConfigsCmd.Flags().Int("page", 1, "page to display")

enclaveConfigsGetCmd.Flags().StringP("project", "p", "", "enclave project (e.g. backend)")
enclaveConfigsGetCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
enclaveConfigsGetCmd.Flags().StringP("config", "c", "", "enclave config (e.g. dev)")
enclaveConfigsGetCmd.RegisterFlagCompletionFunc("config", configNamesValidArgs)
enclaveConfigsCmd.AddCommand(enclaveConfigsGetCmd)

enclaveConfigsCreateCmd.Flags().StringP("project", "p", "", "enclave project (e.g. backend)")
enclaveConfigsCreateCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
enclaveConfigsCreateCmd.Flags().String("name", "", "config name")
enclaveConfigsCreateCmd.Flags().StringP("environment", "e", "", "config environment")
enclaveConfigsCreateCmd.RegisterFlagCompletionFunc("environment", configEnvironmentIDsValidArgs)
enclaveConfigsCmd.AddCommand(enclaveConfigsCreateCmd)

enclaveConfigsUpdateCmd.Flags().StringP("project", "p", "", "enclave project (e.g. backend)")
enclaveConfigsUpdateCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
enclaveConfigsUpdateCmd.Flags().StringP("config", "c", "", "enclave config (e.g. dev)")
enclaveConfigsUpdateCmd.RegisterFlagCompletionFunc("config", configNamesValidArgs)
enclaveConfigsUpdateCmd.Flags().String("name", "", "config name")
if err := enclaveConfigsUpdateCmd.MarkFlagRequired("name"); err != nil {
utils.HandleError(err)
Expand All @@ -115,17 +123,23 @@ func init() {
enclaveConfigsCmd.AddCommand(enclaveConfigsUpdateCmd)

enclaveConfigsDeleteCmd.Flags().StringP("project", "p", "", "enclave project (e.g. backend)")
enclaveConfigsDeleteCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
enclaveConfigsDeleteCmd.Flags().StringP("config", "c", "", "enclave config (e.g. dev)")
enclaveConfigsDeleteCmd.RegisterFlagCompletionFunc("config", configNamesValidArgs)
enclaveConfigsDeleteCmd.Flags().BoolP("yes", "y", false, "proceed without confirmation")
enclaveConfigsCmd.AddCommand(enclaveConfigsDeleteCmd)

enclaveConfigsLockCmd.Flags().StringP("project", "p", "", "enclave project (e.g. backend)")
enclaveConfigsLockCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
enclaveConfigsLockCmd.Flags().StringP("config", "c", "", "enclave config (e.g. dev)")
enclaveConfigsLockCmd.RegisterFlagCompletionFunc("config", lockedConfigNamesValidArgs)
enclaveConfigsLockCmd.Flags().BoolP("yes", "y", false, "proceed without confirmation")
enclaveConfigsCmd.AddCommand(enclaveConfigsLockCmd)

enclaveConfigsUnlockCmd.Flags().StringP("project", "p", "", "enclave project (e.g. backend)")
enclaveConfigsUnlockCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
enclaveConfigsUnlockCmd.Flags().StringP("config", "c", "", "enclave config (e.g. dev)")
enclaveConfigsUnlockCmd.RegisterFlagCompletionFunc("config", unlockedConfigNamesValidArgs)
enclaveConfigsUnlockCmd.Flags().BoolP("yes", "y", false, "proceed without confirmation")
enclaveConfigsCmd.AddCommand(enclaveConfigsUnlockCmd)

Expand Down
8 changes: 8 additions & 0 deletions pkg/cmd/enclave_configs_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,26 @@ var enclaveConfigsLogsRollbackCmd = &cobra.Command{

func init() {
enclaveConfigsLogsCmd.Flags().StringP("project", "p", "", "enclave project (e.g. backend)")
enclaveConfigsLogsCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
enclaveConfigsLogsCmd.Flags().StringP("config", "c", "", "enclave config (e.g. dev)")
enclaveConfigsLogsCmd.RegisterFlagCompletionFunc("config", configNamesValidArgs)
enclaveConfigsLogsCmd.Flags().Int("page", 1, "log page to display")
enclaveConfigsLogsCmd.Flags().IntP("number", "n", 20, "max number of logs to display")
enclaveConfigsCmd.AddCommand(enclaveConfigsLogsCmd)

enclaveConfigsLogsGetCmd.Flags().String("log", "", "audit log id")
enclaveConfigsLogsGetCmd.RegisterFlagCompletionFunc("log", configLogIDsValidArgs)
enclaveConfigsLogsGetCmd.Flags().StringP("project", "p", "", "enclave project (e.g. backend)")
enclaveConfigsLogsGetCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
enclaveConfigsLogsGetCmd.Flags().StringP("config", "c", "", "enclave config (e.g. dev)")
enclaveConfigsLogsGetCmd.RegisterFlagCompletionFunc("config", configNamesValidArgs)
enclaveConfigsLogsCmd.AddCommand(enclaveConfigsLogsGetCmd)

enclaveConfigsLogsRollbackCmd.Flags().String("log", "", "audit log id")
enclaveConfigsLogsRollbackCmd.RegisterFlagCompletionFunc("log", configLogIDsValidArgs)
enclaveConfigsLogsRollbackCmd.Flags().StringP("project", "p", "", "enclave project (e.g. backend)")
enclaveConfigsLogsRollbackCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
enclaveConfigsLogsRollbackCmd.Flags().StringP("config", "c", "", "enclave config (e.g. dev)")
enclaveConfigsLogsRollbackCmd.RegisterFlagCompletionFunc("config", configNamesValidArgs)
enclaveConfigsLogsCmd.AddCommand(enclaveConfigsLogsRollbackCmd)
}
10 changes: 10 additions & 0 deletions pkg/cmd/enclave_configs_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,34 @@ var enclaveConfigsTokensRevokeCmd = &cobra.Command{

func init() {
enclaveConfigsTokensCmd.Flags().StringP("project", "p", "", "enclave project (e.g. backend)")
enclaveConfigsTokensCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
enclaveConfigsTokensCmd.Flags().StringP("config", "c", "", "enclave config (e.g. dev)")
enclaveConfigsTokensCmd.RegisterFlagCompletionFunc("config", configNamesValidArgs)
enclaveConfigsCmd.AddCommand(enclaveConfigsTokensCmd)

enclaveConfigsTokensGetCmd.Flags().String("slug", "", "service token slug")
enclaveConfigsTokensGetCmd.RegisterFlagCompletionFunc("slug", configTokenSlugsValidArgs)
enclaveConfigsTokensGetCmd.Flags().StringP("project", "p", "", "enclave project (e.g. backend)")
enclaveConfigsTokensGetCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
enclaveConfigsTokensGetCmd.Flags().StringP("config", "c", "", "enclave config (e.g. dev)")
enclaveConfigsTokensGetCmd.RegisterFlagCompletionFunc("config", configNamesValidArgs)
enclaveConfigsTokensCmd.AddCommand(enclaveConfigsTokensGetCmd)

enclaveConfigsTokensCreateCmd.Flags().String("name", "", "service token name")
enclaveConfigsTokensCreateCmd.Flags().StringP("project", "p", "", "enclave project (e.g. backend)")
enclaveConfigsTokensCreateCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
enclaveConfigsTokensCreateCmd.Flags().StringP("config", "c", "", "enclave config (e.g. dev)")
enclaveConfigsTokensCreateCmd.RegisterFlagCompletionFunc("config", configNamesValidArgs)
enclaveConfigsTokensCreateCmd.Flags().Bool("plain", false, "print only the token, without formatting")
enclaveConfigsTokensCreateCmd.Flags().Bool("copy", false, "copy the token to your clipboard")
enclaveConfigsTokensCreateCmd.Flags().String("access", "read", "the token's access. one of [\"read\", \"read/write\"]")
enclaveConfigsTokensCmd.AddCommand(enclaveConfigsTokensCreateCmd)

enclaveConfigsTokensRevokeCmd.Flags().String("slug", "", "service token slug")
enclaveConfigsTokensRevokeCmd.RegisterFlagCompletionFunc("slug", configTokenSlugsValidArgs)
enclaveConfigsTokensRevokeCmd.Flags().StringP("project", "p", "", "enclave project (e.g. backend)")
enclaveConfigsTokensRevokeCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
enclaveConfigsTokensRevokeCmd.Flags().StringP("config", "c", "", "enclave config (e.g. dev)")
enclaveConfigsTokensRevokeCmd.RegisterFlagCompletionFunc("config", configNamesValidArgs)
enclaveConfigsTokensCmd.AddCommand(enclaveConfigsTokensRevokeCmd)
}
2 changes: 2 additions & 0 deletions pkg/cmd/enclave_environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ var enclaveEnvironmentsGetCmd = &cobra.Command{

func init() {
enclaveEnvironmentsGetCmd.Flags().StringP("project", "p", "", "enclave project (e.g. backend)")
enclaveEnvironmentsGetCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
enclaveEnvironmentsCmd.AddCommand(enclaveEnvironmentsGetCmd)

enclaveEnvironmentsCmd.Flags().StringP("project", "p", "", "enclave project (e.g. backend)")
enclaveEnvironmentsCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
enclaveEnvironmentsCmd.Flags().IntP("number", "n", 100, "max number of environments to display")
enclaveEnvironmentsCmd.Flags().Int("page", 1, "page to display")
enclaveCmd.AddCommand(enclaveEnvironmentsCmd)
Expand Down
3 changes: 3 additions & 0 deletions pkg/cmd/enclave_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func init() {
enclaveProjectsCmd.Flags().Int("page", 1, "page to display")

enclaveProjectsGetCmd.Flags().StringP("project", "p", "", "enclave project (e.g. backend)")
enclaveProjectsGetCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
enclaveProjectsCmd.AddCommand(enclaveProjectsGetCmd)

enclaveProjectsCreateCmd.Flags().String("name", "", "project name")
Expand All @@ -83,9 +84,11 @@ func init() {

enclaveProjectsDeleteCmd.Flags().BoolP("yes", "y", false, "proceed without confirmation")
enclaveProjectsDeleteCmd.Flags().StringP("project", "p", "", "enclave project (e.g. backend)")
enclaveProjectsDeleteCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
enclaveProjectsCmd.AddCommand(enclaveProjectsDeleteCmd)

enclaveProjectsUpdateCmd.Flags().StringP("project", "p", "", "enclave project (e.g. backend)")
enclaveProjectsUpdateCmd.RegisterFlagCompletionFunc("project", projectIDsValidArgs)
enclaveProjectsUpdateCmd.Flags().String("name", "", "project name")
if err := enclaveProjectsUpdateCmd.MarkFlagRequired("name"); err != nil {
utils.HandleError(err)
Expand Down
Loading

0 comments on commit c85ebe7

Please sign in to comment.