2017-08-31 16:52:51 +02:00
|
|
|
package genautocomplete
|
|
|
|
|
|
|
|
import (
|
2024-08-18 16:58:35 +02:00
|
|
|
"fmt"
|
2017-08-31 16:52:51 +02:00
|
|
|
"os"
|
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/cmd"
|
2024-08-18 16:58:35 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
2017-08-31 16:52:51 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
completionDefinition.AddCommand(zshCommandDefinition)
|
|
|
|
}
|
|
|
|
|
|
|
|
var zshCommandDefinition = &cobra.Command{
|
|
|
|
Use: "zsh [output_file]",
|
|
|
|
Short: `Output zsh completion script for rclone.`,
|
2024-08-12 18:17:46 +02:00
|
|
|
Long: `Generates a zsh autocompletion script for rclone.
|
2017-08-31 16:52:51 +02:00
|
|
|
|
|
|
|
This writes to /usr/share/zsh/vendor-completions/_rclone by default so will
|
2020-10-13 23:49:58 +02:00
|
|
|
probably need to be run with sudo or as root, e.g.
|
2017-08-31 16:52:51 +02:00
|
|
|
|
2024-01-31 17:43:20 +01:00
|
|
|
sudo rclone completion zsh
|
2017-08-31 16:52:51 +02:00
|
|
|
|
|
|
|
Logout and login again to use the autocompletion scripts, or source
|
|
|
|
them directly
|
|
|
|
|
|
|
|
autoload -U compinit && compinit
|
|
|
|
|
|
|
|
If you supply a command line argument the script will be written
|
|
|
|
there.
|
2020-10-10 23:16:21 +02:00
|
|
|
|
|
|
|
If output_file is "-", then the output will be written to stdout.
|
2017-08-31 16:52:51 +02:00
|
|
|
`,
|
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
cmd.CheckArgs(0, 1, command, args)
|
|
|
|
out := "/usr/share/zsh/vendor-completions/_rclone"
|
|
|
|
if len(args) > 0 {
|
2020-10-10 23:16:21 +02:00
|
|
|
if args[0] == "-" {
|
|
|
|
err := cmd.Root.GenZshCompletion(os.Stdout)
|
|
|
|
if err != nil {
|
2024-08-18 16:58:35 +02:00
|
|
|
fs.Fatal(nil, fmt.Sprint(err))
|
2020-10-10 23:16:21 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2017-08-31 16:52:51 +02:00
|
|
|
out = args[0]
|
|
|
|
}
|
|
|
|
outFile, err := os.Create(out)
|
|
|
|
if err != nil {
|
2024-08-18 16:58:35 +02:00
|
|
|
fs.Fatal(nil, fmt.Sprint(err))
|
2017-08-31 16:52:51 +02:00
|
|
|
}
|
|
|
|
defer func() { _ = outFile.Close() }()
|
|
|
|
err = cmd.Root.GenZshCompletion(outFile)
|
|
|
|
if err != nil {
|
2024-08-18 16:58:35 +02:00
|
|
|
fs.Fatal(nil, fmt.Sprint(err))
|
2017-08-31 16:52:51 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|