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

Add simple method of configuring via envars #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 15 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,29 @@ import (
"encoding/json"
"fmt"
"os"

"github.com/caarlos0/env"
)

func ReadConfig(configName string, target interface{}) error {
filename := fmt.Sprintf("/etc/sensu/%s.json", configName)

// First attempt to open the file
file, err := os.Open(filename)
if err != nil {
if err == nil {
decoder := json.NewDecoder(file)
err = decoder.Decode(target)
if err != nil {
return err
}
} else if !os.IsNotExist(err) {
// We don't need the error if it was a does not exist because it's still
// possible to configure via envars.
return err
}

decoder := json.NewDecoder(file)
err = decoder.Decode(target)
// Override any json value with envars
err = env.Parse(target)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module github.com/sensu-utils/toolbox

require (
github.com/caarlos0/env v3.5.0+incompatible
github.com/coreos/etcd v3.3.12+incompatible // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/echlebek/timeproxy v1.0.0 // indirect
Expand Down