Merge pull request #4951 from MichaelEischer/better-completions-poc
Improve CLI completions POC
This commit is contained in:
commit
4ee3c9c8b9
2 changed files with 15 additions and 4 deletions
|
@ -12,6 +12,8 @@ import (
|
||||||
"github.com/restic/restic/internal/restic"
|
"github.com/restic/restic/internal/restic"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var catAllowedCmds = []string{"config", "index", "snapshot", "key", "masterkey", "lock", "pack", "blob", "tree"}
|
||||||
|
|
||||||
var cmdCat = &cobra.Command{
|
var cmdCat = &cobra.Command{
|
||||||
Use: "cat [flags] [masterkey|config|pack ID|blob ID|snapshot ID|index ID|key ID|lock ID|tree snapshot:subfolder]",
|
Use: "cat [flags] [masterkey|config|pack ID|blob ID|snapshot ID|index ID|key ID|lock ID|tree snapshot:subfolder]",
|
||||||
Short: "Print internal objects to stdout",
|
Short: "Print internal objects to stdout",
|
||||||
|
@ -30,6 +32,7 @@ Exit status is 11 if the repository is already locked.
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return runCat(cmd.Context(), globalOptions, args)
|
return runCat(cmd.Context(), globalOptions, args)
|
||||||
},
|
},
|
||||||
|
ValidArgs: catAllowedCmds,
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -37,21 +40,19 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateCatArgs(args []string) error {
|
func validateCatArgs(args []string) error {
|
||||||
var allowedCmds = []string{"config", "index", "snapshot", "key", "masterkey", "lock", "pack", "blob", "tree"}
|
|
||||||
|
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
return errors.Fatal("type not specified")
|
return errors.Fatal("type not specified")
|
||||||
}
|
}
|
||||||
|
|
||||||
validType := false
|
validType := false
|
||||||
for _, v := range allowedCmds {
|
for _, v := range catAllowedCmds {
|
||||||
if v == args[0] {
|
if v == args[0] {
|
||||||
validType = true
|
validType = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !validType {
|
if !validType {
|
||||||
return errors.Fatalf("invalid type %q, must be one of [%s]", args[0], strings.Join(allowedCmds, "|"))
|
return errors.Fatalf("invalid type %q, must be one of [%s]", args[0], strings.Join(catAllowedCmds, "|"))
|
||||||
}
|
}
|
||||||
|
|
||||||
if args[0] != "masterkey" && args[0] != "config" && len(args) != 2 {
|
if args[0] != "masterkey" && args[0] != "config" && len(args) != 2 {
|
||||||
|
|
|
@ -70,10 +70,20 @@ type StatsOptions struct {
|
||||||
|
|
||||||
var statsOptions StatsOptions
|
var statsOptions StatsOptions
|
||||||
|
|
||||||
|
func must(err error) {
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("error during setup: %v", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cmdRoot.AddCommand(cmdStats)
|
cmdRoot.AddCommand(cmdStats)
|
||||||
f := cmdStats.Flags()
|
f := cmdStats.Flags()
|
||||||
f.StringVar(&statsOptions.countMode, "mode", countModeRestoreSize, "counting mode: restore-size (default), files-by-contents, blobs-per-file or raw-data")
|
f.StringVar(&statsOptions.countMode, "mode", countModeRestoreSize, "counting mode: restore-size (default), files-by-contents, blobs-per-file or raw-data")
|
||||||
|
must(cmdStats.RegisterFlagCompletionFunc("mode", func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
|
||||||
|
return []string{countModeRestoreSize, countModeUniqueFilesByContents, countModeBlobsPerFile, countModeRawData}, cobra.ShellCompDirectiveDefault
|
||||||
|
}))
|
||||||
|
|
||||||
initMultiSnapshotFilter(f, &statsOptions.SnapshotFilter, true)
|
initMultiSnapshotFilter(f, &statsOptions.SnapshotFilter, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue