From 2c42629c5109cf88b46629a727343d7e255a32a7 Mon Sep 17 00:00:00 2001 From: Pauline Middelink Date: Sat, 6 May 2017 12:37:40 +0200 Subject: [PATCH] Add bash autocompletion generation Fix #942 --- doc/manual.rst | 33 ++++++++++++++++++++++++++ src/cmds/restic/cmd_autocomplete.go | 36 +++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 src/cmds/restic/cmd_autocomplete.go diff --git a/doc/manual.rst b/doc/manual.rst index 740b91ff5..cdf166c7a 100644 --- a/doc/manual.rst +++ b/doc/manual.rst @@ -16,6 +16,7 @@ Usage help is available: restic [command] Available Commands: + autocomplete generate shell autocompletion script backup create a new backup of files and/or directories cat print internal objects to stdout check check the repository for errors @@ -795,6 +796,38 @@ is a safety feature: it prevents restic from removing many snapshots when no new ones are created. If it was implemented otherwise, running ``forget --keep-daily 4`` on a Friday would remove all snapshots! +Autocompletion +-------------- + +Restic can write out a bash compatible autocompletion script: + +.. code-block:: console + + $ ./restic autocomplete --help + The "autocomplete" command generates a shell autocompletion script. + + NOTE: The current version supports Bash only. + This should work for *nix systems with Bash installed. + + By default, the file is written directly to /etc/bash_completion.d + for convenience, and the command may need superuser rights, e.g.: + + $ sudo restic autocomplete + + Usage: + restic autocomplete [flags] + + Flags: + --completionfile string autocompletion file (default "/etc/bash_completion.d/restic.sh") + + Global Flags: + --json set output mode to JSON for commands that support it + --no-lock do not lock the repo, this allows some operations on read-only repos + -o, --option key=value set extended option (key=value, can be specified multiple times) + -p, --password-file string read the repository password from a file + -q, --quiet do not output comprehensive progress report + -r, --repo string repository to backup to or restore from (default: $RESTIC_REPOSITORY) + Debugging --------- diff --git a/src/cmds/restic/cmd_autocomplete.go b/src/cmds/restic/cmd_autocomplete.go new file mode 100644 index 000000000..881b9ca0e --- /dev/null +++ b/src/cmds/restic/cmd_autocomplete.go @@ -0,0 +1,36 @@ +package main + +import ( + "github.com/spf13/cobra" +) + +var autocompleteTarget string + +var cmdAutocomplete = &cobra.Command{ + Use: "autocomplete", + Short: "generate shell autocompletion script", + Long: `The "autocomplete" command generates a shell autocompletion script. + +NOTE: The current version supports Bash only. + This should work for *nix systems with Bash installed. + +By default, the file is written directly to /etc/bash_completion.d +for convenience, and the command may need superuser rights, e.g.: + +$ sudo restic autocomplete`, + + RunE: func(cmd *cobra.Command, args []string) error { + if err := cmdRoot.GenBashCompletionFile(autocompleteTarget); err != nil { + return err + } + return nil + }, +} + +func init() { + cmdRoot.AddCommand(cmdAutocomplete) + + cmdAutocomplete.Flags().StringVarP(&autocompleteTarget, "completionfile", "", "/etc/bash_completion.d/restic.sh", "autocompletion file") + // For bash-completion + cmdAutocomplete.Flags().SetAnnotation("completionfile", cobra.BashCompFilenameExt, []string{}) +}