2022-12-16 09:48:38 +00:00
|
|
|
// Copyright 2021 Evmos Foundation
|
|
|
|
// This file is part of Evmos' Ethermint library.
|
|
|
|
//
|
|
|
|
// The Ethermint library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The Ethermint library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE
|
2020-08-04 18:52:32 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
|
|
|
"github.com/tendermint/tendermint/libs/cli"
|
2020-09-24 17:50:47 +00:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
|
|
|
|
2022-09-07 06:36:11 +00:00
|
|
|
ethermint "github.com/cerc-io/laconicd/types"
|
2020-08-04 18:52:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// InitConfig adds the chain-id, encoding and output flags to the persistent flag set.
|
|
|
|
func InitConfig(cmd *cobra.Command) error {
|
|
|
|
home, err := cmd.PersistentFlags().GetString(cli.HomeFlag)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
configFile := path.Join(home, "config", "config.toml")
|
2021-09-09 11:11:40 +00:00
|
|
|
_, err = os.Stat(configFile)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
// Immediately return if the error isn't related to the file not existing.
|
2022-09-07 06:36:11 +00:00
|
|
|
// See issue https://github.com/cerc-io/laconicd/issues/539
|
2021-09-09 11:11:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err == nil {
|
2020-08-04 18:52:32 +00:00
|
|
|
viper.SetConfigFile(configFile)
|
|
|
|
|
|
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := viper.BindPFlag(flags.FlagChainID, cmd.PersistentFlags().Lookup(flags.FlagChainID)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := viper.BindPFlag(cli.EncodingFlag, cmd.PersistentFlags().Lookup(cli.EncodingFlag)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return viper.BindPFlag(cli.OutputFlag, cmd.PersistentFlags().Lookup(cli.OutputFlag))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateChainID wraps a cobra command with a RunE function with base 10 integer chain-id verification.
|
|
|
|
func ValidateChainID(baseCmd *cobra.Command) *cobra.Command {
|
|
|
|
// Copy base run command to be used after chain verification
|
|
|
|
baseRunE := baseCmd.RunE
|
|
|
|
|
|
|
|
// Function to replace command's RunE function
|
|
|
|
validateFn := func(cmd *cobra.Command, args []string) error {
|
2021-04-17 10:00:07 +00:00
|
|
|
chainID, _ := cmd.Flags().GetString(flags.FlagChainID)
|
2020-08-04 18:52:32 +00:00
|
|
|
|
2020-09-24 17:50:47 +00:00
|
|
|
if !ethermint.IsValidChainID(chainID) {
|
|
|
|
return fmt.Errorf("invalid chain-id format: %s", chainID)
|
2020-08-04 18:52:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return baseRunE(cmd, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
baseCmd.RunE = validateFn
|
|
|
|
return baseCmd
|
|
|
|
}
|