diff --git a/cmd/restic/cmd_key.go b/cmd/restic/cmd_key.go index 7552c778d..d81be6bb9 100644 --- a/cmd/restic/cmd_key.go +++ b/cmd/restic/cmd_key.go @@ -3,6 +3,9 @@ package main import ( "context" "fmt" + "io/ioutil" + "os" + "strings" "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/repository" @@ -23,8 +26,13 @@ The "key" command manages keys (passwords) for accessing the repository. }, } +var newPasswordFile string + func init() { cmdRoot.AddCommand(cmdKey) + + flags := cmdKey.Flags() + flags.StringVarP(&newPasswordFile, "new-password-file", "", "", "the file from which to load a new password") } func listKeys(ctx context.Context, s *repository.Repository) error { @@ -64,6 +72,10 @@ func getNewPassword(gopts GlobalOptions) (string, error) { return testKeyNewPassword, nil } + if newPasswordFile != "" { + return loadPasswordFromFile(newPasswordFile) + } + // Since we already have an open repository, temporary remove the password // to prompt the user for the passwd. newopts := gopts @@ -182,3 +194,11 @@ func runKey(gopts GlobalOptions, args []string) error { return nil } + +func loadPasswordFromFile(pwdFile string) (string, error) { + s, err := ioutil.ReadFile(pwdFile) + if os.IsNotExist(err) { + return "", errors.Fatalf("%s does not exist", pwdFile) + } + return strings.TrimSpace(string(s)), errors.Wrap(err, "Readfile") +}