Browse Source

Add update cli command, cleanup

pull/1782/head
Ethan Frey 8 years ago
parent
commit
9c427e95e2
3 changed files with 45 additions and 19 deletions
  1. +2
    -12
      cmd/new.go
  2. +27
    -7
      cmd/update.go
  3. +16
    -0
      cmd/utils.go

+ 2
- 12
cmd/new.go View File

@ -36,26 +36,16 @@ func init() {
func newPassword(cmd *cobra.Command, args []string) { func newPassword(cmd *cobra.Command, args []string) {
if len(args) != 1 || len(args[0]) == 0 { 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 return
} }
name := args[0] name := args[0]
// TODO: own function???
pass, err := getPassword("Enter a passphrase:")
pass, err := getCheckPassword("Enter a passphrase:", "Repeat the passphrase:")
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
return 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) info, err := manager.Create(name, pass)
if err != nil { if err != nil {


+ 27
- 7
cmd/update.go View File

@ -18,7 +18,6 @@ import (
"fmt" "fmt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
) )
// updateCmd represents the update command // updateCmd represents the update command
@ -26,14 +25,35 @@ var updateCmd = &cobra.Command{
Use: "update", Use: "update",
Short: "Change the password for a private key", Short: "Change the password for a private key",
Long: `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() { func init() {
RootCmd.AddCommand(updateCmd) 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!")
}
} }

+ 16
- 0
cmd/utils.go View File

@ -22,6 +22,22 @@ func getPassword(prompt string) (string, error) {
return pass, nil 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) { func printInfo(info keys.Info) {
switch output { switch output {
case "text": case "text":


Loading…
Cancel
Save