Skip to content

Commit

Permalink
move wallet and mint create directories
Browse files Browse the repository at this point in the history
  • Loading branch information
elnosh committed Oct 28, 2024
1 parent d80c23c commit 2a875c2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 29 deletions.
11 changes: 11 additions & 0 deletions cmd/mint/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
"syscall"
Expand Down Expand Up @@ -40,6 +41,16 @@ func configFromEnv() (*mint.Config, error) {
port = "3338"
}

mintPath := os.Getenv("MINT_DB_PATH")
// if MINT_DB_PATH is empty, use $HOME/.gonuts/mint
if len(mintPath) == 0 {
homedir, err := os.UserHomeDir()
if err != nil {
return nil, err
}
mintPath = filepath.Join(homedir, ".gonuts", "mint")
}

mintLimits := mint.MintLimits{}
if maxBalanceEnv, ok := os.LookupEnv("MAX_BALANCE"); ok {
maxBalance, err := strconv.ParseUint(maxBalanceEnv, 10, 64)
Expand Down
10 changes: 1 addition & 9 deletions cmd/nutw/nutw.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"log"
"net/url"
"os"
Expand Down Expand Up @@ -41,16 +40,9 @@ func walletConfig() (wallet.Config, error) {
}

walletPath := os.Getenv("WALLET_PATH")
if len(walletPath) > 0 {
if !fs.ValidPath(walletPath) {
return wallet.Config{}, fmt.Errorf("invalid WALLET_PATH")
}
} else {
if len(walletPath) == 0 {
walletPath = defaultWalletPath()
}
if err := os.MkdirAll(walletPath, 0700); err != nil {
return wallet.Config{}, fmt.Errorf("could not create wallet directory: %v", err)
}

mint := os.Getenv("MINT_URL")
if len(mint) == 0 {
Expand Down
21 changes: 2 additions & 19 deletions mint/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"errors"
"fmt"
"io"
"log"
"log/slog"
"os"
"path/filepath"
Expand Down Expand Up @@ -62,8 +61,8 @@ type Mint struct {

func LoadMint(config Config) (*Mint, error) {
path := config.MintPath
if len(path) == 0 {
path = mintPath()
if err := os.MkdirAll(path, 0700); err != nil {
return nil, err
}

logger, err := setupLogger(path, config.LogLevel)
Expand Down Expand Up @@ -181,22 +180,6 @@ func LoadMint(config Config) (*Mint, error) {
return mint, nil
}

// mintPath returns the mint's path
// at $HOME/.gonuts/mint
func mintPath() string {
homedir, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}

path := filepath.Join(homedir, ".gonuts", "mint")
err = os.MkdirAll(path, 0700)
if err != nil {
log.Fatal(err)
}
return path
}

func setupLogger(mintPath string, logLevel LogLevel) (*slog.Logger, error) {
replacer := func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.SourceKey {
Expand Down
7 changes: 6 additions & 1 deletion wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ func InitStorage(path string) (storage.WalletDB, error) {
}

func LoadWallet(config Config) (*Wallet, error) {
db, err := InitStorage(config.WalletPath)
path := config.WalletPath
if err := os.MkdirAll(path, 0700); err != nil {
return nil, err
}

db, err := InitStorage(path)
if err != nil {
return nil, fmt.Errorf("InitStorage: %v", err)
}
Expand Down

0 comments on commit 2a875c2

Please sign in to comment.