From 22d28a37b65dc7ca61662ce1ea4c3d9625e2aa70 Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Sun, 3 Oct 2021 08:14:57 +0200 Subject: [PATCH] cmd: improve completion sub-commands --- cmd/gencompletion.go | 121 +++++++++++++++++++++++-------------- cmd/genman.go | 7 ++- cmd/initprovider.go | 2 +- cmd/portable.go | 2 +- cmd/service_windows.go | 2 +- cmd/start_windows.go | 2 +- cmd/startsubsys.go | 2 +- cmd/stop_windows.go | 2 +- cmd/uninstall_windows.go | 2 +- docs/full-configuration.md | 6 +- 10 files changed, 91 insertions(+), 57 deletions(-) diff --git a/cmd/gencompletion.go b/cmd/gencompletion.go index b0c4e44e..5205e2ad 100644 --- a/cmd/gencompletion.go +++ b/cmd/gencompletion.go @@ -3,84 +3,117 @@ package cmd import ( "os" - "github.com/rs/zerolog" "github.com/spf13/cobra" - - "github.com/drakkan/sftpgo/v2/logger" ) var genCompletionCmd = &cobra.Command{ Use: "completion [bash|zsh|fish|powershell]", - Short: "Generate shell completion script", - Long: `To load completions: + Short: "Generate the autocompletion script for the specified shell", + Long: `Generate the autocompletion script for sftpgo for the specified shell. -Bash: +See each sub-command's help for details on how to use the generated script. +`, +} + +var genCompletionBashCmd = &cobra.Command{ + Use: "bash", + Short: "Generate the autocompletion script for bash", + Long: `Generate the autocompletion script for the bash shell. + +This script depends on the 'bash-completion' package. +If it is not installed already, you can install it via your OS's package +manager. + +To load completions in your current shell session: $ source <(sftpgo gen completion bash) -To load completions for each session, execute once: +To load completions for every new session, execute once: Linux: - -$ sudo sftpgo gen completion bash > /usr/share/bash-completion/completions/sftpgo + $ sudo sftpgo gen completion bash > /usr/share/bash-completion/completions/sftpgo MacOS: + $ sudo sftpgo gen completion bash > /usr/local/etc/bash_completion.d/sftpgo -$ sudo sftpgo gen completion bash > /usr/local/etc/bash_completion.d/sftpgo +You will need to start a new shell for this setup to take effect. +`, + DisableFlagsInUseLine: true, + RunE: func(cmd *cobra.Command, args []string) error { + return cmd.Root().GenBashCompletionV2(os.Stdout, true) + }, +} -Zsh: +var genCompletionZshCmd = &cobra.Command{ + Use: "zsh", + Short: "Generate the autocompletion script for zsh", + Long: `Generate the autocompletion script for the zsh shell. 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: +To load completions for every new session, execute once: -$ sftpgo gen completion zsh > "${fpath[1]}/_sftpgo" +Linux: + $ sftpgo gen completion zsh > > "${fpath[1]}/_sftpgo" -Fish: +macOS: + $ sudo sftpgo gen completion zsh > /usr/local/share/zsh/site-functions/_sftpgo + +You will need to start a new shell for this setup to take effect. +`, + DisableFlagsInUseLine: true, + RunE: func(cmd *cobra.Command, args []string) error { + return cmd.Root().GenZshCompletion(os.Stdout) + }, +} + +var genCompletionFishCmd = &cobra.Command{ + Use: "fish", + Short: "Generate the autocompletion script for fish", + Long: `Generate the autocompletion script for the fish shell. + +To load completions in your current shell session: $ sftpgo gen completion fish | source -To load completions for each session, execute once: +To load completions for every new session, execute once: $ sftpgo gen completion fish > ~/.config/fish/completions/sftpgo.fish -Powershell: - -PS> sftpgo gen completion powershell | Out-String | Invoke-Expression - -To load completions for every new session, run: - -PS> sftpgo gen completion powershell > sftpgo.ps1 - -and source this file from your powershell profile. +You will need to start a new shell for this setup to take effect. `, DisableFlagsInUseLine: true, - ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, - Args: cobra.ExactValidArgs(1), - Run: func(cmd *cobra.Command, args []string) { - var err error - logger.DisableLogger() - logger.EnableConsoleLogger(zerolog.DebugLevel) - switch args[0] { - case "bash": - err = cmd.Root().GenBashCompletion(os.Stdout) - case "zsh": - err = cmd.Root().GenZshCompletion(os.Stdout) - case "fish": - err = cmd.Root().GenFishCompletion(os.Stdout, true) - case "powershell": - err = cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout) - } - if err != nil { - logger.WarnToConsole("Unable to generate shell completion script: %v", err) - os.Exit(1) - } + RunE: func(cmd *cobra.Command, args []string) error { + return cmd.Root().GenFishCompletion(os.Stdout, true) + }, +} + +var genCompletionPowerShellCmd = &cobra.Command{ + Use: "powershell", + Short: "Generate the autocompletion script for powershell", + Long: `Generate the autocompletion script for powershell. + +To load completions in your current shell session: + +PS C:\> sftpgo gen completion powershell | Out-String | Invoke-Expression + +To load completions for every new session, add the output of the above command +to your powershell profile. +`, + DisableFlagsInUseLine: true, + RunE: func(cmd *cobra.Command, args []string) error { + return cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout) }, } func init() { + genCompletionCmd.AddCommand(genCompletionBashCmd) + genCompletionCmd.AddCommand(genCompletionZshCmd) + genCompletionCmd.AddCommand(genCompletionFishCmd) + genCompletionCmd.AddCommand(genCompletionPowerShellCmd) + genCmd.AddCommand(genCompletionCmd) } diff --git a/cmd/genman.go b/cmd/genman.go index 31f69cc6..3b8cde29 100644 --- a/cmd/genman.go +++ b/cmd/genman.go @@ -16,10 +16,11 @@ var ( manDir string genManCmd = &cobra.Command{ Use: "man", - Short: "Generate man pages for SFTPGo CLI", + Short: "Generate man pages for sftpgo", Long: `This command automatically generates up-to-date man pages of SFTPGo's -command-line interface. By default, it creates the man page files -in the "man" directory under the current directory. +command-line interface. +By default, it creates the man page files in the "man" directory under the +current directory. `, Run: func(cmd *cobra.Command, args []string) { logger.DisableLogger() diff --git a/cmd/initprovider.go b/cmd/initprovider.go index 2bb2ce61..85d669e5 100644 --- a/cmd/initprovider.go +++ b/cmd/initprovider.go @@ -16,7 +16,7 @@ import ( var ( initProviderCmd = &cobra.Command{ Use: "initprovider", - Short: "Initializes and/or updates the configured data provider", + Short: "Initialize and/or updates the configured data provider", Long: `This command reads the data provider connection details from the specified configuration file and creates the initial structure or update the existing one, as needed. diff --git a/cmd/portable.go b/cmd/portable.go index 6d2a76f0..3bfd74ac 100644 --- a/cmd/portable.go +++ b/cmd/portable.go @@ -78,7 +78,7 @@ var ( portableSFTPDBufferSize int64 portableCmd = &cobra.Command{ Use: "portable", - Short: "Serve a single directory", + Short: "Serve a single directory/account", Long: `To serve the current working directory with auto generated credentials simply use: diff --git a/cmd/service_windows.go b/cmd/service_windows.go index 11c12a5b..706d9fb1 100644 --- a/cmd/service_windows.go +++ b/cmd/service_windows.go @@ -7,7 +7,7 @@ import ( var ( serviceCmd = &cobra.Command{ Use: "service", - Short: "Manage SFTPGo Windows Service", + Short: "Manage the SFTPGo Windows Service", } ) diff --git a/cmd/start_windows.go b/cmd/start_windows.go index 3165701d..1096090d 100644 --- a/cmd/start_windows.go +++ b/cmd/start_windows.go @@ -14,7 +14,7 @@ import ( var ( startCmd = &cobra.Command{ Use: "start", - Short: "Start SFTPGo Windows Service", + Short: "Start the SFTPGo Windows Service", Run: func(cmd *cobra.Command, args []string) { configDir = util.CleanDirInput(configDir) if !filepath.IsAbs(logFilePath) && util.IsFileInputValid(logFilePath) { diff --git a/cmd/startsubsys.go b/cmd/startsubsys.go index 21a4f208..76f45463 100644 --- a/cmd/startsubsys.go +++ b/cmd/startsubsys.go @@ -26,7 +26,7 @@ var ( baseHomeDir = "" subsystemCmd = &cobra.Command{ Use: "startsubsys", - Short: "Use SFTPGo as SFTP file transfer subsystem", + Short: "Use sftpgo as SFTP file transfer subsystem", Long: `In this mode SFTPGo speaks the server side of SFTP protocol to stdout and expects client requests from stdin. This mode is not intended to be called directly, but from sshd using the diff --git a/cmd/stop_windows.go b/cmd/stop_windows.go index 2f837448..656aa076 100644 --- a/cmd/stop_windows.go +++ b/cmd/stop_windows.go @@ -12,7 +12,7 @@ import ( var ( stopCmd = &cobra.Command{ Use: "stop", - Short: "Stop SFTPGo Windows Service", + Short: "Stop the SFTPGo Windows Service", Run: func(cmd *cobra.Command, args []string) { s := service.WindowsService{ Service: service.Service{ diff --git a/cmd/uninstall_windows.go b/cmd/uninstall_windows.go index 4bbade81..6f467d70 100644 --- a/cmd/uninstall_windows.go +++ b/cmd/uninstall_windows.go @@ -12,7 +12,7 @@ import ( var ( uninstallCmd = &cobra.Command{ Use: "uninstall", - Short: "Uninstall SFTPGo Windows Service", + Short: "Uninstall the SFTPGo Windows Service", Run: func(cmd *cobra.Command, args []string) { s := service.WindowsService{ Service: service.Service{ diff --git a/docs/full-configuration.md b/docs/full-configuration.md index 450c199c..b079a6c1 100644 --- a/docs/full-configuration.md +++ b/docs/full-configuration.md @@ -11,12 +11,12 @@ Usage: Available Commands: gen A collection of useful generators help Help about any command - initprovider Initializes and/or updates the configured data provider - portable Serve a single directory + initprovider Initialize and/or updates the configured data provider + portable Serve a single directory/account revertprovider Revert the configured data provider to a previous version serve Start the SFTPGo service smtptest Test the SMTP configuration - startsubsys Use SFTPGo as SFTP file transfer subsystem + startsubsys Use sftpgo as SFTP file transfer subsystem Flags: -h, --help help for sftpgo