1
0
mirror of https://github.com/thomiceli/opengist synced 2024-11-12 07:05:50 +01:00
opengist/internal/config/config.go

231 lines
5.6 KiB
Go
Raw Normal View History

2023-03-14 16:22:52 +01:00
package config
import (
2023-03-24 14:41:08 +01:00
"fmt"
"net/url"
2023-03-14 16:22:52 +01:00
"os"
"path/filepath"
2023-06-07 20:50:30 +02:00
"reflect"
2023-03-24 14:41:08 +01:00
"strconv"
"strings"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/thomiceli/opengist/internal/utils"
"gopkg.in/yaml.v3"
2023-03-14 16:22:52 +01:00
)
2023-07-26 10:54:16 +02:00
var OpengistVersion = "1.5-dev"
2023-03-14 16:22:52 +01:00
var C *config
2023-04-07 01:52:56 +02:00
// Not using nested structs because the library
// doesn't support dot notation in this case sadly
2023-03-14 16:22:52 +01:00
type config struct {
2023-06-07 20:50:30 +02:00
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"`
2023-06-09 15:25:41 +02:00
SqliteJournalMode string `yaml:"sqlite.journal-mode" env:"OG_SQLITE_JOURNAL_MODE"`
2023-06-07 20:50:30 +02:00
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" 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" env:"OG_GITHUB_CLIENT_KEY"`
GithubSecret string `yaml:"github.secret" env:"OG_GITHUB_SECRET"`
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"`
2023-03-14 16:22:52 +01:00
}
2023-03-15 01:36:46 +01:00
func configWithDefaults() (*config, error) {
2023-03-14 16:22:52 +01:00
homeDir, err := os.UserHomeDir()
2023-03-15 01:36:46 +01:00
c := &config{}
2023-03-14 16:22:52 +01:00
if err != nil {
2023-03-15 01:36:46 +01:00
return c, err
2023-03-14 16:22:52 +01:00
}
2023-03-15 01:36:46 +01:00
2023-04-07 01:52:56 +02:00
c.LogLevel = "warn"
2023-03-14 16:22:52 +01:00
c.OpengistHome = filepath.Join(homeDir, ".opengist")
2023-03-15 01:36:46 +01:00
c.DBFilename = "opengist.db"
2023-06-09 15:25:41 +02:00
c.SqliteJournalMode = "WAL"
2023-04-07 01:52:56 +02:00
c.HttpHost = "0.0.0.0"
c.HttpPort = "6157"
c.HttpGit = true
c.HttpTLSEnabled = false
2023-03-15 10:37:17 +01:00
2023-04-07 01:52:56 +02:00
c.SshGit = true
c.SshHost = "0.0.0.0"
c.SshPort = "2222"
c.SshKeygen = "ssh-keygen"
2023-03-15 01:36:46 +01:00
2023-04-17 19:11:32 +02:00
c.GiteaUrl = "http://gitea.com"
2023-03-15 01:36:46 +01:00
return c, nil
}
func InitConfig(configPath string) error {
2023-04-07 01:52:56 +02:00
// Default values
2023-03-15 01:36:46 +01:00
c, err := configWithDefaults()
if err != nil {
return err
}
2023-06-07 20:50:30 +02:00
if err = loadConfigFromYaml(c, configPath); err != nil {
return err
2023-03-14 16:22:52 +01:00
}
2023-06-07 20:50:30 +02:00
if err = loadConfigFromEnv(c); err != nil {
return err
2023-03-14 16:22:52 +01:00
}
2023-04-07 01:52:56 +02:00
if err = checks(c); err != nil {
return err
}
2023-03-14 16:22:52 +01:00
C = c
return nil
}
func InitLog() {
if err := os.MkdirAll(filepath.Join(GetHomeDir(), "log"), 0755); err != nil {
panic(err)
}
file, err := os.OpenFile(filepath.Join(GetHomeDir(), "log", "opengist.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
var level zerolog.Level
level, err = zerolog.ParseLevel(C.LogLevel)
if err != nil {
level = zerolog.InfoLevel
}
2023-05-24 16:40:27 +02:00
multi := zerolog.MultiLevelWriter(zerolog.NewConsoleWriter(), file)
log.Logger = zerolog.New(multi).Level(level).With().Timestamp().Logger()
2023-06-09 15:25:41 +02:00
if !utils.SliceContains([]string{"trace", "debug", "info", "warn", "error", "fatal", "panic"}, strings.ToLower(C.LogLevel)) {
log.Warn().Msg("Invalid log level: " + C.LogLevel)
}
2023-03-14 16:22:52 +01:00
}
2023-03-24 14:41:08 +01:00
func CheckGitVersion(version string) (bool, error) {
versionParts := strings.Split(version, ".")
if len(versionParts) < 2 {
return false, fmt.Errorf("invalid version string")
}
major, err := strconv.Atoi(versionParts[0])
if err != nil {
return false, fmt.Errorf("invalid major version number")
}
minor, err := strconv.Atoi(versionParts[1])
if err != nil {
return false, fmt.Errorf("invalid minor version number")
}
// Check if version is prior to 2.20
if major < 2 || (major == 2 && minor < 20) {
return false, nil
}
return true, nil
}
2023-03-14 16:22:52 +01:00
func GetHomeDir() string {
absolutePath, _ := filepath.Abs(C.OpengistHome)
return filepath.Clean(absolutePath)
}
2023-06-07 20:50:30 +02:00
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.")
}
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
}
func checks(c *config) error {
if _, err := url.Parse(c.ExternalUrl); err != nil {
return err
}
if _, err := url.Parse(c.GiteaUrl); err != nil {
return err
}
return nil
}