cmd: improve completion sub-commands

This commit is contained in:
Nicola Murino
2021-10-03 08:14:57 +02:00
parent cc134cad9a
commit 22d28a37b6
10 changed files with 91 additions and 57 deletions

View File

@@ -3,84 +3,117 @@ package cmd
import ( import (
"os" "os"
"github.com/rs/zerolog"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/drakkan/sftpgo/v2/logger"
) )
var genCompletionCmd = &cobra.Command{ var genCompletionCmd = &cobra.Command{
Use: "completion [bash|zsh|fish|powershell]", Use: "completion [bash|zsh|fish|powershell]",
Short: "Generate shell completion script", Short: "Generate the autocompletion script for the specified shell",
Long: `To load completions: 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) $ source <(sftpgo gen completion bash)
To load completions for each session, execute once: To load completions for every new session, execute once:
Linux: Linux:
$ sudo sftpgo gen completion bash > /usr/share/bash-completion/completions/sftpgo
$ sudo sftpgo gen completion bash > /usr/share/bash-completion/completions/sftpgo
MacOS: 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 If shell completion is not already enabled in your environment you will need
to enable it. You can execute the following once: to enable it. You can execute the following once:
$ echo "autoload -U compinit; compinit" >> ~/.zshrc $ 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 $ 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 $ sftpgo gen completion fish > ~/.config/fish/completions/sftpgo.fish
Powershell: You will need to start a new shell for this setup to take effect.
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.
`, `,
DisableFlagsInUseLine: true, DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, RunE: func(cmd *cobra.Command, args []string) error {
Args: cobra.ExactValidArgs(1), return cmd.Root().GenFishCompletion(os.Stdout, true)
Run: func(cmd *cobra.Command, args []string) { },
var err error }
logger.DisableLogger()
logger.EnableConsoleLogger(zerolog.DebugLevel) var genCompletionPowerShellCmd = &cobra.Command{
switch args[0] { Use: "powershell",
case "bash": Short: "Generate the autocompletion script for powershell",
err = cmd.Root().GenBashCompletion(os.Stdout) Long: `Generate the autocompletion script for powershell.
case "zsh":
err = cmd.Root().GenZshCompletion(os.Stdout) To load completions in your current shell session:
case "fish":
err = cmd.Root().GenFishCompletion(os.Stdout, true) PS C:\> sftpgo gen completion powershell | Out-String | Invoke-Expression
case "powershell":
err = cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout) To load completions for every new session, add the output of the above command
} to your powershell profile.
if err != nil { `,
logger.WarnToConsole("Unable to generate shell completion script: %v", err) DisableFlagsInUseLine: true,
os.Exit(1) RunE: func(cmd *cobra.Command, args []string) error {
} return cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
}, },
} }
func init() { func init() {
genCompletionCmd.AddCommand(genCompletionBashCmd)
genCompletionCmd.AddCommand(genCompletionZshCmd)
genCompletionCmd.AddCommand(genCompletionFishCmd)
genCompletionCmd.AddCommand(genCompletionPowerShellCmd)
genCmd.AddCommand(genCompletionCmd) genCmd.AddCommand(genCompletionCmd)
} }

View File

@@ -16,10 +16,11 @@ var (
manDir string manDir string
genManCmd = &cobra.Command{ genManCmd = &cobra.Command{
Use: "man", 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 Long: `This command automatically generates up-to-date man pages of SFTPGo's
command-line interface. By default, it creates the man page files command-line interface.
in the "man" directory under the current directory. By default, it creates the man page files in the "man" directory under the
current directory.
`, `,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
logger.DisableLogger() logger.DisableLogger()

View File

@@ -16,7 +16,7 @@ import (
var ( var (
initProviderCmd = &cobra.Command{ initProviderCmd = &cobra.Command{
Use: "initprovider", 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 Long: `This command reads the data provider connection details from the specified
configuration file and creates the initial structure or update the existing one, configuration file and creates the initial structure or update the existing one,
as needed. as needed.

View File

@@ -78,7 +78,7 @@ var (
portableSFTPDBufferSize int64 portableSFTPDBufferSize int64
portableCmd = &cobra.Command{ portableCmd = &cobra.Command{
Use: "portable", 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 Long: `To serve the current working directory with auto generated credentials simply
use: use:

View File

@@ -7,7 +7,7 @@ import (
var ( var (
serviceCmd = &cobra.Command{ serviceCmd = &cobra.Command{
Use: "service", Use: "service",
Short: "Manage SFTPGo Windows Service", Short: "Manage the SFTPGo Windows Service",
} }
) )

View File

@@ -14,7 +14,7 @@ import (
var ( var (
startCmd = &cobra.Command{ startCmd = &cobra.Command{
Use: "start", Use: "start",
Short: "Start SFTPGo Windows Service", Short: "Start the SFTPGo Windows Service",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
configDir = util.CleanDirInput(configDir) configDir = util.CleanDirInput(configDir)
if !filepath.IsAbs(logFilePath) && util.IsFileInputValid(logFilePath) { if !filepath.IsAbs(logFilePath) && util.IsFileInputValid(logFilePath) {

View File

@@ -26,7 +26,7 @@ var (
baseHomeDir = "" baseHomeDir = ""
subsystemCmd = &cobra.Command{ subsystemCmd = &cobra.Command{
Use: "startsubsys", 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 Long: `In this mode SFTPGo speaks the server side of SFTP protocol to stdout and
expects client requests from stdin. expects client requests from stdin.
This mode is not intended to be called directly, but from sshd using the This mode is not intended to be called directly, but from sshd using the

View File

@@ -12,7 +12,7 @@ import (
var ( var (
stopCmd = &cobra.Command{ stopCmd = &cobra.Command{
Use: "stop", Use: "stop",
Short: "Stop SFTPGo Windows Service", Short: "Stop the SFTPGo Windows Service",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
s := service.WindowsService{ s := service.WindowsService{
Service: service.Service{ Service: service.Service{

View File

@@ -12,7 +12,7 @@ import (
var ( var (
uninstallCmd = &cobra.Command{ uninstallCmd = &cobra.Command{
Use: "uninstall", Use: "uninstall",
Short: "Uninstall SFTPGo Windows Service", Short: "Uninstall the SFTPGo Windows Service",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
s := service.WindowsService{ s := service.WindowsService{
Service: service.Service{ Service: service.Service{

View File

@@ -11,12 +11,12 @@ Usage:
Available Commands: Available Commands:
gen A collection of useful generators gen A collection of useful generators
help Help about any command help Help about any command
initprovider Initializes and/or updates the configured data provider initprovider Initialize and/or updates the configured data provider
portable Serve a single directory portable Serve a single directory/account
revertprovider Revert the configured data provider to a previous version revertprovider Revert the configured data provider to a previous version
serve Start the SFTPGo service serve Start the SFTPGo service
smtptest Test the SMTP configuration smtptest Test the SMTP configuration
startsubsys Use SFTPGo as SFTP file transfer subsystem startsubsys Use sftpgo as SFTP file transfer subsystem
Flags: Flags:
-h, --help help for sftpgo -h, --help help for sftpgo