From 988b386e8b5ed50e9fdd29bb7185f008dfeeb8c5 Mon Sep 17 00:00:00 2001
From: Herbert Graeber <herbert@graeber-clan.de>
Date: Sun, 11 Sep 2022 00:44:12 +0200
Subject: [PATCH] Add powershell completion

- Add code for powersehll complition available in cobra
- Add documentation for powershell completion
- Add changelog for pr3925
---
 changelog/unreleased/pull-3925  |  6 ++++++
 cmd/restic/cmd_generate.go      | 24 +++++++++++++++++++-----
 doc/020_installation.rst        |  5 +++--
 helpers/prepare-release/main.go |  1 +
 4 files changed, 29 insertions(+), 7 deletions(-)
 create mode 100644 changelog/unreleased/pull-3925

diff --git a/changelog/unreleased/pull-3925 b/changelog/unreleased/pull-3925
new file mode 100644
index 000000000..1dcf26e2a
--- /dev/null
+++ b/changelog/unreleased/pull-3925
@@ -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
\ No newline at end of file
diff --git a/cmd/restic/cmd_generate.go b/cmd/restic/cmd_generate.go
index 710c5c721..959a9d518 100644
--- a/cmd/restic/cmd_generate.go
+++ b/cmd/restic/cmd_generate.go
@@ -10,7 +10,7 @@ import (
 
 var cmdGenerate = &cobra.Command{
 	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: `
 The "generate" command writes automatically generated files (like the man pages
 and the auto-completion files for bash, fish and zsh).
@@ -25,10 +25,11 @@ Exit status is 0 if the command was successful, and non-zero if there was any er
 }
 
 type generateOptions struct {
-	ManDir             string
-	BashCompletionFile string
-	FishCompletionFile string
-	ZSHCompletionFile  string
+	ManDir                   string
+	BashCompletionFile       string
+	FishCompletionFile       string
+	ZSHCompletionFile        string
+	PowerShellCompletionFile string
 }
 
 var genOpts generateOptions
@@ -40,6 +41,7 @@ func init() {
 	fs.StringVar(&genOpts.BashCompletionFile, "bash-completion", "", "write bash 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.PowerShellCompletionFile, "powershell-completion", "", "write powershell completion `file`")
 }
 
 func writeManpages(dir string) error {
@@ -75,6 +77,11 @@ func writeZSHCompletion(file string) error {
 	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 {
 	if 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
 	if genOpts == empty {
 		return errors.Fatal("nothing to do, please specify at least one output file/dir")
diff --git a/doc/020_installation.rst b/doc/020_installation.rst
index 9f6ffa141..3e98625f9 100644
--- a/doc/020_installation.rst
+++ b/doc/020_installation.rst
@@ -313,14 +313,14 @@ compiler. Building restic with gccgo may work, but is not supported.
 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
 
     $ ./restic generate --help
 
     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:
       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
       -h, --help                   help for generate
           --man directory          write man pages to directory
+          --powershell-completion  write powershell 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:
diff --git a/helpers/prepare-release/main.go b/helpers/prepare-release/main.go
index 2d38b0dd9..ca5afd742 100644
--- a/helpers/prepare-release/main.go
+++ b/helpers/prepare-release/main.go
@@ -290,6 +290,7 @@ func generateFiles() {
 	run("./restic-generate.temp", "generate",
 		"--man", "doc/man",
 		"--zsh-completion", "doc/zsh-completion.zsh",
+		"--powershell-completion", "doc/powershell-completion.ps1",
 		"--fish-completion", "doc/fish-completion.fish",
 		"--bash-completion", "doc/bash-completion.sh")
 	rm("restic-generate.temp")