2017-09-26 11:07:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/cobra/doc"
|
|
|
|
)
|
|
|
|
|
|
|
|
var cmdGenerate = &cobra.Command{
|
2020-08-27 03:29:43 +00:00
|
|
|
Use: "generate [flags]",
|
2022-09-10 22:44:12 +00:00
|
|
|
Short: "Generate manual pages and auto-completion files (bash, fish, zsh, powershell)",
|
2017-09-26 11:07:13 +00:00
|
|
|
Long: `
|
2019-04-23 02:06:32 +00:00
|
|
|
The "generate" command writes automatically generated files (like the man pages
|
2021-03-01 02:15:59 +00:00
|
|
|
and the auto-completion files for bash, fish and zsh).
|
2019-11-05 06:03:38 +00:00
|
|
|
|
|
|
|
EXIT STATUS
|
|
|
|
===========
|
|
|
|
|
|
|
|
Exit status is 0 if the command was successful, and non-zero if there was any error.
|
2017-09-26 11:07:13 +00:00
|
|
|
`,
|
|
|
|
DisableAutoGenTag: true,
|
|
|
|
RunE: runGenerate,
|
|
|
|
}
|
|
|
|
|
|
|
|
type generateOptions struct {
|
2022-09-10 22:44:12 +00:00
|
|
|
ManDir string
|
|
|
|
BashCompletionFile string
|
|
|
|
FishCompletionFile string
|
|
|
|
ZSHCompletionFile string
|
|
|
|
PowerShellCompletionFile string
|
2017-09-26 11:07:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var genOpts generateOptions
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cmdRoot.AddCommand(cmdGenerate)
|
|
|
|
fs := cmdGenerate.Flags()
|
|
|
|
fs.StringVar(&genOpts.ManDir, "man", "", "write man pages to `directory`")
|
|
|
|
fs.StringVar(&genOpts.BashCompletionFile, "bash-completion", "", "write bash completion `file`")
|
2021-03-01 02:15:59 +00:00
|
|
|
fs.StringVar(&genOpts.FishCompletionFile, "fish-completion", "", "write fish completion `file`")
|
2017-09-26 11:07:13 +00:00
|
|
|
fs.StringVar(&genOpts.ZSHCompletionFile, "zsh-completion", "", "write zsh completion `file`")
|
2022-09-10 22:44:12 +00:00
|
|
|
fs.StringVar(&genOpts.PowerShellCompletionFile, "powershell-completion", "", "write powershell completion `file`")
|
2017-09-26 11:07:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func writeManpages(dir string) error {
|
|
|
|
// use a fixed date for the man pages so that generating them is deterministic
|
|
|
|
date, err := time.Parse("Jan 2006", "Jan 2017")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
header := &doc.GenManHeader{
|
|
|
|
Title: "restic backup",
|
|
|
|
Section: "1",
|
|
|
|
Source: "generated by `restic generate`",
|
|
|
|
Date: &date,
|
|
|
|
}
|
|
|
|
|
|
|
|
Verbosef("writing man pages to directory %v\n", dir)
|
|
|
|
return doc.GenManTree(cmdRoot, header, dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeBashCompletion(file string) error {
|
2023-03-22 07:18:08 +00:00
|
|
|
if stdoutIsTerminal() {
|
|
|
|
Verbosef("writing bash completion file to %v\n", file)
|
|
|
|
}
|
2017-09-26 11:07:13 +00:00
|
|
|
return cmdRoot.GenBashCompletionFile(file)
|
|
|
|
}
|
|
|
|
|
2021-03-01 02:15:59 +00:00
|
|
|
func writeFishCompletion(file string) error {
|
2023-03-22 07:18:08 +00:00
|
|
|
if stdoutIsTerminal() {
|
|
|
|
Verbosef("writing fish completion file to %v\n", file)
|
|
|
|
}
|
2021-03-01 02:15:59 +00:00
|
|
|
return cmdRoot.GenFishCompletionFile(file, true)
|
|
|
|
}
|
|
|
|
|
2017-09-26 11:07:13 +00:00
|
|
|
func writeZSHCompletion(file string) error {
|
2023-03-22 07:18:08 +00:00
|
|
|
if stdoutIsTerminal() {
|
|
|
|
Verbosef("writing zsh completion file to %v\n", file)
|
|
|
|
}
|
2017-09-26 11:07:13 +00:00
|
|
|
return cmdRoot.GenZshCompletionFile(file)
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:44:12 +00:00
|
|
|
func writePowerShellCompletion(file string) error {
|
2023-03-22 07:18:08 +00:00
|
|
|
if stdoutIsTerminal() {
|
|
|
|
Verbosef("writing powershell completion file to %v\n", file)
|
|
|
|
}
|
2022-09-10 22:44:12 +00:00
|
|
|
return cmdRoot.GenPowerShellCompletionFile(file)
|
|
|
|
}
|
|
|
|
|
2023-05-18 17:48:20 +00:00
|
|
|
func runGenerate(_ *cobra.Command, args []string) error {
|
|
|
|
if len(args) > 0 {
|
|
|
|
return errors.Fatal("the generate command expects no arguments, only options - please see `restic help generate` for usage and flags")
|
|
|
|
}
|
|
|
|
|
2017-09-26 11:07:13 +00:00
|
|
|
if genOpts.ManDir != "" {
|
|
|
|
err := writeManpages(genOpts.ManDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if genOpts.BashCompletionFile != "" {
|
|
|
|
err := writeBashCompletion(genOpts.BashCompletionFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 02:15:59 +00:00
|
|
|
if genOpts.FishCompletionFile != "" {
|
|
|
|
err := writeFishCompletion(genOpts.FishCompletionFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-26 11:07:13 +00:00
|
|
|
if genOpts.ZSHCompletionFile != "" {
|
|
|
|
err := writeZSHCompletion(genOpts.ZSHCompletionFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:44:12 +00:00
|
|
|
if genOpts.PowerShellCompletionFile != "" {
|
|
|
|
err := writePowerShellCompletion(genOpts.PowerShellCompletionFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-26 11:07:13 +00:00
|
|
|
var empty generateOptions
|
|
|
|
if genOpts == empty {
|
|
|
|
return errors.Fatal("nothing to do, please specify at least one output file/dir")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|