mirror of
https://github.com/thomiceli/opengist.git
synced 2025-06-12 13:37:13 +02:00
Better config (#50)
This commit is contained in:
@ -7,6 +7,7 @@ import (
|
||||
"gopkg.in/yaml.v3"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@ -18,30 +19,30 @@ var C *config
|
||||
// Not using nested structs because the library
|
||||
// doesn't support dot notation in this case sadly
|
||||
type config struct {
|
||||
LogLevel string `yaml:"log-level"`
|
||||
ExternalUrl string `yaml:"external-url"`
|
||||
OpengistHome string `yaml:"opengist-home"`
|
||||
DBFilename string `yaml:"db-filename"`
|
||||
LogLevel string `yaml:"log-level" env:"OG_LOG_LEVEL"`
|
||||
ExternalUrl string `yaml:"external-url" env:"OG_EXTERNAL_URL"`
|
||||
OpengistHome string `yaml:"opengist-home" env:"OG_OPENGIST_HOME"`
|
||||
DBFilename string `yaml:"db-filename" env:"OG_DB_FILENAME"`
|
||||
|
||||
HttpHost string `yaml:"http.host"`
|
||||
HttpPort string `yaml:"http.port"`
|
||||
HttpGit bool `yaml:"http.git-enabled"`
|
||||
HttpTLSEnabled bool `yaml:"http.tls-enabled"`
|
||||
HttpCertFile string `yaml:"http.cert-file"`
|
||||
HttpKeyFile string `yaml:"http.key-file"`
|
||||
HttpHost string `yaml:"http.host" env:"OG_HTTP_HOST"`
|
||||
HttpPort string `yaml:"http.port" env:"OG_HTTP_PORT"`
|
||||
HttpGit bool `yaml:"http.git-enabled" env:"OG_HTTP_GIT_ENABLED"`
|
||||
HttpTLSEnabled bool `yaml:"http.tls-enabled" env:"OG_HTTP_TLS_ENABLED"`
|
||||
HttpCertFile string `yaml:"http.cert-file" env:"OG_HTTP_CERT_FILE"`
|
||||
HttpKeyFile string `yaml:"http.key-file" env:"OG_HTTP_KEY_FILE"`
|
||||
|
||||
SshGit bool `yaml:"ssh.git-enabled"`
|
||||
SshHost string `yaml:"ssh.host"`
|
||||
SshPort string `yaml:"ssh.port"`
|
||||
SshExternalDomain string `yaml:"ssh.external-domain"`
|
||||
SshKeygen string `yaml:"ssh.keygen-executable"`
|
||||
SshGit bool `yaml:"ssh.git-enabled" env:"OG_SSH_GIT_ENABLED"`
|
||||
SshHost string `yaml:"ssh.host" env:"OG_SSH_HOST"`
|
||||
SshPort string `yaml:"ssh.port" env:"OG_SSH_PORT"`
|
||||
SshExternalDomain string `yaml:"ssh.external-domain" env:"OG_SSH_EXTERNAL_DOMAIN"`
|
||||
SshKeygen string `yaml:"ssh.keygen-executable" env:"OG_SSH_KEYGEN_EXECUTABLE"`
|
||||
|
||||
GithubClientKey string `yaml:"github.client-key"`
|
||||
GithubSecret string `yaml:"github.secret"`
|
||||
GithubClientKey string `yaml:"github.client-key" env:"OG_GITHUB_CLIENT_KEY"`
|
||||
GithubSecret string `yaml:"github.secret" env:"OG_GITHUB_SECRET"`
|
||||
|
||||
GiteaClientKey string `yaml:"gitea.client-key"`
|
||||
GiteaSecret string `yaml:"gitea.secret"`
|
||||
GiteaUrl string `yaml:"gitea.url"`
|
||||
GiteaClientKey string `yaml:"gitea.client-key" env:"OG_GITEA_CLIENT_KEY"`
|
||||
GiteaSecret string `yaml:"gitea.secret" env:"OG_GITEA_SECRET"`
|
||||
GiteaUrl string `yaml:"gitea.url" env:"OG_GITEA_URL"`
|
||||
}
|
||||
|
||||
func configWithDefaults() (*config, error) {
|
||||
@ -77,37 +78,12 @@ func InitConfig(configPath string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if configPath != "" {
|
||||
absolutePath, _ := filepath.Abs(configPath)
|
||||
absolutePath = filepath.Clean(absolutePath)
|
||||
file, err := os.Open(absolutePath)
|
||||
if err != nil {
|
||||
if !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
fmt.Println("No YML config file found at " + absolutePath)
|
||||
} else {
|
||||
fmt.Println("Using config file: " + absolutePath)
|
||||
|
||||
// Override default values with values from config.yml
|
||||
d := yaml.NewDecoder(file)
|
||||
if err = d.Decode(&c); err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
}
|
||||
} else {
|
||||
fmt.Println("No config file specified. Using default values.")
|
||||
if err = loadConfigFromYaml(c, configPath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Override default values with environment variables (as yaml)
|
||||
configEnv := os.Getenv("CONFIG")
|
||||
if configEnv != "" {
|
||||
fmt.Println("Using config from environment variable: CONFIG")
|
||||
d := yaml.NewDecoder(strings.NewReader(configEnv))
|
||||
if err = d.Decode(&c); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = loadConfigFromEnv(c); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
C = c
|
||||
@ -159,3 +135,79 @@ func GetHomeDir() string {
|
||||
absolutePath, _ := filepath.Abs(C.OpengistHome)
|
||||
return filepath.Clean(absolutePath)
|
||||
}
|
||||
|
||||
func loadConfigFromYaml(c *config, configPath string) error {
|
||||
if configPath != "" {
|
||||
absolutePath, _ := filepath.Abs(configPath)
|
||||
absolutePath = filepath.Clean(absolutePath)
|
||||
file, err := os.Open(absolutePath)
|
||||
if err != nil {
|
||||
if !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
fmt.Println("No YAML config file found at " + absolutePath)
|
||||
} else {
|
||||
fmt.Println("Using YAML config file: " + absolutePath)
|
||||
|
||||
// Override default values with values from config.yml
|
||||
d := yaml.NewDecoder(file)
|
||||
if err = d.Decode(&c); err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
}
|
||||
} else {
|
||||
fmt.Println("No YAML config file specified.")
|
||||
}
|
||||
|
||||
// Override default values with environment variables (as yaml)
|
||||
configEnv := os.Getenv("CONFIG")
|
||||
if configEnv != "" {
|
||||
fmt.Println("Using config from environment variable: CONFIG")
|
||||
d := yaml.NewDecoder(strings.NewReader(configEnv))
|
||||
if err := d.Decode(&c); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadConfigFromEnv(c *config) error {
|
||||
v := reflect.ValueOf(c).Elem()
|
||||
var envVars []string
|
||||
|
||||
for i := 0; i < v.NumField(); i++ {
|
||||
tag := v.Type().Field(i).Tag.Get("env")
|
||||
|
||||
if tag == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
envValue := os.Getenv(strings.ToUpper(tag))
|
||||
if envValue == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
switch v.Field(i).Kind() {
|
||||
case reflect.String:
|
||||
v.Field(i).SetString(envValue)
|
||||
case reflect.Bool:
|
||||
boolVal, err := strconv.ParseBool(envValue)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
v.Field(i).SetBool(boolVal)
|
||||
}
|
||||
|
||||
envVars = append(envVars, tag)
|
||||
}
|
||||
|
||||
if len(envVars) > 0 {
|
||||
fmt.Println("Using environment variables config: " + strings.Join(envVars, ", "))
|
||||
} else {
|
||||
fmt.Println("No environment variables config specified.")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user