2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-22 04:45:15 +00:00

Check for arguments before opening remote connection in cat command

Signed-off-by: Yakul Garg <2000yeshu@gmail.com>
This commit is contained in:
Yakul Garg 2023-08-12 23:54:40 +05:30
parent 57a08291f5
commit 705556f134
No known key found for this signature in database
GPG Key ID: C60D3CF949331300

View File

@ -3,6 +3,7 @@ package main
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -12,6 +13,8 @@ import (
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
) )
var allowedCmds = []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",
@ -33,9 +36,26 @@ func init() {
cmdRoot.AddCommand(cmdCat) cmdRoot.AddCommand(cmdCat)
} }
func validateParam(param string) bool {
for _, v := range allowedCmds {
if v == param {
return true
}
}
return false
}
func runCat(ctx context.Context, gopts GlobalOptions, args []string) error { func runCat(ctx context.Context, gopts GlobalOptions, args []string) error {
if len(args) < 1 || (args[0] != "masterkey" && args[0] != "config" && len(args) != 2) { if len(args) < 1 {
return errors.Fatal("type or ID not specified") return errors.Fatal("type not specified")
}
if ok := validateParam(args[0]); !ok {
return errors.Fatalf("invalid type %q, must be one of [%s]", args[0], strings.Join(allowedCmds, "|"))
}
if args[0] != "masterkey" && args[0] != "config" && len(args) != 2 {
return errors.Fatal("ID not specified")
} }
repo, err := OpenRepository(ctx, gopts) repo, err := OpenRepository(ctx, gopts)