Skip to content

Commit

Permalink
Merge pull request #233 from DopplerHQ/update-cobra
Browse files Browse the repository at this point in the history
Add support for auto-completing command args
  • Loading branch information
Piccirello authored Jul 7, 2021
2 parents 631a9c8 + 957a193 commit 726f30f
Show file tree
Hide file tree
Showing 24 changed files with 822 additions and 101 deletions.
3 changes: 3 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ brews:
bin.install "doppler"
bash_completion.install "completions/doppler.bash" => "doppler"
zsh_completion.install "completions/doppler.zsh" => "_doppler"
fish_completion.install "completions/doppler.fish"
test: |
system "#{bin}/doppler --version"
Expand Down Expand Up @@ -152,6 +153,8 @@ nfpms:
dst: /etc/bash_completion.d/doppler
- src: ./completions/doppler.zsh
dst: /usr/local/share/zsh/site-functions/_doppler
- src: ./completions/doppler.fish
dst: /usr/share/fish/completions/doppler.fish

blobs:
-
Expand Down
30 changes: 20 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,31 @@ module github.com/DopplerHQ/cli
go 1.16

require (
github.com/AlecAivazis/survey/v2 v2.0.8
github.com/atotto/clipboard v0.1.2
github.com/AlecAivazis/survey/v2 v2.2.13
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6 // indirect
github.com/atotto/clipboard v0.1.4
github.com/coreos/go-etcd v2.0.0+incompatible // indirect
github.com/cpuguy83/go-md2man v1.0.10 // indirect
github.com/go-openapi/strfmt v0.19.3 // indirect
github.com/google/uuid v1.1.1
github.com/hashicorp/go-version v1.2.1
github.com/godbus/dbus/v5 v5.0.4 // indirect
github.com/google/uuid v1.2.0
github.com/hashicorp/go-version v1.3.0
github.com/jedib0t/go-pretty v4.3.0+incompatible
github.com/mattn/go-colorable v0.1.4 // indirect
github.com/mattn/go-isatty v0.0.12
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.13
github.com/mattn/go-runewidth v0.0.5 // indirect
github.com/skratchdot/open-golang v0.0.0-20190402232053-79abb63cd66e
github.com/spf13/cobra v0.0.5
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5 // indirect
github.com/zalando/go-keyring v0.1.1-0.20210112083600-4d37811583ad
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8 // indirect
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77 // indirect
github.com/zalando/go-keyring v0.1.1
go.mongodb.org/mongo-driver v1.1.2 // indirect
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22 // indirect
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
golang.org/x/text v0.3.6 // indirect
gopkg.in/gookit/color.v1 v1.1.6
gopkg.in/yaml.v3 v3.0.0-20191026110619-0b21df46bc1d
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)
287 changes: 287 additions & 0 deletions go.sum

Large diffs are not rendered by default.

19 changes: 16 additions & 3 deletions pkg/cmd/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cmd

import (
"github.com/DopplerHQ/cli/pkg/configuration"
"github.com/DopplerHQ/cli/pkg/controllers"
"github.com/DopplerHQ/cli/pkg/http"
"github.com/DopplerHQ/cli/pkg/printer"
"github.com/DopplerHQ/cli/pkg/utils"
Expand Down Expand Up @@ -44,9 +45,10 @@ var activityCmd = &cobra.Command{
}

var activityGetCmd = &cobra.Command{
Use: "get [log_id]",
Short: "Get workplace activity log",
Args: cobra.MaximumNArgs(1),
Use: "get [log_id]",
Short: "Get workplace activity log",
Args: cobra.MaximumNArgs(1),
ValidArgsFunction: activityLogIDsValidArgs,
Run: func(cmd *cobra.Command, args []string) {
jsonFlag := utils.OutputJSON
localConfig := configuration.LocalConfig(cmd)
Expand All @@ -68,6 +70,17 @@ var activityGetCmd = &cobra.Command{
},
}

func activityLogIDsValidArgs(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
persistentValidArgsFunction(cmd)

localConfig := configuration.LocalConfig(cmd)
ids, err := controllers.GetActivityLogIDs(localConfig)
if err.IsNil() {
return ids, cobra.ShellCompDirectiveNoFileComp
}
return nil, cobra.ShellCompDirectiveNoFileComp
}

func init() {
activityGetCmd.Flags().String("log", "", "activity log id")
activityCmd.AddCommand(activityGetCmd)
Expand Down
6 changes: 5 additions & 1 deletion pkg/cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
var completionCmd = &cobra.Command{
Use: "completion",
Short: "Print shell completion script",
ValidArgs: []string{"bash", "zsh"},
ValidArgs: []string{"bash", "zsh", "fish"},
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
shell := getShell(args)
Expand All @@ -42,6 +42,10 @@ var completionCmd = &cobra.Command{
if err := cmd.Root().GenZshCompletion(os.Stdout); err != nil {
utils.HandleError(err, "Unable to generate zsh completions.")
}
} else if strings.HasSuffix(shell, "/fish") {
if err := cmd.Root().GenFishCompletion(os.Stdout, true); err != nil {
utils.HandleError(err, "Unable to generate fish completions.")
}
} else {
utils.HandleError(fmt.Errorf("Your shell is not supported"))
}
Expand Down
112 changes: 88 additions & 24 deletions pkg/cmd/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"strings"

"github.com/DopplerHQ/cli/pkg/configuration"
"github.com/DopplerHQ/cli/pkg/controllers"
"github.com/DopplerHQ/cli/pkg/http"
"github.com/DopplerHQ/cli/pkg/printer"
"github.com/DopplerHQ/cli/pkg/utils"
Expand All @@ -35,10 +36,11 @@ var configsCmd = &cobra.Command{
}

var configsGetCmd = &cobra.Command{
Use: "get [config]",
Short: "Get info for a config",
Args: cobra.MaximumNArgs(1),
Run: getConfigs,
Use: "get [config]",
Short: "Get info for a config",
Args: cobra.MaximumNArgs(1),
ValidArgsFunction: configNamesValidArgs,
Run: getConfigs,
}

var configsCreateCmd = &cobra.Command{
Expand All @@ -49,38 +51,43 @@ var configsCreateCmd = &cobra.Command{
}

var configsDeleteCmd = &cobra.Command{
Use: "delete [config]",
Short: "Delete a config",
Args: cobra.MaximumNArgs(1),
Run: deleteConfigs,
Use: "delete [config]",
Short: "Delete a config",
Args: cobra.MaximumNArgs(1),
ValidArgsFunction: configNamesValidArgs,
Run: deleteConfigs,
}

var configsUpdateCmd = &cobra.Command{
Use: "update [config]",
Short: "Update a config",
Args: cobra.MaximumNArgs(1),
Run: updateConfigs,
Use: "update [config]",
Short: "Update a config",
Args: cobra.MaximumNArgs(1),
ValidArgsFunction: configNamesValidArgs,
Run: updateConfigs,
}

var configsLockCmd = &cobra.Command{
Use: "lock [config]",
Short: "Lock a config",
Args: cobra.MaximumNArgs(1),
Run: lockConfigs,
Use: "lock [config]",
Short: "Lock a config",
Args: cobra.MaximumNArgs(1),
ValidArgsFunction: unlockedConfigNamesValidArgs,
Run: lockConfigs,
}

var configsUnlockCmd = &cobra.Command{
Use: "unlock [config]",
Short: "Unlock a config",
Args: cobra.MaximumNArgs(1),
Run: unlockConfigs,
Use: "unlock [config]",
Short: "Unlock a config",
Args: cobra.MaximumNArgs(1),
ValidArgsFunction: lockedConfigNamesValidArgs,
Run: unlockConfigs,
}

var configsCloneCmd = &cobra.Command{
Use: "clone [config]",
Short: "Clone a config",
Args: cobra.MaximumNArgs(1),
Run: cloneConfigs,
Use: "clone [config]",
Short: "Clone a config",
Args: cobra.MaximumNArgs(1),
ValidArgsFunction: configNamesValidArgs,
Run: cloneConfigs,
}

func configs(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -187,6 +194,7 @@ func deleteConfigs(cmd *cobra.Command, args []string) {
func updateConfigs(cmd *cobra.Command, args []string) {
jsonFlag := utils.OutputJSON
name := cmd.Flag("name").Value.String()
yes := utils.GetBoolFlag(cmd, "yes")
localConfig := configuration.LocalConfig(cmd)

utils.RequireValue("token", localConfig.Token.Value)
Expand All @@ -197,6 +205,14 @@ func updateConfigs(cmd *cobra.Command, args []string) {
config = args[0]
}

if !yes {
utils.LogWarning("Renaming this config may break your current deploys.")
if !utils.ConfirmationPrompt("Continue?", false) {
utils.Log("Aborting")
return
}
}

info, err := http.UpdateConfig(localConfig.APIHost.Value, utils.GetBool(localConfig.VerifyTLS.Value, true), localConfig.Token.Value, localConfig.EnclaveProject.Value, config, name)
if !err.IsNil() {
utils.HandleError(err.Unwrap(), err.Message)
Expand Down Expand Up @@ -286,6 +302,53 @@ func cloneConfigs(cmd *cobra.Command, args []string) {
}
}

func configNamesValidArgs(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
persistentValidArgsFunction(cmd)

localConfig := configuration.LocalConfig(cmd)
names, err := controllers.GetConfigNames(localConfig)
if err.IsNil() {
return names, cobra.ShellCompDirectiveNoFileComp
}
return nil, cobra.ShellCompDirectiveNoFileComp
}

func lockedConfigNamesValidArgs(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
persistentValidArgsFunction(cmd)

localConfig := configuration.LocalConfig(cmd)
configs, err := controllers.GetConfigs(localConfig)
if !err.IsNil() {
return nil, cobra.ShellCompDirectiveNoFileComp
}

var names []string
for _, config := range configs {
if config.Locked {
names = append(names, config.Name)
}
}
return names, cobra.ShellCompDirectiveNoFileComp
}

func unlockedConfigNamesValidArgs(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
persistentValidArgsFunction(cmd)

localConfig := configuration.LocalConfig(cmd)
configs, err := controllers.GetConfigs(localConfig)
if !err.IsNil() {
return nil, cobra.ShellCompDirectiveNoFileComp
}

var names []string
for _, config := range configs {
if !config.Locked {
names = append(names, config.Name)
}
}
return names, cobra.ShellCompDirectiveNoFileComp
}

func init() {
configsCmd.Flags().StringP("project", "p", "", "project (e.g. backend)")

Expand All @@ -304,6 +367,7 @@ func init() {
if err := configsUpdateCmd.MarkFlagRequired("name"); err != nil {
utils.HandleError(err)
}
configsUpdateCmd.Flags().BoolP("yes", "y", false, "proceed without confirmation")
configsCmd.AddCommand(configsUpdateCmd)

configsDeleteCmd.Flags().StringP("project", "p", "", "project (e.g. backend)")
Expand Down
30 changes: 22 additions & 8 deletions pkg/cmd/configs_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cmd

import (
"github.com/DopplerHQ/cli/pkg/configuration"
"github.com/DopplerHQ/cli/pkg/controllers"
"github.com/DopplerHQ/cli/pkg/http"
"github.com/DopplerHQ/cli/pkg/printer"
"github.com/DopplerHQ/cli/pkg/utils"
Expand All @@ -31,17 +32,19 @@ var configsLogsCmd = &cobra.Command{
}

var configsLogsGetCmd = &cobra.Command{
Use: "get [log_id]",
Short: "Get config audit log",
Args: cobra.MaximumNArgs(1),
Run: getConfigsLogs,
Use: "get [log_id]",
Short: "Get config audit log",
Args: cobra.MaximumNArgs(1),
ValidArgsFunction: configLogIDsValidArgs,
Run: getConfigsLogs,
}

var configsLogsRollbackCmd = &cobra.Command{
Use: "rollback [log_id]",
Short: "Rollback a config change",
Args: cobra.MaximumNArgs(1),
Run: rollbackConfigsLogs,
Use: "rollback [log_id]",
Short: "Rollback a config change",
Args: cobra.MaximumNArgs(1),
ValidArgsFunction: configLogIDsValidArgs,
Run: rollbackConfigsLogs,
}

func configsLogs(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -101,6 +104,17 @@ func rollbackConfigsLogs(cmd *cobra.Command, args []string) {
}
}

func configLogIDsValidArgs(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
persistentValidArgsFunction(cmd)

localConfig := configuration.LocalConfig(cmd)
ids, err := controllers.GetConfigLogIDs(localConfig)
if err.IsNil() {
return ids, cobra.ShellCompDirectiveNoFileComp
}
return nil, cobra.ShellCompDirectiveNoFileComp
}

func init() {
configsLogsCmd.Flags().StringP("project", "p", "", "project (e.g. backend)")
configsLogsCmd.Flags().StringP("config", "c", "", "config (e.g. dev)")
Expand Down
Loading

0 comments on commit 726f30f

Please sign in to comment.