From a9bc874480c485a0adc4f7af185561539fe165e3 Mon Sep 17 00:00:00 2001 From: Sergey Voloshin Date: Sat, 11 Dec 2021 17:05:28 +0300 Subject: [PATCH 1/3] feat(app): add completion command Refs: CU-1ty8v5h --- command/bash.go | 1 + command/completion.go | 56 +++++++++++++++++++++++++++++++++++++ command/deploy.go | 3 +- command/root.go | 2 +- command/service.go | 7 +++-- command/service_down.go | 1 + command/service_recreate.go | 1 + command/service_up.go | 1 + 8 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 command/completion.go diff --git a/command/bash.go b/command/bash.go index 54d055e..8f66dd9 100644 --- a/command/bash.go +++ b/command/bash.go @@ -21,6 +21,7 @@ var bashCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { bash() }, + ValidArgs: []string{"--root"}, } var bashRoot bool diff --git a/command/completion.go b/command/completion.go new file mode 100644 index 0000000..2c9adc5 --- /dev/null +++ b/command/completion.go @@ -0,0 +1,56 @@ +package command + +import ( + "fmt" + "os" + + "github.com/spf13/cobra" +) + +var completionDesc = `To load completions: + +Bash: + + $ source <(%[1]s completion bash) + + # To load completions for each session, execute once: + + $ echo "\nsource <(%[1]s completion bash)" >> ~/.bashrc + +Zsh: + + # If shell completion is not already enabled in your environment, + # you will need to enable it. You can execute the following once: + + $ echo "autoload -U compinit; compinit" >> ~/.zshrc + + # To load completions for each session, execute once: + $ %[1]s completion zsh > "${fpath[1]}/_%[1]s" + +You will need to start a new shell for this setup to take effect. +` + +// completionCmd represents the completion command +var completionCmd = &cobra.Command{ + Use: "completion [bash|zsh]", + Short: "Generate completion script", + Long: fmt.Sprintf(completionDesc, rootCmd.Root().Name()), + DisableFlagsInUseLine: true, + ValidArgs: []string{"bash", "zsh"}, + Run: func(cmd *cobra.Command, args []string) { + if len(args) > 0 { + switch args[0] { + case "bash": + cmd.Root().GenBashCompletion(os.Stdout) + case "zsh": + cmd.Root().GenZshCompletion(os.Stdout) + } + } else { + fmt.Printf(completionDesc, rootCmd.Root().Name()) + } + }, +} + +func init() { + rootCmd.AddCommand(completionCmd) +} diff --git a/command/deploy.go b/command/deploy.go index 1e7f4e7..bb0e442 100644 --- a/command/deploy.go +++ b/command/deploy.go @@ -36,7 +36,8 @@ Laravel: only the database is downloaded`, Run: func(cmd *cobra.Command, args []string) { deploy() }, - Example: "dl deploy\ndl deploy -d\ndl deploy -f -o bitrix,upload", + Example: "dl deploy\ndl deploy -d\ndl deploy -f -o bitrix,upload", + ValidArgs: []string{"--database", "--files", "--override"}, } var ( diff --git a/command/root.go b/command/root.go index 709cf71..af38eca 100644 --- a/command/root.go +++ b/command/root.go @@ -38,7 +38,7 @@ func Execute() { rootCmd.SetUsageTemplate(usageTemplate) rootCmd.DisableAutoGenTag = true - rootCmd.CompletionOptions.DisableDefaultCmd = true + // rootCmd.CompletionOptions.DisableDefaultCmd = true cobra.CheckErr(rootCmd.Execute()) } diff --git a/command/service.go b/command/service.go index 9235b14..60112f4 100644 --- a/command/service.go +++ b/command/service.go @@ -29,9 +29,10 @@ var source string var localNetworkName = "dl_default" var serviceCmd = &cobra.Command{ - Use: "service", - Short: "Local services configuration", - Long: `Local services configuration (portainer, mailcatcher, traefik).`, + Use: "service", + Short: "Local services configuration", + Long: `Local services configuration (portainer, mailcatcher, traefik).`, + ValidArgs: []string{"up", "down", "recreate", "restart"}, } // getServicesContainer local services containers diff --git a/command/service_down.go b/command/service_down.go index b3ddb76..25629c7 100644 --- a/command/service_down.go +++ b/command/service_down.go @@ -26,6 +26,7 @@ Valid parameters for the "--service" flag: portainer, mail, traefik`, Run: func(cmd *cobra.Command, args []string) { downService() }, + ValidArgs: []string{"--service"}, } func downService() { diff --git a/command/service_recreate.go b/command/service_recreate.go index bc5e353..b0b4cc2 100644 --- a/command/service_recreate.go +++ b/command/service_recreate.go @@ -17,4 +17,5 @@ var recreateServiceCmd = &cobra.Command{ downService() upService() }, + ValidArgs: []string{"--service"}, } diff --git a/command/service_up.go b/command/service_up.go index f696f83..383ade5 100644 --- a/command/service_up.go +++ b/command/service_up.go @@ -31,6 +31,7 @@ var upServiceCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { upService() }, + ValidArgs: []string{"--service", "--restart"}, } func upService() { From 4bfce1651841d36976861477649baab595e99a6c Mon Sep 17 00:00:00 2001 From: Sergey Voloshin Date: Sat, 11 Dec 2021 17:07:12 +0300 Subject: [PATCH 2/3] fix(app): add completion command Refs: CU-1ty8v5h --- command/completion.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/command/completion.go b/command/completion.go index 2c9adc5..b84a61d 100644 --- a/command/completion.go +++ b/command/completion.go @@ -41,9 +41,9 @@ var completionCmd = &cobra.Command{ if len(args) > 0 { switch args[0] { case "bash": - cmd.Root().GenBashCompletion(os.Stdout) + _ = cmd.Root().GenBashCompletion(os.Stdout) case "zsh": - cmd.Root().GenZshCompletion(os.Stdout) + _ = cmd.Root().GenZshCompletion(os.Stdout) } } else { fmt.Printf(completionDesc, rootCmd.Root().Name()) From d53095756dccfaa2dafb5c597afdef03262e17db Mon Sep 17 00:00:00 2001 From: Sergey Voloshin Date: Sun, 12 Dec 2021 22:07:21 +0300 Subject: [PATCH 3/3] feat(app): version up --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index a4fc338..8db5221 100644 --- a/main.go +++ b/main.go @@ -13,7 +13,7 @@ import ( "github.com/varrcan/dl/helper" ) -var version = "0.2.1" +var version = "0.2.2" func main() { if !helper.IsConfigDirExists() {