From 832a13d98eabd8421a29b43a22427d76b8f648e0 Mon Sep 17 00:00:00 2001 From: elnosh Date: Tue, 3 Sep 2024 13:02:56 -0500 Subject: [PATCH] wallet - command to see and change default mint --- cmd/nutw/nutw.go | 86 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 74 insertions(+), 12 deletions(-) diff --git a/cmd/nutw/nutw.go b/cmd/nutw/nutw.go index af5f2eb..d998c93 100644 --- a/cmd/nutw/nutw.go +++ b/cmd/nutw/nutw.go @@ -29,19 +29,10 @@ func walletConfig() wallet.Config { path := setWalletPath() // default config config := wallet.Config{WalletPath: path, CurrentMintURL: "http://127.0.0.1:3338"} + env := envPath(path) - envPath := filepath.Join(path, ".env") - if _, err := os.Stat(envPath); err != nil { - wd, err := os.Getwd() - if err != nil { - envPath = "" - } else { - envPath = filepath.Join(wd, ".env") - } - } - - if len(envPath) > 0 { - err := godotenv.Load(envPath) + if len(env) > 0 { + err := godotenv.Load(env) if err == nil { config.CurrentMintURL = getMintURL() } @@ -64,6 +55,19 @@ func setWalletPath() string { return path } +func envPath(walletPath string) string { + envPath := filepath.Join(walletPath, ".env") + if _, err := os.Stat(envPath); err != nil { + wd, err := os.Getwd() + if err != nil { + envPath = "" + } else { + envPath = filepath.Join(wd, ".env") + } + } + return envPath +} + func getMintURL() string { mintUrl := os.Getenv("MINT_URL") if len(mintUrl) > 0 { @@ -108,6 +112,7 @@ func main() { p2pkLockCmd, mnemonicCmd, restoreCmd, + currentMintCmd, decodeCmd, }, } @@ -430,6 +435,63 @@ func restore(ctx *cli.Context) error { return nil } +var currentMintCmd = &cli.Command{ + Name: "currentmint", + Usage: "See and change default mint", + Subcommands: []*cli.Command{ + { + Name: "set", + Usage: "Change the current mint", + ArgsUsage: "[MINT URL]", + Action: setCurrentMint, + }, + }, + Action: currentMint, +} + +func currentMint(ctx *cli.Context) error { + config := walletConfig() + fmt.Printf("current mint: %v\n", config.CurrentMintURL) + return nil +} + +func setCurrentMint(ctx *cli.Context) error { + args := ctx.Args() + if args.Len() < 1 { + printErr(errors.New("specify new mint url to set as default")) + } + mintURL := args.First() + _, err := url.ParseRequestURI(mintURL) + if err != nil { + printErr(fmt.Errorf("invalid mint url: %v", err)) + } + + path := setWalletPath() + envFilePath := envPath(path) + + envFileData, err := os.ReadFile(envFilePath) + if err != nil { + printErr(fmt.Errorf("could not read .env file: %v", err)) + } + + lines := strings.Split(string(envFileData), "\n") + for i, line := range lines { + if strings.HasPrefix(strings.TrimSpace(line), "MINT_URL=") { + // replace line in file setting mint url + lines[i] = fmt.Sprintf("MINT_URL=%s", mintURL) + } + } + changed := strings.Join(lines, "\n") + + if err := os.WriteFile(envFilePath, []byte(changed), 0644); err != nil { + printErr(err) + } + + fmt.Println("updated mint successfully") + + return nil +} + var decodeCmd = &cli.Command{ Name: "decode", ArgsUsage: "[TOKEN]",