Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(config): correct error handling for envConfig #165

Merged
merged 2 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions cmd/argo-watcher/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package config

import (
"errors"
"github.com/shini4i/argo-watcher/internal/helpers"
"strconv"

envConfig "github.com/kelseyhightower/envconfig"
"github.com/shini4i/argo-watcher/internal/helpers"
)

type ServerConfig struct {
Expand Down Expand Up @@ -35,13 +35,21 @@ type ServerConfig struct {
// Otherwise, it returns the parsed server configuration and any error encountered during the parsing process.
func NewServerConfig() (*ServerConfig, error) {
// parse config
var config ServerConfig
err := envConfig.Process("", &config)
var (
err error
config ServerConfig
)

if err := envConfig.Process("", &config); err != nil {
panic(err)
}

// custom checks
allowedTypes := []string{"postgres", "in-memory"}
if config.StateType == "" || !helpers.Contains(allowedTypes, config.StateType) {
return nil, errors.New("variable STATE_TYPE must be one of [\"postgres\", \"in-memory\"]")
}

// return config
return &config, err
}
Expand Down
2 changes: 1 addition & 1 deletion docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ cd cmd/argo-watcher
# install dependencies
go mod tidy
# start argo-watcher
ARGO_URL=http://localhost:8081 STATE_TYPE=in-memory go run .
ARGO_URL=http://localhost:8081 STATE_TYPE=in-memory go run . -server
```

### Running the unit tests
Expand Down
Loading