2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-28 15:56:37 +00:00

Add powershell completion

- Add code for powersehll complition available in cobra
- Add documentation for powershell completion
- Add changelog for pr3925
This commit is contained in:
Herbert Graeber 2022-09-11 00:44:12 +02:00
parent 14d09a6081
commit 988b386e8b
4 changed files with 29 additions and 7 deletions

View File

@ -0,0 +1,6 @@
Enhancement: Provide command completion for powershell
Restic allows generation of completion files for bash, fish and zsh. Now powershell
is supported, too.
https://github.com/restic/restic/pull/3925/files

View File

@ -10,7 +10,7 @@ import (
var cmdGenerate = &cobra.Command{ var cmdGenerate = &cobra.Command{
Use: "generate [flags]", Use: "generate [flags]",
Short: "Generate manual pages and auto-completion files (bash, fish, zsh)", Short: "Generate manual pages and auto-completion files (bash, fish, zsh, powershell)",
Long: ` Long: `
The "generate" command writes automatically generated files (like the man pages The "generate" command writes automatically generated files (like the man pages
and the auto-completion files for bash, fish and zsh). and the auto-completion files for bash, fish and zsh).
@ -29,6 +29,7 @@ type generateOptions struct {
BashCompletionFile string BashCompletionFile string
FishCompletionFile string FishCompletionFile string
ZSHCompletionFile string ZSHCompletionFile string
PowerShellCompletionFile string
} }
var genOpts generateOptions var genOpts generateOptions
@ -40,6 +41,7 @@ func init() {
fs.StringVar(&genOpts.BashCompletionFile, "bash-completion", "", "write bash completion `file`") fs.StringVar(&genOpts.BashCompletionFile, "bash-completion", "", "write bash completion `file`")
fs.StringVar(&genOpts.FishCompletionFile, "fish-completion", "", "write fish completion `file`") fs.StringVar(&genOpts.FishCompletionFile, "fish-completion", "", "write fish completion `file`")
fs.StringVar(&genOpts.ZSHCompletionFile, "zsh-completion", "", "write zsh completion `file`") fs.StringVar(&genOpts.ZSHCompletionFile, "zsh-completion", "", "write zsh completion `file`")
fs.StringVar(&genOpts.PowerShellCompletionFile, "powershell-completion", "", "write powershell completion `file`")
} }
func writeManpages(dir string) error { func writeManpages(dir string) error {
@ -75,6 +77,11 @@ func writeZSHCompletion(file string) error {
return cmdRoot.GenZshCompletionFile(file) return cmdRoot.GenZshCompletionFile(file)
} }
func writePowerShellCompletion(file string) error {
Verbosef("writing powershell completion file to %v\n", file)
return cmdRoot.GenPowerShellCompletionFile(file)
}
func runGenerate(cmd *cobra.Command, args []string) error { func runGenerate(cmd *cobra.Command, args []string) error {
if genOpts.ManDir != "" { if genOpts.ManDir != "" {
err := writeManpages(genOpts.ManDir) err := writeManpages(genOpts.ManDir)
@ -104,6 +111,13 @@ func runGenerate(cmd *cobra.Command, args []string) error {
} }
} }
if genOpts.PowerShellCompletionFile != "" {
err := writePowerShellCompletion(genOpts.PowerShellCompletionFile)
if err != nil {
return err
}
}
var empty generateOptions var empty generateOptions
if genOpts == empty { if genOpts == empty {
return errors.Fatal("nothing to do, please specify at least one output file/dir") return errors.Fatal("nothing to do, please specify at least one output file/dir")

View File

@ -313,14 +313,14 @@ compiler. Building restic with gccgo may work, but is not supported.
Autocompletion Autocompletion
************** **************
Restic can write out man pages and bash/fish/zsh compatible autocompletion scripts: Restic can write out man pages and bash/fish/zsh/powershell compatible autocompletion scripts:
.. code-block:: console .. code-block:: console
$ ./restic generate --help $ ./restic generate --help
The "generate" command writes automatically generated files (like the man pages The "generate" command writes automatically generated files (like the man pages
and the auto-completion files for bash, fish and zsh). and the auto-completion files for bash, fish, zsh and powershell).
Usage: Usage:
restic generate [flags] [command] restic generate [flags] [command]
@ -330,6 +330,7 @@ Restic can write out man pages and bash/fish/zsh compatible autocompletion scrip
--fish-completion file write fish completion file --fish-completion file write fish completion file
-h, --help help for generate -h, --help help for generate
--man directory write man pages to directory --man directory write man pages to directory
--powershell-completion write powershell completion file
--zsh-completion file write zsh completion file --zsh-completion file write zsh completion file
Example for using sudo to write a bash completion script directly to the system-wide location: Example for using sudo to write a bash completion script directly to the system-wide location:

View File

@ -290,6 +290,7 @@ func generateFiles() {
run("./restic-generate.temp", "generate", run("./restic-generate.temp", "generate",
"--man", "doc/man", "--man", "doc/man",
"--zsh-completion", "doc/zsh-completion.zsh", "--zsh-completion", "doc/zsh-completion.zsh",
"--powershell-completion", "doc/powershell-completion.ps1",
"--fish-completion", "doc/fish-completion.fish", "--fish-completion", "doc/fish-completion.fish",
"--bash-completion", "doc/bash-completion.sh") "--bash-completion", "doc/bash-completion.sh")
rm("restic-generate.temp") rm("restic-generate.temp")