2019-09-08 00:14:00 +05:00
|
|
|
package configuration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This structure represents application's configuration.
|
|
|
|
type config struct {
|
|
|
|
Debug bool `yaml:"debug"`
|
|
|
|
Database struct {
|
|
|
|
DSN string `yaml:"dsn"`
|
|
|
|
Parameters string `yaml:"parameters"`
|
|
|
|
Prefix string `yaml:"prefix"`
|
|
|
|
} `yaml:"database"`
|
|
|
|
Groups struct {
|
|
|
|
Default string `yaml:"default"`
|
|
|
|
Groups []struct {
|
|
|
|
Group string `yaml:"group"`
|
|
|
|
Users []string `yaml:"users"`
|
|
|
|
}
|
|
|
|
} `yaml:"groups"`
|
|
|
|
}
|
|
|
|
|
2022-08-29 09:48:51 +05:00
|
|
|
// Checks necessary parameters for filling.
|
2019-09-08 00:14:00 +05:00
|
|
|
func (c *config) checkParameters() {
|
|
|
|
if c.Database.DSN == "" {
|
|
|
|
log.Fatalln("database/dsn parameter isn't filled, don't know to which database I should connect!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize initializes configuration structure by reading configuration
|
|
|
|
// file and populate structure with it's data.
|
|
|
|
func (c *config) Initialize() {
|
|
|
|
// If -config wasn't provided or empty - do nothing.
|
|
|
|
if configPath == "" {
|
|
|
|
log.Fatalln("-config parameter is empty, don't know where configuration is!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normalize configuration path.
|
|
|
|
if strings.HasPrefix(configPath, "~") {
|
|
|
|
userDir, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Failed to get user's home directory for user! Error was: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
configPath = strings.Replace(configPath, "~", userDir, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
absolutePath, err := filepath.Abs(configPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Failed to get configuration file's absolute path! Error was: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read and parse.
|
2022-08-29 09:48:51 +05:00
|
|
|
dataAsBytes, err1 := os.ReadFile(absolutePath)
|
2019-09-08 00:14:00 +05:00
|
|
|
if err1 != nil {
|
|
|
|
log.Fatalln("Failed to read configuration file! Error was: " + err1.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
err2 := yaml.Unmarshal(dataAsBytes, c)
|
|
|
|
if err2 != nil {
|
|
|
|
log.Fatalln("Failed to parse configuration file! Error was: " + err2.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Debug {
|
|
|
|
log.Printf("Configuration parsed: %+v\n", c)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.checkParameters()
|
|
|
|
}
|