[#161] adm: Refactor init context creation
Resolve funlen linter for newInitializeContext function Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
parent
0e13be174d
commit
502cd8608a
1 changed files with 76 additions and 37 deletions
|
@ -108,7 +108,6 @@ func (c *initializeContext) close() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// nolint: funlen
|
|
||||||
func newInitializeContext(cmd *cobra.Command, v *viper.Viper) (*initializeContext, error) {
|
func newInitializeContext(cmd *cobra.Command, v *viper.Viper) (*initializeContext, error) {
|
||||||
walletDir := config.ResolveHomePath(viper.GetString(alphabetWalletsFlag))
|
walletDir := config.ResolveHomePath(viper.GetString(alphabetWalletsFlag))
|
||||||
wallets, err := openAlphabetWallets(v, walletDir)
|
wallets, err := openAlphabetWallets(v, walletDir)
|
||||||
|
@ -119,24 +118,14 @@ func newInitializeContext(cmd *cobra.Command, v *viper.Viper) (*initializeContex
|
||||||
needContracts := cmd.Name() == "update-contracts" || cmd.Name() == "init"
|
needContracts := cmd.Name() == "update-contracts" || cmd.Name() == "init"
|
||||||
|
|
||||||
var w *wallet.Wallet
|
var w *wallet.Wallet
|
||||||
if needContracts {
|
w, err = getWallet(cmd, v, needContracts, walletDir)
|
||||||
w, err = openContractWallet(v, cmd, walletDir)
|
if err != nil {
|
||||||
if err != nil {
|
return nil, err
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var c Client
|
c, err := createClient(cmd, v, wallets)
|
||||||
if v.GetString(localDumpFlag) != "" {
|
|
||||||
if v.GetString(endpointFlag) != "" {
|
|
||||||
return nil, fmt.Errorf("`%s` and `%s` flags are mutually exclusive", endpointFlag, localDumpFlag)
|
|
||||||
}
|
|
||||||
c, err = newLocalClient(cmd, v, wallets)
|
|
||||||
} else {
|
|
||||||
c, err = getN3Client(v)
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("can't create N3 client: %w", err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
committeeAcc, err := getWalletAccount(wallets[0], committeeAccountName)
|
committeeAcc, err := getWalletAccount(wallets[0], committeeAccountName)
|
||||||
|
@ -149,35 +138,22 @@ func newInitializeContext(cmd *cobra.Command, v *viper.Viper) (*initializeContex
|
||||||
return nil, fmt.Errorf("can't find consensus account: %w", err)
|
return nil, fmt.Errorf("can't find consensus account: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var ctrPath string
|
if err := validateInit(cmd); err != nil {
|
||||||
if cmd.Name() == "init" {
|
return nil, err
|
||||||
if viper.GetInt64(epochDurationInitFlag) <= 0 {
|
|
||||||
return nil, fmt.Errorf("epoch duration must be positive")
|
|
||||||
}
|
|
||||||
|
|
||||||
if viper.GetInt64(maxObjectSizeInitFlag) <= 0 {
|
|
||||||
return nil, fmt.Errorf("max object size must be positive")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if needContracts {
|
ctrPath, err := getContractsPath(cmd, v, needContracts)
|
||||||
ctrPath, err = cmd.Flags().GetString(contractsInitFlag)
|
if err != nil {
|
||||||
if err != nil {
|
return nil, err
|
||||||
return nil, fmt.Errorf("invalid contracts path: %w", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := checkNotaryEnabled(c); err != nil {
|
if err := checkNotaryEnabled(c); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
accounts := make([]*wallet.Account, len(wallets))
|
accounts, err := createWalletAccounts(wallets)
|
||||||
for i, w := range wallets {
|
if err != nil {
|
||||||
acc, err := getWalletAccount(w, singleAccountName)
|
return nil, err
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("wallet %s is invalid (no single account): %w", w.Path(), err)
|
|
||||||
}
|
|
||||||
accounts[i] = acc
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cliCtx, err := defaultClientContext(c, committeeAcc)
|
cliCtx, err := defaultClientContext(c, committeeAcc)
|
||||||
|
@ -207,6 +183,69 @@ func newInitializeContext(cmd *cobra.Command, v *viper.Viper) (*initializeContex
|
||||||
return initCtx, nil
|
return initCtx, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func validateInit(cmd *cobra.Command) error {
|
||||||
|
if cmd.Name() != "init" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if viper.GetInt64(epochDurationInitFlag) <= 0 {
|
||||||
|
return fmt.Errorf("epoch duration must be positive")
|
||||||
|
}
|
||||||
|
|
||||||
|
if viper.GetInt64(maxObjectSizeInitFlag) <= 0 {
|
||||||
|
return fmt.Errorf("max object size must be positive")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func createClient(cmd *cobra.Command, v *viper.Viper, wallets []*wallet.Wallet) (Client, error) {
|
||||||
|
var c Client
|
||||||
|
var err error
|
||||||
|
if v.GetString(localDumpFlag) != "" {
|
||||||
|
if v.GetString(endpointFlag) != "" {
|
||||||
|
return nil, fmt.Errorf("`%s` and `%s` flags are mutually exclusive", endpointFlag, localDumpFlag)
|
||||||
|
}
|
||||||
|
c, err = newLocalClient(cmd, v, wallets)
|
||||||
|
} else {
|
||||||
|
c, err = getN3Client(v)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("can't create N3 client: %w", err)
|
||||||
|
}
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getWallet(cmd *cobra.Command, v *viper.Viper, needContracts bool, walletDir string) (*wallet.Wallet, error) {
|
||||||
|
if !needContracts {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return openContractWallet(v, cmd, walletDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getContractsPath(cmd *cobra.Command, v *viper.Viper, needContracts bool) (string, error) {
|
||||||
|
if !needContracts {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ctrPath, err := cmd.Flags().GetString(contractsInitFlag)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("invalid contracts path: %w", err)
|
||||||
|
}
|
||||||
|
return ctrPath, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func createWalletAccounts(wallets []*wallet.Wallet) ([]*wallet.Account, error) {
|
||||||
|
accounts := make([]*wallet.Account, len(wallets))
|
||||||
|
for i, w := range wallets {
|
||||||
|
acc, err := getWalletAccount(w, singleAccountName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("wallet %s is invalid (no single account): %w", w.Path(), err)
|
||||||
|
}
|
||||||
|
accounts[i] = acc
|
||||||
|
}
|
||||||
|
return accounts, nil
|
||||||
|
}
|
||||||
|
|
||||||
func openAlphabetWallets(v *viper.Viper, walletDir string) ([]*wallet.Wallet, error) {
|
func openAlphabetWallets(v *viper.Viper, walletDir string) ([]*wallet.Wallet, error) {
|
||||||
walletFiles, err := os.ReadDir(walletDir)
|
walletFiles, err := os.ReadDir(walletDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue