mirror of
https://github.com/restic/restic.git
synced 2026-08-25 06:53:18 +00:00
Add command "key add"
This commit is contained in:
+21
-1
@@ -36,14 +36,34 @@ func list_keys(be backend.Server, key *khepri.Key) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func add_key(be backend.Server, key *khepri.Key) error {
|
||||
pw := readPassword("KHEPRI_NEWPASSWORD", "enter password for new key: ")
|
||||
pw2 := readPassword("KHEPRI_NEWPASSWORD", "enter password again: ")
|
||||
|
||||
if pw != pw2 {
|
||||
errx(1, "passwords do not match")
|
||||
}
|
||||
|
||||
id, err := key.AddKey(be, pw)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating new key failed: %v\n", err)
|
||||
}
|
||||
|
||||
fmt.Printf("saved new key as %s\n", id)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func commandKey(be backend.Server, key *khepri.Key, args []string) error {
|
||||
if len(args) < 1 {
|
||||
return errors.New("usage: key [list]")
|
||||
return errors.New("usage: key [list|add]")
|
||||
}
|
||||
|
||||
switch args[0] {
|
||||
case "list":
|
||||
return list_keys(be, key)
|
||||
case "add":
|
||||
return add_key(be, key)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
+11
-7
@@ -34,10 +34,14 @@ type commandFunc func(backend.Server, *khepri.Key, []string) error
|
||||
|
||||
var commands map[string]commandFunc
|
||||
|
||||
func readPassword(prompt string) string {
|
||||
p := os.Getenv("KHEPRI_PASSWORD")
|
||||
if p != "" {
|
||||
return p
|
||||
func readPassword(env string, prompt string) string {
|
||||
|
||||
if env != "" {
|
||||
p := os.Getenv(env)
|
||||
|
||||
if p != "" {
|
||||
return p
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Print(prompt)
|
||||
@@ -51,8 +55,8 @@ func readPassword(prompt string) string {
|
||||
}
|
||||
|
||||
func commandInit(repo string) error {
|
||||
pw := readPassword("enter password for new backend: ")
|
||||
pw2 := readPassword("enter password again: ")
|
||||
pw := readPassword("KHEPRI_PASSWORD", "enter password for new backend: ")
|
||||
pw2 := readPassword("KHEPRI_PASSWORD", "enter password again: ")
|
||||
|
||||
if pw != pw2 {
|
||||
errx(1, "passwords do not match")
|
||||
@@ -188,7 +192,7 @@ func main() {
|
||||
errx(1, "unable to open repo: %v", err)
|
||||
}
|
||||
|
||||
key, err := khepri.SearchKey(repo, readPassword("Enter Password for Repository: "))
|
||||
key, err := khepri.SearchKey(repo, readPassword("KHEPRI_PASSWORD", "Enter Password for Repository: "))
|
||||
if err != nil {
|
||||
errx(2, "unable to open repo: %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user