opengist/internal/config/config.go

166 lines
4 KiB
Go
Raw Normal View History

2023-03-14 15:22:52 +00:00
package config
import (
2023-03-24 13:41:08 +00:00
"fmt"
2023-03-14 15:22:52 +00:00
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v3"
"os"
"path/filepath"
2023-03-24 13:41:08 +00:00
"strconv"
"strings"
2023-03-14 15:22:52 +00:00
)
2023-04-20 19:54:54 +00:00
var OpengistVersion = "1.1.1"
2023-03-14 15:22:52 +00:00
var C *config
2023-04-06 23:52:56 +00:00
// Not using nested structs because the library
// doesn't support dot notation in this case sadly
2023-03-14 15:22:52 +00:00
type config struct {
LogLevel string `yaml:"log-level"`
ExternalUrl string `yaml:"external-url"`
OpengistHome string `yaml:"opengist-home"`
DBFilename string `yaml:"db-filename"`
2023-03-14 15:22:52 +00:00
2023-04-06 23:52:56 +00:00
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"`
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"`
2023-04-17 17:11:32 +00:00
GithubClientKey string `yaml:"github.client-key"`
GithubSecret string `yaml:"github.secret"`
GiteaClientKey string `yaml:"gitea.client-key"`
GiteaSecret string `yaml:"gitea.secret"`
GiteaUrl string `yaml:"gitea.url"`
2023-03-14 15:22:52 +00:00
}
2023-03-15 00:36:46 +00:00
func configWithDefaults() (*config, error) {
2023-03-14 15:22:52 +00:00
homeDir, err := os.UserHomeDir()
2023-03-15 00:36:46 +00:00
c := &config{}
2023-03-14 15:22:52 +00:00
if err != nil {
2023-03-15 00:36:46 +00:00
return c, err
2023-03-14 15:22:52 +00:00
}
2023-03-15 00:36:46 +00:00
2023-04-06 23:52:56 +00:00
c.LogLevel = "warn"
2023-03-14 15:22:52 +00:00
c.OpengistHome = filepath.Join(homeDir, ".opengist")
2023-03-15 00:36:46 +00:00
c.DBFilename = "opengist.db"
2023-04-06 23:52:56 +00:00
c.HttpHost = "0.0.0.0"
c.HttpPort = "6157"
c.HttpGit = true
c.HttpTLSEnabled = false
2023-03-15 09:37:17 +00:00
2023-04-06 23:52:56 +00:00
c.SshGit = true
c.SshHost = "0.0.0.0"
c.SshPort = "2222"
c.SshKeygen = "ssh-keygen"
2023-03-15 00:36:46 +00:00
2023-04-17 17:11:32 +00:00
c.GiteaUrl = "http://gitea.com"
2023-03-15 00:36:46 +00:00
return c, nil
}
func InitConfig(configPath string) error {
2023-04-06 23:52:56 +00:00
// Default values
2023-03-15 00:36:46 +00:00
c, err := configWithDefaults()
if err != nil {
return err
}
2023-04-07 21:58:06 +00:00
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()
2023-04-06 23:52:56 +00:00
}
2023-04-07 21:58:06 +00:00
} else {
fmt.Println("No config file specified. Using default values.")
2023-03-14 15:22:52 +00:00
}
2023-04-06 23:52:56 +00:00
// 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
}
2023-03-14 15:22:52 +00:00
}
2023-04-06 23:52:56 +00:00
2023-03-14 15:22:52 +00: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-04-07 21:58:06 +00:00
if os.Getenv("DEV") == "1" {
multi := zerolog.MultiLevelWriter(zerolog.NewConsoleWriter(), file)
log.Logger = zerolog.New(multi).Level(level).With().Timestamp().Logger()
} else {
log.Logger = zerolog.New(file).Level(level).With().Timestamp().Logger()
}
2023-03-14 15:22:52 +00:00
}
2023-03-24 13:41:08 +00: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 15:22:52 +00:00
func GetHomeDir() string {
absolutePath, _ := filepath.Abs(C.OpengistHome)
return filepath.Clean(absolutePath)
}