Add update cli command, cleanup

This commit is contained in:
Ethan Frey 2017-02-28 19:02:24 +01:00
parent 506ff7d85a
commit 9c427e95e2
3 changed files with 45 additions and 19 deletions

View File

@ -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 {

View File

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

View File

@ -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":