webhookd/pkg/config/config.go
Nicolas Carlier 1dab1e968d feat(notification): complete refactoring of the notification system
- URL based configuration
- Only prefixed output lines are notified
- HTTP notifier: send a JSON with notification in the text attribute
- SMTP notifier: send an email with notification text in body
2018-12-30 21:00:22 +00:00

65 lines
2.0 KiB
Go

package config
import (
"flag"
"os"
"strconv"
)
// Config contain global configuration
type Config struct {
ListenAddr *string
NbWorkers *int
Debug *bool
Timeout *int
ScriptDir *string
PasswdFile *string
LogDir *string
NotificationURI *string
}
var config = &Config{
ListenAddr: flag.String("listen", getEnv("LISTEN_ADDR", ":8080"), "HTTP service address (e.g.address, ':8080')"),
NbWorkers: flag.Int("nb-workers", getIntEnv("NB_WORKERS", 2), "The number of workers to start"),
Debug: flag.Bool("debug", getBoolEnv("DEBUG", false), "Output debug logs"),
Timeout: flag.Int("timeout", getIntEnv("HOOK_TIMEOUT", 10), "Hook maximum delay before timeout (in second)"),
ScriptDir: flag.String("scripts", getEnv("SCRIPTS_DIR", "scripts"), "Scripts directory"),
PasswdFile: flag.String("passwd", getEnv("PASSWD_FILE", ".htpasswd"), "Password file (encoded with htpasswd)"),
LogDir: flag.String("log-dir", getEnv("LOG_DIR", os.TempDir()), "Webhooks execution log directory"),
NotificationURI: flag.String("notification-uri", getEnv("NOTIFICATION_URI", ""), "Notification URI"),
}
func init() {
flag.StringVar(config.ListenAddr, "l", *config.ListenAddr, "HTTP service (e.g address: ':8080')")
flag.BoolVar(config.Debug, "d", *config.Debug, "Output debug logs")
flag.StringVar(config.PasswdFile, "p", *config.PasswdFile, "Password file (encoded with htpasswd)")
}
// Get global configuration
func Get() *Config {
return config
}
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv("APP_" + key); ok {
return value
}
return fallback
}
func getIntEnv(key string, fallback int) int {
strValue := getEnv(key, strconv.Itoa(fallback))
if value, err := strconv.Atoi(strValue); err == nil {
return value
}
return fallback
}
func getBoolEnv(key string, fallback bool) bool {
strValue := getEnv(key, strconv.FormatBool(fallback))
if value, err := strconv.ParseBool(strValue); err == nil {
return value
}
return fallback
}