Skip to content

Commit

Permalink
fix(config): correct error handling for envConfig (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
shini4i authored Aug 3, 2023
1 parent d02f132 commit b4238ff
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
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 {
return nil, 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

0 comments on commit b4238ff

Please sign in to comment.