Skip to content

Commit

Permalink
feat: import config from file or env
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed May 4, 2022
1 parent 3a18495 commit beafb9e
Show file tree
Hide file tree
Showing 3 changed files with 547 additions and 9 deletions.
71 changes: 63 additions & 8 deletions cmd/send.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,73 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"okp4/cosmos-faucet/util"
)

// SendCommand returns a CLI command to interactively send amount to given address.
var sendCmd = &cobra.Command{
Use: "send",
Short: "Send token to a given address",
RunE: func(cmd *cobra.Command, args []string) error {
return nil
},
const (
defaultConfigFilename = "config"
envPrefix = "FAUCET"
)

// NewSendCommand returns a CLI command to interactively send amount token(s) to given address.
func NewSendCommand() *cobra.Command {
var config util.Config

sendCmd := &cobra.Command{
Use: "send <address>",
Short: "Send tokens to a given address",
Args: cobra.ExactArgs(1),
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return initializeConfig(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
return nil
},
}

sendCmd.Flags().StringVar(&config.Mnemonic, "mnemonic", "", "")
sendCmd.Flags().StringVar(&config.ChainId, "chain-id", "okp4", "The network chain ID")
sendCmd.Flags().StringVar(&config.Url, "url", "127.0.0.1", "The grpc server url")
sendCmd.Flags().IntVar(&config.Port, "port", 9090, "The grpc server port")
sendCmd.Flags().StringVar(&config.Denom, "denom", "know", "Token denom")
sendCmd.Flags().StringVar(&config.Prefix, "prefix", "okp4", "Address prefix")
sendCmd.Flags().Int64Var(&config.FeeAmount, "fee-amount", 1000, "Fee amount") // TODO: Determine the default value
sendCmd.Flags().Int64Var(&config.AmountSend, "amount-send", 1, "Amount send value")
sendCmd.Flags().StringVar(&config.Memo, "memo", "Sent by økp4 faucet", "The memo description")
sendCmd.Flags().Int64Var(&config.GasLimit, "gas-limit", 200000, "Gas limit")

return sendCmd
}

func init() {
rootCmd.AddCommand(sendCmd)
rootCmd.AddCommand(NewSendCommand())
}

func initializeConfig(cmd *cobra.Command) error {
v := viper.New()

v.SetConfigName(defaultConfigFilename)
v.AddConfigPath(".")

if err := v.ReadInConfig(); err != nil {
// It's okay if there isn't a config file
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
return err
}
}
v.SetEnvPrefix(envPrefix)
v.AutomaticEnv()

cmd.Flags().VisitAll(func(f *pflag.Flag) {
if !f.Changed && v.IsSet(f.Name) {
val := v.Get(f.Name)
cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val))
}
})

return nil
}
15 changes: 15 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,25 @@ go 1.18

require (
github.com/spf13/cobra v1.4.0
github.com/spf13/viper v1.11.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pelletier/go-toml/v2 v2.0.0-beta.8 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
Loading

0 comments on commit beafb9e

Please sign in to comment.