Skip to content

Commit

Permalink
feat(wsctl): wsctl config add project chain endpoint & project contra…
Browse files Browse the repository at this point in the history
…ct address
  • Loading branch information
huangzhiran committed Nov 13, 2023
1 parent 5c3b9cc commit 1255e68
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
28 changes: 26 additions & 2 deletions cmd/wsctl/cmd/config_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"github.com/asaskevich/govalidator"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -13,11 +14,20 @@ var setCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
host, err := cmd.Flags().GetString(NodeHost)
if err != nil {
return errors.Wrap(err, "get flag nodeHost failed")
return errors.Wrap(err, "get flag node-host failed")
}
port, err := cmd.Flags().GetString(NodePort)
if err != nil {
return errors.Wrap(err, "get flag nodePort failed")
return errors.Wrap(err, "get flag node-port failed")
}

chainEndpoint, err := cmd.Flags().GetString(ChainEndpoint)
if err != nil {
return errors.Wrap(err, "get flag chain-endpoint failed")
}
contractAddress, err := cmd.Flags().GetString(ContractAddress)
if err != nil {
return errors.Wrap(err, "get flag contract-address failed")
}

if host != "" && !govalidator.IsHost(host) {
Expand All @@ -26,13 +36,25 @@ var setCmd = &cobra.Command{
if port != "" && !govalidator.IsPort(port) {
return errors.New("invalid host port")
}
if chainEndpoint != "" && !govalidator.IsURL(chainEndpoint) {
return errors.New("invalid endpoint format")
}
if contractAddress != "" && !common.IsHexAddress(contractAddress) {
return errors.New("invalid contract address format")
}

if host != "" {
viper.Set(NodeHost, host)
}
if port != "" {
viper.Set(NodePort, port)
}
if chainEndpoint != "" {
viper.Set(ChainEndpoint, chainEndpoint)
}
if contractAddress != "" {
viper.Set(ContractAddress, contractAddress)
}

if err := viper.WriteConfig(); err != nil {
return errors.Wrap(err, "failed to write config file")
Expand All @@ -47,4 +69,6 @@ func init() {

setCmd.Flags().StringP(NodeHost, "n", "", "node host")
setCmd.Flags().StringP(NodePort, "p", "", "node port")
setCmd.Flags().StringP(ChainEndpoint, "c", "", "chain endpoint")
setCmd.Flags().StringP(ContractAddress, "a", "", "contract address")
}
6 changes: 4 additions & 2 deletions cmd/wsctl/cmd/enum.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

const (
NodeHost = "node-host"
NodePort = "node-port"
NodeHost = "node-host"
NodePort = "node-port"
ChainEndpoint = "chain-endpoint"
ContractAddress = "contract-address"
)
2 changes: 2 additions & 0 deletions cmd/wsctl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func initConfig() {
viper.SetConfigFile(path.Join(confDir, "config.yaml"))
viper.SetDefault(NodeHost, "localhost")
viper.SetDefault(NodePort, "9000")
viper.SetDefault(ChainEndpoint, "https://babel-api.testnet.iotex.io")
viper.SetDefault(ContractAddress, "0x190Cc9af23504ac5Dc461376C1e2319bc3B9cD29")

if err := viper.ReadInConfig(); err != nil {
if strings.Contains(err.Error(), "no such file or directory") {
Expand Down

0 comments on commit 1255e68

Please sign in to comment.