refactor(ops/besadii): Get config from home directory by default

Slightly more ergonomic in some setups.

Change-Id: I565f2d242852ffd299ef5d5740a47520187dd4b4
This commit is contained in:
Vincent Ambo 2021-12-02 17:13:49 +03:00
parent dcb2410982
commit b679bb4034

View file

@ -23,6 +23,7 @@ import (
"log/syslog"
"net/http"
"os"
"os/user"
"path"
"regexp"
"strconv"
@ -96,10 +97,24 @@ type reviewInput struct {
Tag string `json:"tag"`
}
func defaultConfigLocation() (string, error) {
usr, err := user.Current()
if err != nil {
return "", fmt.Errorf("failed to get current user: %w", err)
}
return path.Join(usr.HomeDir, "besadii.json"), nil
}
func loadConfig() (*config, error) {
configPath := os.Getenv("BESADII_CONFIG")
if configPath == "" {
configPath = "/etc/besadii/config.json"
var err error
configPath, err = defaultConfigLocation()
if err != nil {
return nil, fmt.Errorf("failed to get config location: %w", err)
}
}
configJson, err := ioutil.ReadFile(configPath)