key: Add flag for non-interactive password changes

This commit is contained in:
Matthew Holt 2018-04-14 18:40:57 -06:00 committed by Alexander Neumann
parent a9c2e84ccd
commit c5829e9ffc
1 changed files with 20 additions and 0 deletions

View File

@ -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")
}