-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from warden-protocol/configfile-support
feat: add config file support
- Loading branch information
Showing
7 changed files
with
204 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,23 @@ | ||
# Discord Faucet | ||
|
||
Another Discord Faucet for Cosmos ecosystems, specifically Warden | ||
Another Discord Faucet for Cosmos ecosystems, specifically Warden but can work on | ||
others. | ||
|
||
## Config | ||
|
||
| ENV | Type | Default | | ||
| -------------- | ------ | --------------------------------------------- | | ||
| PORT | string | 8081 | | ||
| ENV_FILE | string | | | ||
| TOKEN | string | | | ||
| PURGE_INTERVAL | string | 10s | | ||
| MNEMONIC | string | | | ||
| NODE | string | https://rpc.buenavista.wardenprotocol.org:443 | | ||
| CHAIN_ID | string | | | ||
| CLI_NAME | string | wardend | | ||
| ACCOUNT_NAME | string | faucet | | ||
| DENOM | string | uward | | ||
| AMOUNT | string | 10000000 | | ||
| FEES | string | 25uward | | ||
| TX_RETRY | int | 10 | | ||
| COOLDOWN | string | 10s | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/caarlos0/env/v10" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var errConfig = errors.New("config error") | ||
|
||
func configError(msg string) error { | ||
return fmt.Errorf("%w: %s", errConfig, msg) | ||
} | ||
|
||
type Config struct { | ||
Port string `env:"PORT" envDefault:"8081" mapstructure:"PORT"` | ||
EnvFile string `env:"ENV_FILE" envDefault:""` | ||
Token string `env:"TOKEN" envDefault:"" mapstructure:"TOKEN"` | ||
PurgeInterval string `env:"PURGE_INTERVAL" envDefault:"10s" mapstructure:"PURGE_INTERVAL"` | ||
Mnemonic string `env:"MNEMONIC" envDefault:"" mapstructure:"MNEMONIC"` | ||
Node string `env:"NODE" envDefault:"https://rpc.buenavista.wardenprotocol.org:443" mapstructure:"NODE"` | ||
ChainID string `env:"CHAIN_ID" envDefault:"buenavista-1" mapstructure:"CHAIN_ID"` | ||
CliName string `env:"CLI_NAME" envDefault:"wardend" mapstructure:"CLI_NAME"` | ||
AccountName string `env:"ACCOUNT_NAME" envDefault:"faucet" mapstructure:"ACCOUNT_NAME"` | ||
Denom string `env:"DENOM" envDefault:"uward" mapstructure:"DENOM"` | ||
Amount string `env:"AMOUNT" envDefault:"10000000" mapstructure:"AMOUNT"` | ||
Fees string `env:"FEES" envDefault:"25uward" mapstructure:"FEES"` | ||
TXRetry int `env:"TX_RETRY" envDefault:"10" mapstructure:"TX_RETRY"` | ||
CoolDown string `env:"COOLDOWN" envDefault:"10s" mapstructure:"COOLDOWN"` | ||
} | ||
|
||
func LoadConfig() (Config, error) { | ||
cfg := Config{} | ||
var err error | ||
|
||
// setDefaults(*cfg) | ||
|
||
if err = env.Parse(&cfg); err != nil { | ||
return Config{}, configError(err.Error()) | ||
} | ||
|
||
if cfg.EnvFile != "" { | ||
if err = loadConfigFile(&cfg); err != nil { | ||
return Config{}, configError(err.Error()) | ||
} | ||
} | ||
return cfg, nil | ||
} | ||
|
||
func loadConfigFile(cfg *Config) error { | ||
var err error | ||
|
||
// parse config file params | ||
// Extract the directory | ||
dir := filepath.Dir(cfg.EnvFile) + "/" | ||
|
||
// Extract the base name (filename without directory) | ||
base := filepath.Base(cfg.EnvFile) | ||
|
||
// Split the base name into name and extension | ||
name := strings.TrimSuffix(base, filepath.Ext(base)) | ||
ext := strings.TrimPrefix(filepath.Ext(base), ".") | ||
|
||
viper.AddConfigPath(dir) | ||
viper.SetConfigName(name) | ||
viper.SetConfigType(ext) | ||
|
||
viper.AutomaticEnv() | ||
err = viper.ReadInConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err = viper.Unmarshal(&cfg); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.