Browse Source

cmd: improve completion sub-commands

Nicola Murino 4 years ago
parent
commit
22d28a37b6

+ 73 - 40
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.
+
+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.
 
-Bash:
+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:
+
+Linux:
+  $ sftpgo gen completion zsh > > "${fpath[1]}/_sftpgo"
+
+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)
+	},
+}
 
-$ sftpgo gen completion zsh > "${fpath[1]}/_sftpgo"
+var genCompletionFishCmd = &cobra.Command{
+	Use:   "fish",
+	Short: "Generate the autocompletion script for fish",
+	Long: `Generate the autocompletion script for the fish shell.
 
-Fish:
+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:
+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().GenFishCompletion(os.Stdout, true)
+	},
+}
 
-PS> sftpgo gen completion powershell | Out-String | Invoke-Expression
+var genCompletionPowerShellCmd = &cobra.Command{
+	Use:   "powershell",
+	Short: "Generate the autocompletion script for powershell",
+	Long: `Generate the autocompletion script for powershell.
 
-To load completions for every new session, run:
+To load completions in your current shell session:
 
-PS> sftpgo gen completion powershell > sftpgo.ps1
+PS C:\> sftpgo gen completion powershell | Out-String | Invoke-Expression
 
-and source this file from your powershell profile.
+To load completions for every new session, add the output of the above command
+to your powershell profile.
 `,
 	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().GenPowerShellCompletionWithDesc(os.Stdout)
 	},
 }
 
 func init() {
+	genCompletionCmd.AddCommand(genCompletionBashCmd)
+	genCompletionCmd.AddCommand(genCompletionZshCmd)
+	genCompletionCmd.AddCommand(genCompletionFishCmd)
+	genCompletionCmd.AddCommand(genCompletionPowerShellCmd)
+
 	genCmd.AddCommand(genCompletionCmd)
 }

+ 4 - 3
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()

+ 1 - 1
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.

+ 1 - 1
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:
 

+ 1 - 1
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",
 	}
 )
 

+ 1 - 1
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) {

+ 1 - 1
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

+ 1 - 1
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{

+ 1 - 1
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{

+ 3 - 3
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