Skip to content

Commit

Permalink
Improve migrate output and add verbose flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Toby Padilla authored and toby committed Feb 18, 2022
1 parent 43c8a10 commit 989596b
Showing 1 changed file with 104 additions and 52 deletions.
156 changes: 104 additions & 52 deletions cmd/migrate_account.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"fmt"
"log"

"github.com/charmbracelet/charm/client"
Expand All @@ -10,91 +11,142 @@ import (

// MigrateAccountCmd is a command to convert your legacy RSA SSH keys to the
// new Ed25519 standard keys
var MigrateAccountCmd = &cobra.Command{
Use: "migrate-account",
Hidden: true,
Short: "",
Long: "",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
rcfg, err := client.ConfigFromEnv()
if err != nil {
return err
}
rcfg.KeyType = "rsa"
rsaClient, err := client.NewClient(rcfg)
if err != nil {
return err
}

ecfg, err := client.ConfigFromEnv()
if err != nil {
return err
}
ecfg.KeyType = "ed25519"
ed25519Client, err := client.NewClient(ecfg)
if err != nil {
return err
}
lh := &linkHandler{linkChan: make(chan string)}
go func() {
rsaClient.LinkGen(lh)
}()
tok := <-lh.linkChan
ed25519Client.Link(lh, tok)
err = rsaClient.SyncEncryptKeys()
if err != nil {
return err
}
err = ed25519Client.SyncEncryptKeys()
if err != nil {
}
return nil
},
}
var (
verbose bool
linkError bool

MigrateAccountCmd = &cobra.Command{
Use: "migrate-account",
Hidden: true,
Short: "",
Long: "",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("Migrating account...")
rcfg, err := client.ConfigFromEnv()
if err != nil {
return err
}
rcfg.KeyType = "rsa"
rsaClient, err := client.NewClient(rcfg)
if err != nil {
return err
}

ecfg, err := client.ConfigFromEnv()
if err != nil {
return err
}
ecfg.KeyType = "ed25519"
ed25519Client, err := client.NewClient(ecfg)
if err != nil {
return err
}

lc := make(chan string)
go func() {
lh := &linkHandler{desc: "link-gen", linkChan: lc}
rsaClient.LinkGen(lh)
}()
tok := <-lc
lh := &linkHandler{desc: "link-request", linkChan: lc}
ed25519Client.Link(lh, tok)
if verbose {
log.Printf("link-gen sync encrypt keys")
}
err = rsaClient.SyncEncryptKeys()
if err != nil {
if verbose {
log.Printf("link-gen sync encrypt keys failed")
} else {
printError()
}
return err
}
if verbose {
log.Printf("link-request sync encrypt keys")
}
err = ed25519Client.SyncEncryptKeys()
if err != nil {
if verbose {
log.Printf("link-request sync encrypt keys failed")
} else {
printError()
}
return err
}
if !linkError {
fmt.Println("Account migrated! You're good to go.")
} else {
printError()
}
return nil
},
}
)

type linkHandler struct {
desc string
linkChan chan string
}

func (lh *linkHandler) TokenCreated(l *proto.Link) {
// log.Printf("token created %v", l)
lh.printDebug("token created", l)
lh.linkChan <- string(l.Token)
lh.printDebug("token created sent to chan", l)
}

func (lh *linkHandler) TokenSent(l *proto.Link) {
// log.Printf("token sent %v", l)
lh.printDebug("token sent", l)
}

func (lh *linkHandler) ValidToken(l *proto.Link) {
// log.Printf("valid token %v", l)
lh.printDebug("valid token", l)
}

func (lh *linkHandler) InvalidToken(l *proto.Link) {
// log.Printf("invalid token %v", l)
lh.printDebug("invalid token", l)
}

func (lh *linkHandler) Request(l *proto.Link) bool {
// log.Printf("request %v", l)
lh.printDebug("request", l)
return true
}

func (lh *linkHandler) RequestDenied(l *proto.Link) {
// log.Printf("request denied %v", l)
lh.printDebug("request denied", l)
}

func (lh *linkHandler) SameUser(l *proto.Link) {
log.Println("Success! Looks like you've done this already.")
lh.printDebug("same user", l)
}

func (lh *linkHandler) Success(l *proto.Link) {
log.Println("Success! You're good to go.")
lh.printDebug("success", l)
}

func (lh *linkHandler) Timeout(l *proto.Link) {
log.Println("Timeout")
lh.printDebug("timeout", l)
}

func (lh linkHandler) Error(l *proto.Link) {
log.Printf("Error %v", l)
linkError = true
lh.printDebug("error", l)
if !verbose {
printError()
}
}

func (lh *linkHandler) printDebug(msg string, l *proto.Link) {
if verbose {
log.Printf("%s %s:\t%v\n", lh.desc, msg, l)
}
}

func printError() {
fmt.Println("\nThere was an error migrating your account. Please re-run with the -v argument `charm migrate-account -v` and join our slack at https://charm.sh/slack to help debug the issue. Sorry about that, we'll try to figure it out!")
}

func init() {
MigrateAccountCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "print debug output")
}

0 comments on commit 989596b

Please sign in to comment.