Skip to content

Commit

Permalink
feat: use viper in price-feeder config (#1274)
Browse files Browse the repository at this point in the history
* Use viper in price-feeder config

* update CHANGELOG.md with #1274

* address pr suggestions

* chore: update codeowners (#1275)

* Run go fmt

* fix: use http.Status* codes (#1284)

* Update price-feeder/CHANGELOG.md

Co-authored-by: Adam Wozniak <29418299+adamewozniak@users.noreply.github.com>

* build(deps): Bump github.com/golangci/golangci-lint from 1.48.0 to 1.49.0 (#1277)

Bumps [github.com/golangci/golangci-lint](https://github.com/golangci/golangci-lint) from 1.48.0 to 1.49.0.
<details>
<summary>Release notes</summary>
<p><em>Sourced from <a href="https://github.com/golangci/golangci-lint/releases">github.com/golangci/golangci-lint's releases</a>.</em></p>
<blockquote>
<h2>v1.49.0</h2>
<h2>Changelog</h2>
<ul>
<li>69a49cf6 build(deps): bump github.com/GaijinEntertainment/go-exhaustruct/v2 from 2.2.2 to 2.3.0 (<a href="https://github-redirect.dependabot.com/golangci/golangci-lint/issues/3122">#3122</a>)</li>
<li>9da04f50 build(deps): bump github.com/go-critic/go-critic from 0.6.3 to 0.6.4 (<a href="https://github-redirect.dependabot.com/golangci/golangci-lint/issues/3089">#3089</a>)</li>
<li>1bb23af9 build(deps): bump github.com/mattn/go-colorable from 0.1.12 to 0.1.13 (<a href="https://github-redirect.dependabot.com/golangci/golangci-lint/issues/3123">#3123</a>)</li>
<li>be10ef26 build(deps): bump github.com/mgechev/revive from 1.2.1 to 1.2.2 (<a href="https://github-redirect.dependabot.com/golangci/golangci-lint/issues/3075">#3075</a>)</li>
<li>1ca9affe build(deps): bump github.com/mgechev/revive from 1.2.2 to 1.2.3 (<a href="https://github-redirect.dependabot.com/golangci/golangci-lint/issues/3091">#3091</a>)</li>
<li>f78f6dd3 build(deps): bump github.com/polyfloyd/go-errorlint from 1.0.0 to 1.0.1 (<a href="https://github-redirect.dependabot.com/golangci/golangci-lint/issues/3092">#3092</a>)</li>
<li>edeaa17f build(deps): bump github.com/polyfloyd/go-errorlint from 1.0.1 to 1.0.2 (<a href="https://github-redirect.dependabot.com/golangci/golangci-lint/issues/3097">#3097</a>)</li>
<li>14714050 build(deps): bump github.com/sashamelentyev/usestdlibvars from 1.10.0 to 1.13.0 (<a href="https://github-redirect.dependabot.com/golangci/golangci-lint/issues/3120">#3120</a>)</li>
<li>0e044f25 build(deps): bump github.com/sashamelentyev/usestdlibvars from 1.8.0 to 1.10.0 (<a href="https://github-redirect.dependabot.com/golangci/golangci-lint/issues/3090">#3090</a>)</li>
<li>d57145d4 build(deps): bump github.com/securego/gosec/v2 from 2.12.0 to 2.13.1 (<a href="https://github-redirect.dependabot.com/golangci/golangci-lint/issues/3119">#3119</a>)</li>
<li>a813244b build(deps): bump github.com/shirou/gopsutil/v3 from 3.22.6 to 3.22.7 (<a href="https://github-redirect.dependabot.com/golangci/golangci-lint/issues/3074">#3074</a>)</li>
<li>cc2d97f3 contextcheck: bump to v1.0.6 &amp;&amp; re-enable (<a href="https://github-redirect.dependabot.com/golangci/golangci-lint/issues/3102">#3102</a>)</li>
<li>63b2fe00 feat: add interfacebloat (<a href="https://github-redirect.dependabot.com/golangci/golangci-lint/issues/3024">#3024</a>)</li>
<li>f48530e2 feat: add logrlint (<a href="https://github-redirect.dependabot.com/golangci/golangci-lint/issues/3093">#3093</a>)</li>
<li>58809c3b feat: add reassign linter (<a href="https://github-redirect.dependabot.com/golangci/golangci-lint/issues/3064">#3064</a>)</li>
<li>37d3aa43 feat: deprecate varcheck, deadcode, and structcheck (<a href="https://github-redirect.dependabot.com/golangci/golangci-lint/issues/3125">#3125</a>)</li>
<li>da3b1496 fix: exit early on run --version (<a href="https://github-redirect.dependabot.com/golangci/golangci-lint/issues/3067">#3067</a>)</li>
<li>970b0a5b fix: set an explicit GOROOT in the Docker image for go-critic (<a href="https://github-redirect.dependabot.com/golangci/golangci-lint/issues/3106">#3106</a>)</li>
<li>d4fd2db3 nosnakecase: deprecate linter (<a href="https://github-redirect.dependabot.com/golangci/golangci-lint/issues/3065">#3065</a>)</li>
</ul>
</blockquote>
</details>
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a href="https://github.com/golangci/golangci-lint/blob/master/CHANGELOG.md">github.com/golangci/golangci-lint's changelog</a>.</em></p>
<blockquote>
<h3>v1.49.0</h3>
<p>IMPORTANT: <code>varcheck</code> and <code>deadcode</code> has been removed of default linters.</p>
<ol>
<li>new linters
<ul>
<li><code>interfacebloat</code>: <a href="https://github.com/sashamelentyev/interfacebloat">https://github.com/sashamelentyev/interfacebloat</a></li>
<li><code>logrlint</code>: <a href="https://github.com/timonwong/logrlint">https://github.com/timonwong/logrlint</a></li>
<li><code>reassign</code>: <a href="https://github.com/curioswitch/go-reass">https://github.com/curioswitch/go-reass</a></li>
</ul>
</li>
<li>updated linters
<ul>
<li><code>go-colorable</code>: from 0.1.12 to 0.1.13</li>
<li><code>go-critic</code>: from 0.6.3 to 0.6.4</li>
<li><code>go-errorlint</code>: from 1.0.0 to 1.0.2</li>
<li><code>go-exhaustruct</code>: from 2.2.2 to 2.3.0</li>
<li><code>gopsutil</code>: from 3.22.6 to 3.22.7</li>
<li><code>gosec</code>: from 2.12.0 to 2.13.1</li>
<li><code>revive</code>: from 1.2.1 to 1.2.3</li>
<li><code>usestdlibvars</code>: from 1.8.0 to 1.13.0</li>
<li><code>contextcheck</code>: from v1.0.4 to v1.0.6 &amp;&amp; re-enable</li>
<li><code>nosnakecase</code>: This linter is deprecated.</li>
<li><code>varcheck</code>: This linter is deprecated use <code>unused</code> instead.</li>
<li><code>deadcode</code>: This linter is deprecated use <code>unused</code> instead.</li>
<li><code>structcheck</code>: This linter is deprecated use <code>unused</code> instead.</li>
</ul>
</li>
<li>documentation
<ul>
<li><code>revive</code>: fix wrong URL</li>
<li>Add a section about default exclusions</li>
<li><code>usestdlibvars</code>: fix typo in documentation</li>
<li><code>nolintlint</code>: remove allow-leading-space option</li>
<li>Update documentation and assets</li>
</ul>
</li>
<li>misc.
<ul>
<li>dev: rewrite the internal tests framework</li>
<li>fix: exit early on run --version</li>
<li>fix: set an explicit <code>GOROOT</code> in the Docker image for <code>go-critic</code></li>
</ul>
</li>
</ol>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a href="https://github.com/golangci/golangci-lint/commit/cc2d97f31dea62c47c55d5b7a93637e242df7ad1"><code>cc2d97f</code></a> contextcheck: bump to v1.0.6 &amp;&amp; re-enable (<a href="https://github-redirect.dependabot.com/golangci/golangci-lint/issues/3102">#3102</a>)</li>
<li><a href="https://github.com/golangci/golangci-lint/commit/58809c3bf5d4e94500f7880978b6eb5e204f0f96"><code>58809c3</code></a> feat: add reassign linter (<a href="https://github-redirect.dependabot.com/golangci/golangci-lint/issues/3064">#3064</a>)</li>
<li><a href="https://github.com/golangci/golangci-lint/commit/77962e92832feaf1e2e9056bc66869fa0e6ebc5d"><code>77962e9</code></a> dev: fix linter tests (<a href="https://github-redirect.dependabot.com/golangci/golangci-lint/issues/3128">#3128</a>)</li>
<li><a href="https://github.com/golangci/golangci-lint/commit/f48530e25ea0b1db098c025a27348a720c4492b7"><code>f48530e</code></a> feat: add logrlint (<a href="https://github-redirect.dependabot.com/golangci/golangci-lint/issues/3093">#3093</a>)</li>
<li><a href="https://github.com/golangci/golangci-lint/commit/320a18eb372f5a1084b038633095a3955c806d9b"><code>320a18e</code></a> dev: concat string</li>
<li><a href="https://github.com/golangci/golangci-lint/commit/3aea4ce9ced663f34beb27a7c944c0eac68d778f"><code>3aea4ce</code></a> fix: flaky tests</li>
<li><a href="https://github.com/golangci/golangci-lint/commit/37d3aa437a9192ae167c71e63719ddf1c858be7f"><code>37d3aa4</code></a> feat: deprecate varcheck, deadcode, and structcheck (<a href="https://github-redirect.dependabot.com/golangci/golangci-lint/issues/3125">#3125</a>)</li>
<li><a href="https://github.com/golangci/golangci-lint/commit/e1afce44332c177ae56e3fd8e6d0b1d6250d7b3f"><code>e1afce4</code></a> dev: improve runner to run dir with go.mod (<a href="https://github-redirect.dependabot.com/golangci/golangci-lint/issues/3124">#3124</a>)</li>
<li><a href="https://github.com/golangci/golangci-lint/commit/970b0a5bd7568f3a2b69f0af49be092c7a378307"><code>970b0a5</code></a> fix: set an explicit GOROOT in the Docker image for go-critic (<a href="https://github-redirect.dependabot.com/golangci/golangci-lint/issues/3106">#3106</a>)</li>
<li><a href="https://github.com/golangci/golangci-lint/commit/63b2fe004847ccbe4d744f392e749d575dd77be8"><code>63b2fe0</code></a> feat: add interfacebloat (<a href="https://github-redirect.dependabot.com/golangci/golangci-lint/issues/3024">#3024</a>)</li>
<li>Additional commits viewable in <a href="https://github.com/golangci/golangci-lint/compare/v1.48.0...v1.49.0">compare view</a></li>
</ul>
</details>
<br />

[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=github.com/golangci/golangci-lint&package-manager=go_modules&previous-version=1.48.0&new-version=1.49.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)

</details>

* chore: bump umee to v3 (#1283)

## Description

closes: #1282

---

### Author Checklist

_All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues._

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] added appropriate labels to the PR
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/umee-network/umee/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

_All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items._

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)

* ci: lint file patterns (#1286)

Co-authored-by: Adam Wozniak <29418299+adamewozniak@users.noreply.github.com>
Co-authored-by: Robert Zaremba <robert@zaremba.ch>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Aug 26, 2022
1 parent 5323833 commit 4bdbf52
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 101 deletions.
1 change: 1 addition & 0 deletions price-feeder/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
- [#1170](https://github.com/umee-network/umee/pull/1170) Restrict price feeder quotes to USD, USDT, USDC, ETH, DAI, and BTC.
- [#1175](https://github.com/umee-network/umee/pull/1175) Add ProviderName type to facilitate the reading of maps.
- [#1215](https://github.com/umee-network/umee/pull/1215) Moved ProviderName to Name in provider package.
- [#1274](https://github.com/umee-network/umee/pull/1274) Add option to set config by env variables.

## [v0.2.5](https://github.com/umee-network/umee/releases/tag/price-feeder%2Fv0.2.5) - 2022-07-28

Expand Down
103 changes: 37 additions & 66 deletions price-feeder/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package config
import (
"errors"
"fmt"
"os"
"strings"
"time"

"github.com/BurntSushi/toml"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/go-playground/validator/v10"
"github.com/spf13/viper"
"github.com/umee-network/umee/price-feeder/oracle/provider"
)

Expand Down Expand Up @@ -60,98 +60,67 @@ var (
type (
// Config defines all necessary price-feeder configuration parameters.
Config struct {
Server Server `toml:"server"`
CurrencyPairs []CurrencyPair `toml:"currency_pairs" validate:"required,gt=0,dive,required"`
Deviations []Deviation `toml:"deviation_thresholds"`
Account Account `toml:"account" validate:"required,gt=0,dive,required"`
Keyring Keyring `toml:"keyring" validate:"required,gt=0,dive,required"`
RPC RPC `toml:"rpc" validate:"required,gt=0,dive,required"`
Telemetry Telemetry `toml:"telemetry"`
GasAdjustment float64 `toml:"gas_adjustment" validate:"required"`
ProviderTimeout string `toml:"provider_timeout"`
ProviderEndpoints []provider.Endpoint `toml:"provider_endpoints" validate:"dive"`
Server Server `mapstructure:"server"`
CurrencyPairs []CurrencyPair `mapstructure:"currency_pairs" validate:"required,gt=0,dive,required"`
Deviations []Deviation `mapstructure:"deviation_thresholds"`
Account Account `mapstructure:"account" validate:"required,gt=0,dive,required"`
Keyring Keyring `mapstructure:"keyring" validate:"required,gt=0,dive,required"`
RPC RPC `mapstructure:"rpc" validate:"required,gt=0,dive,required"`
Telemetry telemetry.Config `mapstructure:"telemetry"`
GasAdjustment float64 `mapstructure:"gas_adjustment" validate:"required"`
ProviderTimeout string `mapstructure:"provider_timeout"`
ProviderEndpoints []provider.Endpoint `mapstructure:"provider_endpoints" validate:"dive"`
}

// Server defines the API server configuration.
Server struct {
ListenAddr string `toml:"listen_addr"`
WriteTimeout string `toml:"write_timeout"`
ReadTimeout string `toml:"read_timeout"`
VerboseCORS bool `toml:"verbose_cors"`
AllowedOrigins []string `toml:"allowed_origins"`
ListenAddr string `mapstructure:"listen_addr"`
WriteTimeout string `mapstructure:"write_timeout"`
ReadTimeout string `mapstructure:"read_timeout"`
VerboseCORS bool `mapstructure:"verbose_cors"`
AllowedOrigins []string `mapstructure:"allowed_origins"`
}

// CurrencyPair defines a price quote of the exchange rate for two different
// currencies and the supported providers for getting the exchange rate.
CurrencyPair struct {
Base string `toml:"base" validate:"required"`
Quote string `toml:"quote" validate:"required"`
Providers []provider.Name `toml:"providers" validate:"required,gt=0,dive,required"`
Base string `mapstructure:"base" validate:"required"`
Quote string `mapstructure:"quote" validate:"required"`
Providers []provider.Name `mapstructure:"providers" validate:"required,gt=0,dive,required"`
}

// Deviation defines a maximum amount of standard deviations that a given asset can
// be from the median without being filtered out before voting.
Deviation struct {
Base string `toml:"base" validate:"required"`
Threshold string `toml:"threshold" validate:"required"`
Base string `mapstructure:"base" validate:"required"`
Threshold string `mapstructure:"threshold" validate:"required"`
}

// Account defines account related configuration that is related to the Umee
// network and transaction signing functionality.
Account struct {
ChainID string `toml:"chain_id" validate:"required"`
Address string `toml:"address" validate:"required"`
Validator string `toml:"validator" validate:"required"`
ChainID string `mapstructure:"chain_id" validate:"required"`
Address string `mapstructure:"address" validate:"required"`
Validator string `mapstructure:"validator" validate:"required"`
}

// Keyring defines the required Umee keyring configuration.
Keyring struct {
Backend string `toml:"backend" validate:"required"`
Dir string `toml:"dir" validate:"required"`
Backend string `mapstructure:"backend" validate:"required"`
Dir string `mapstructure:"dir" validate:"required"`
}

// RPC defines RPC configuration of both the Umee gRPC and Tendermint nodes.
RPC struct {
TMRPCEndpoint string `toml:"tmrpc_endpoint" validate:"required"`
GRPCEndpoint string `toml:"grpc_endpoint" validate:"required"`
RPCTimeout string `toml:"rpc_timeout" validate:"required"`
}

// Telemetry defines the configuration options for application telemetry.
Telemetry struct {
// Prefixed with keys to separate services
ServiceName string `toml:"service_name" mapstructure:"service-name"`

// Enabled enables the application telemetry functionality. When enabled,
// an in-memory sink is also enabled by default. Operators may also enabled
// other sinks such as Prometheus.
Enabled bool `toml:"enabled" mapstructure:"enabled"`

// Enable prefixing gauge values with hostname
EnableHostname bool `toml:"enable_hostname" mapstructure:"enable-hostname"`

// Enable adding hostname to labels
EnableHostnameLabel bool `toml:"enable_hostname_label" mapstructure:"enable-hostname-label"`

// Enable adding service to labels
EnableServiceLabel bool `toml:"enable_service_label" mapstructure:"enable-service-label"`

// GlobalLabels defines a global set of name/value label tuples applied to all
// metrics emitted using the wrapper functions defined in telemetry package.
//
// Example:
// [["chain_id", "cosmoshub-1"]]
GlobalLabels [][]string `toml:"global_labels" mapstructure:"global-labels"`

// PrometheusRetentionTime, when positive, enables a Prometheus metrics sink.
// It defines the retention duration in seconds.
PrometheusRetentionTime int64 `toml:"prometheus_retention" mapstructure:"prometheus-retention-time"`
TMRPCEndpoint string `mapstructure:"tmrpc_endpoint" validate:"required"`
GRPCEndpoint string `mapstructure:"grpc_endpoint" validate:"required"`
RPCTimeout string `mapstructure:"rpc_timeout" validate:"required"`
}
)

// telemetryValidation is custom validation for the Telemetry struct.
func telemetryValidation(sl validator.StructLevel) {
tel := sl.Current().Interface().(Telemetry)
tel := sl.Current().Interface().(telemetry.Config)

if tel.Enabled && (len(tel.GlobalLabels) == 0 || len(tel.ServiceName) == 0) {
sl.ReportError(tel.Enabled, "enabled", "Enabled", "enabledNoOptions", "")
Expand All @@ -172,7 +141,7 @@ func endpointValidation(sl validator.StructLevel) {

// Validate returns an error if the Config object is invalid.
func (c Config) Validate() error {
validate.RegisterStructValidation(telemetryValidation, Telemetry{})
validate.RegisterStructValidation(telemetryValidation, telemetry.Config{})
validate.RegisterStructValidation(endpointValidation, provider.Endpoint{})
return validate.Struct(c)
}
Expand All @@ -186,12 +155,14 @@ func ParseConfig(configPath string) (Config, error) {
return cfg, ErrEmptyConfigPath
}

configData, err := os.ReadFile(configPath)
if err != nil {
viper.AutomaticEnv()
viper.SetConfigFile(configPath)

if err := viper.ReadInConfig(); err != nil {
return cfg, fmt.Errorf("failed to read config: %w", err)
}

if _, err := toml.Decode(string(configData), &cfg); err != nil {
if err := viper.Unmarshal(&cfg); err != nil {
return cfg, fmt.Errorf("failed to decode config: %w", err)
}

Expand Down
Loading

0 comments on commit 4bdbf52

Please sign in to comment.