diff --git a/Makefile b/Makefile index d976713..43f1b5d 100644 --- a/Makefile +++ b/Makefile @@ -167,12 +167,16 @@ $(ARTIFACTDIR): $(ARTIFACTDIR)/docs: $(ARTIFACTDIR) mkdir -p "$@" -noarch: $(ARTIFACTDIR)/bash_completion $(ARTIFACTDIR)/go_env.txt docs +noarch: $(ARTIFACTDIR)/bash_completion $(ARTIFACTDIR)/_zrepl.zsh_completion $(ARTIFACTDIR)/go_env.txt docs # pass $(ARTIFACTDIR)/bash_completion: $(MAKE) zrepl-bin GOOS=$(GOHOSTOS) GOARCH=$(GOHOSTARCH) - artifacts/zrepl-$(GOHOSTOS)-$(GOHOSTARCH) bashcomp "$@" + artifacts/zrepl-$(GOHOSTOS)-$(GOHOSTARCH) gencompletion bash "$@" + +$(ARTIFACTDIR)/_zrepl.zsh_completion: + $(MAKE) zrepl-bin GOOS=$(GOHOSTOS) GOARCH=$(GOHOSTARCH) + artifacts/zrepl-$(GOHOSTOS)-$(GOHOSTARCH) gencompletion zsh "$@" $(ARTIFACTDIR)/go_env.txt: $(GO_ENV_VARS) $(GO) env > $@ diff --git a/cli/cli.go b/cli/cli.go index e84affe..3546fe0 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -19,29 +19,55 @@ var rootCmd = &cobra.Command{ Short: "One-stop ZFS replication solution", } -var bashcompCmd = &cobra.Command{ - Use: "bashcomp path/to/out/file", - Short: "generate bash completions", - Run: func(cmd *cobra.Command, args []string) { - if len(args) != 1 { - fmt.Fprintf(os.Stderr, "specify exactly one positional agument\n") - err := cmd.Usage() - if err != nil { - panic(err) - } - os.Exit(1) - } - if err := rootCmd.GenBashCompletionFile(args[0]); err != nil { - fmt.Fprintf(os.Stderr, "error generating bash completion: %s", err) - os.Exit(1) - } +func init() { + rootCmd.PersistentFlags().StringVar(&rootArgs.configPath, "config", "", "config file path") +} + +var genCompletionCmd = &cobra.Command{ + Use: "gencompletion", + Short: "generate shell auto-completions", +} + +type completionCmdInfo struct { + genFunc func(outpath string) error + help string +} + +var completionCmdMap = map[string]completionCmdInfo{ + "zsh": { + rootCmd.GenZshCompletionFile, + " save to file `_zrepl` in your zsh's $fpath", + }, + "bash": { + rootCmd.GenBashCompletionFile, + " save to a path and source that path in your .bashrc", }, - Hidden: true, } func init() { - rootCmd.PersistentFlags().StringVar(&rootArgs.configPath, "config", "", "config file path") - rootCmd.AddCommand(bashcompCmd) + for sh, info := range completionCmdMap { + sh, info := sh, info + genCompletionCmd.AddCommand(&cobra.Command{ + Use: fmt.Sprintf("%s path/to/out/file", sh), + Short: fmt.Sprintf("generate %s completions", sh), + Example: info.help, + Run: func(cmd *cobra.Command, args []string) { + if len(args) != 1 { + fmt.Fprintf(os.Stderr, "specify exactly one positional agument\n") + err := cmd.Usage() + if err != nil { + panic(err) + } + os.Exit(1) + } + if err := info.genFunc(args[0]); err != nil { + fmt.Fprintf(os.Stderr, "error generating %s completion: %s", sh, err) + os.Exit(1) + } + }, + }) + } + rootCmd.AddCommand(genCompletionCmd) } type Subcommand struct { diff --git a/docs/changelog.rst b/docs/changelog.rst index 32f77ff..f8ce0f2 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -62,7 +62,9 @@ Actual changelog: * |bugfix| |docs| snapshotting: clarify sync-up behavior and warn about filesystems that will not be snapshotted until the sync-up phase is over * |docs| Document new replication features in the :ref:`config overview ` and :repomasterlink:`replication/design.md`. +* |feature| documented subcommand to generate ``bash`` and ``zsh`` completions * **[MAINTAINER NOTICE]** New platform tests in this version, please make sure you run them for your distro! +* **[MAINTAINER NOTICE]** Please add the shell completions to the zrepl packages. 0.2.1 -----