Skip to content

Commit

Permalink
feat: refactor runtime struct
Browse files Browse the repository at this point in the history
  • Loading branch information
SpontaneousOverthrow committed Nov 4, 2023
1 parent cb75364 commit 2bd6e84
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 91 deletions.
45 changes: 5 additions & 40 deletions cmd/convert/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,62 +14,27 @@ func newConvertRuntime() (*ConvertRuntime, error) {
cr := &utils.ConvertRuntime{}
var err error

utils.LogConvert.Debug().Msg("validating input wallet")
utils.LogConvert.Debug().Msg("getting input wallet paths")
var inputW utils.W
err = viper.UnmarshalKey("input", &inputW)
if err != nil {
return nil, err
}

err = inputW.Validate()
if err != nil {
return nil, err
}

utils.LogConvert.Debug().Msg("validating output field")
utils.LogConvert.Debug().Msg("getting output wallet paths")
var outputW utils.W
err = viper.UnmarshalKey("output", &outputW)
if err != nil {
return nil, err
}

err = outputW.Validate()
if err != nil {
return nil, err
}

cr.ctx = context.Background()
cr.dWalletsPath = dWalletConfig.Path
cr.ndWalletsPath = ndWalletConfig.Path
utils.LogCombine.Debug().Msgf("getting input passwords form file %s", dWalletConfig.Passphrases)
cr.passphrasesIn, err = utils.GetAccountsPasswords(dWalletConfig.Passphrases)
if err != nil {
return nil, err
}
utils.LogCombine.Debug().Msgf("getting output passwords form file %s", ndWalletConfig.Passphrases)
cr.passphrasesOut, err = utils.GetAccountsPasswords(ndWalletConfig.Passphrases)
if err != nil {
return nil, err
}
cr.accountDatas = make(map[string]AccountExtends)
utils.LogCombine.Debug().Msgf("loading stores form %s", cr.dWalletsPath)
cr.stores, err = utils.LoadStores(cr.ctx, cr.dWalletsPath, cr.passphrasesIn)
if err != nil {
return nil, err
}

cr.peers = dWalletConfig.Peers
cr.InputW = inputW
cr.OutputW = outputW
cr.Ctx = context.Background()

return cr, nil
}

func (cr *utils.ConvertRuntime) validate() error {
if cr.dWalletsPath == cr.ndWalletsPath {
return utils.ErrorSameDirs
}
return nil
}

func (cr *CombineRuntime) createWalletAndStore() error {
var err error
utils.LogCombine.Debug().Msgf("creating store %s", cr.ndWalletsPath)
Expand Down
12 changes: 7 additions & 5 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ input:
type: distributed
path: DISTRIBUTED_WALLETS
passphrases: ./passphrases.txt
threshold: 2
peers:
10: old1:9091
20: old2:9091
30: old3:9091
opts:
threshold: 2
peers:
10: old1:9091
20: old2:9091
30: old3:9091
output:
type: nd
path: ND_WALLETS
passphrases: ./passphrases.txt
log-level: debug

55 changes: 55 additions & 0 deletions utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"bytes"
"context"
"os"

"github.com/pkg/errors"
Expand All @@ -20,6 +21,60 @@ type DWalletConfig struct {
WalletName string
}

type W struct {
Path string
Type string
Passphrases string
}

type ConvertRuntime struct {
Ctx context.Context
InputW W
OutputW W
}

func (W *W) wValidate() error {
if W.Path == "" {
return ErrorPathField
}

if W.Type == "" {
return ErrorEmptyType

Check failure on line 42 in utils/config.go

View workflow job for this annotation

GitHub Actions / go-tests

undefined: ErrorEmptyType
}

if len(W.Passphrases) == 0 {
return ErrorPassphraseEmpty
}

return nil

}

func (CR *ConvertRuntime) Validate() error {
LogConvert.Debug().Msg("validating input wallet")

Check failure on line 54 in utils/config.go

View workflow job for this annotation

GitHub Actions / go-tests

undefined: LogConvert
CR.InputW.wValidate()
if CR.InputW.Passphrases == "" {
return ErrorInputPassphrasesIsEmpty

Check failure on line 57 in utils/config.go

View workflow job for this annotation

GitHub Actions / go-tests

undefined: ErrorInputPassphrasesIsEmpty
}

LogConvert.Debug().Msg("validating output wallet")

Check failure on line 60 in utils/config.go

View workflow job for this annotation

GitHub Actions / go-tests

undefined: LogConvert
CR.OutputW.wValidate()
if CR.OutputW.Passphrases == "" {
return ErrorOutputPassphrasesIsEmpty

Check failure on line 63 in utils/config.go

View workflow job for this annotation

GitHub Actions / go-tests

undefined: ErrorOutputPassphrasesIsEmpty
}

LogConvert.Debug().Msg("validating types for both wallets")

Check failure on line 66 in utils/config.go

View workflow job for this annotation

GitHub Actions / go-tests

undefined: LogConvert
if CR.InputW.Type != CR.OutputW.Type {
return ErrorSameWalletType

Check failure on line 68 in utils/config.go

View workflow job for this annotation

GitHub Actions / go-tests

undefined: ErrorSameWalletType
}

LogConvert.Debug().Msg("validating dirs for both wallets")

Check failure on line 71 in utils/config.go

View workflow job for this annotation

GitHub Actions / go-tests

undefined: LogConvert
if CR.OutputW.Path != CR.OutputW.Path {
return ErrorSameDirs
}

return nil
}
func GetAccountsPasswords(path string) ([][]byte, error) {

content, err := os.ReadFile(path)
Expand Down
46 changes: 0 additions & 46 deletions utils/structs.go

This file was deleted.

0 comments on commit 2bd6e84

Please sign in to comment.