From 9c427e95e224e8e375c586e5cbeafe17fa4a1ce6 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Tue, 28 Feb 2017 19:02:24 +0100 Subject: [PATCH] Add update cli command, cleanup --- cmd/new.go | 14 ++------------ cmd/update.go | 34 +++++++++++++++++++++++++++------- cmd/utils.go | 16 ++++++++++++++++ 3 files changed, 45 insertions(+), 19 deletions(-) diff --git a/cmd/new.go b/cmd/new.go index 38d9762f..03e8fd2c 100644 --- a/cmd/new.go +++ b/cmd/new.go @@ -36,26 +36,16 @@ func init() { func newPassword(cmd *cobra.Command, args []string) { if len(args) != 1 || len(args[0]) == 0 { - fmt.Print("You must provide a name for the key") + fmt.Println("You must provide a name for the key") return } name := args[0] - // TODO: own function??? - pass, err := getPassword("Enter a passphrase:") + pass, err := getCheckPassword("Enter a passphrase:", "Repeat the passphrase:") if err != nil { fmt.Println(err.Error()) return } - pass2, err := getPassword("Repeat the passphrase:") - if err != nil { - fmt.Println(err.Error()) - return - } - if pass != pass2 { - fmt.Println("Passphrases don't match") - return - } info, err := manager.Create(name, pass) if err != nil { diff --git a/cmd/update.go b/cmd/update.go index c36bcf25..9b5387cb 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -18,7 +18,6 @@ import ( "fmt" "github.com/spf13/cobra" - "github.com/spf13/viper" ) // updateCmd represents the update command @@ -26,14 +25,35 @@ var updateCmd = &cobra.Command{ Use: "update", Short: "Change the password for a private key", Long: `Change the password for a private key.`, - Run: func(cmd *cobra.Command, args []string) { - // TODO: Work your own magic here - fmt.Println(viper.Get("name")) - fmt.Println("update called") - }, + Run: updatePassword, } func init() { RootCmd.AddCommand(updateCmd) - updateCmd.Flags().StringP("name", "n", "", "Name of key to update") +} + +func updatePassword(cmd *cobra.Command, args []string) { + if len(args) != 1 || len(args[0]) == 0 { + fmt.Println("You must provide a name for the key") + return + } + name := args[0] + + oldpass, err := getPassword("Enter the current passphrase:") + if err != nil { + fmt.Println(err.Error()) + return + } + newpass, err := getCheckPassword("Enter the new passphrase:", "Repeat the new passphrase:") + if err != nil { + fmt.Println(err.Error()) + return + } + + err = manager.Update(name, oldpass, newpass) + if err != nil { + fmt.Println(err.Error()) + } else { + fmt.Println("Password successfully updated!") + } } diff --git a/cmd/utils.go b/cmd/utils.go index bd067f9c..9a62fe7e 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -22,6 +22,22 @@ func getPassword(prompt string) (string, error) { return pass, nil } +func getCheckPassword(prompt, prompt2 string) (string, error) { + // TODO: own function??? + pass, err := getPassword(prompt) + if err != nil { + return "", err + } + pass2, err := getPassword(prompt2) + if err != nil { + return "", err + } + if pass != pass2 { + return "", errors.New("Passphrases don't match") + } + return pass, nil +} + func printInfo(info keys.Info) { switch output { case "text":