2017-08-28 01:13:45 +05:00
|
|
|
// OpenSAPS - Open Slack API server for everyone.
|
|
|
|
//
|
|
|
|
// Copyright (c) 2017, Stanislav N. aka pztrn.
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2018-02-02 09:17:40 +05:00
|
|
|
// stdlib
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os/user"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
// local
|
2019-12-15 22:17:01 +05:00
|
|
|
configstruct "go.dev.pztrn.name/opensaps/config/struct"
|
2018-02-02 09:17:40 +05:00
|
|
|
|
|
|
|
// other
|
2019-12-15 22:17:01 +05:00
|
|
|
"go.dev.pztrn.name/flagger"
|
2018-02-02 09:17:40 +05:00
|
|
|
"gopkg.in/yaml.v2"
|
2017-08-28 01:13:45 +05:00
|
|
|
)
|
|
|
|
|
2018-02-02 09:17:40 +05:00
|
|
|
type Configuration struct{}
|
2017-08-28 01:13:45 +05:00
|
|
|
|
|
|
|
// Returns configuration to caller.
|
|
|
|
func (conf Configuration) GetConfig() *configstruct.ConfigStruct {
|
2018-02-02 09:17:40 +05:00
|
|
|
return config
|
2017-08-28 01:13:45 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Gets value from temporary configuration storage.
|
|
|
|
// If value isn't found - returns empty string with error.
|
|
|
|
func (conf Configuration) GetTempValue(key string) (string, error) {
|
2018-02-02 09:17:40 +05:00
|
|
|
value, found := tempconfig[key]
|
|
|
|
if !found {
|
|
|
|
return "", errors.New("No such key in temporary configuration storage: " + key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have path with tilde in front (home directory) - replace
|
|
|
|
// tilde with actual home directory.
|
|
|
|
if value[0] == '~' {
|
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
c.Log.Fatalln("Failed to get current user data: " + err.Error())
|
|
|
|
}
|
2019-12-15 22:17:01 +05:00
|
|
|
|
2018-02-02 09:17:40 +05:00
|
|
|
value = strings.Replace(value, "~", usr.HomeDir, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return value, nil
|
2017-08-28 01:13:45 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (conf Configuration) Initialize() {
|
2018-02-02 09:17:40 +05:00
|
|
|
c.Log.Infoln("Initializing configuration storage...")
|
2017-08-28 01:13:45 +05:00
|
|
|
|
2018-02-02 09:17:40 +05:00
|
|
|
tempconfig = make(map[string]string)
|
2017-08-28 01:13:45 +05:00
|
|
|
|
2019-12-15 22:17:01 +05:00
|
|
|
flagConfigpath := flagger.Flag{
|
2018-02-02 09:17:40 +05:00
|
|
|
Name: "config",
|
|
|
|
Description: "Path to configuration file.",
|
|
|
|
Type: "string",
|
|
|
|
DefaultValue: "~/.config/OpenSAPS/config.yaml",
|
|
|
|
}
|
2017-08-28 01:13:45 +05:00
|
|
|
|
2019-12-15 22:17:01 +05:00
|
|
|
_ = c.Flagger.AddFlag(&flagConfigpath)
|
2017-08-28 01:13:45 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initializes configuration root path for later usage.
|
|
|
|
func (conf Configuration) initializeConfigurationFilePath() {
|
2018-02-02 09:17:40 +05:00
|
|
|
c.Log.Debugln("Asking flagger about configuration root path supplied by user...")
|
2017-08-28 01:13:45 +05:00
|
|
|
|
2018-02-02 09:17:40 +05:00
|
|
|
configpath, err := c.Flagger.GetStringValue("config")
|
|
|
|
if err != nil {
|
|
|
|
c.Log.Fatalln("Something went wrong - Flagger doesn't know about \"-config\" parameter!")
|
|
|
|
}
|
2017-08-28 01:13:45 +05:00
|
|
|
|
2018-02-02 09:17:40 +05:00
|
|
|
c.Log.Infoln("Will use configuration file: '" + configpath + "'")
|
|
|
|
conf.SetTempValue("CONFIGURATION_FILE", configpath)
|
2017-08-28 01:13:45 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Asking Flagger about flags, initialize internal variables.
|
|
|
|
// Should be called **after** Flagger.Parse().
|
|
|
|
func (conf Configuration) InitializeLater() {
|
2018-02-02 09:17:40 +05:00
|
|
|
c.Log.Infoln("Completing configuration initialization...")
|
2017-08-28 01:13:45 +05:00
|
|
|
|
2018-02-02 09:17:40 +05:00
|
|
|
conf.initializeConfigurationFilePath()
|
2017-08-28 01:13:45 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Loads configuration from file.
|
|
|
|
func (conf Configuration) LoadConfigurationFromFile() {
|
2018-02-02 09:17:40 +05:00
|
|
|
configpath, err := conf.GetTempValue("CONFIGURATION_FILE")
|
|
|
|
if err != nil {
|
|
|
|
c.Log.Fatalln("Failed to get configuration file path from internal temporary configuration storage! OpenSAPS is BROKEN!")
|
|
|
|
}
|
2019-12-15 22:17:01 +05:00
|
|
|
|
2018-02-02 09:17:40 +05:00
|
|
|
c.Log.Infof("Loading configuration from '%s'...", configpath)
|
|
|
|
|
|
|
|
// Read file into memory.
|
2019-12-15 22:17:01 +05:00
|
|
|
configBytes, err1 := ioutil.ReadFile(configpath)
|
2018-02-02 09:17:40 +05:00
|
|
|
if err1 != nil {
|
2019-12-15 22:17:01 +05:00
|
|
|
c.Log.Fatalf("Error occurred while reading configuration file: %s", err1.Error())
|
2018-02-02 09:17:40 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
config = &configstruct.ConfigStruct{}
|
|
|
|
// Parse YAML.
|
2019-12-15 22:17:01 +05:00
|
|
|
err2 := yaml.Unmarshal(configBytes, config)
|
2018-02-02 09:17:40 +05:00
|
|
|
if err2 != nil {
|
|
|
|
c.Log.Fatalf("Failed to parse configuration file: %s", err2.Error())
|
|
|
|
}
|
2019-12-15 22:17:01 +05:00
|
|
|
|
2018-02-02 09:17:40 +05:00
|
|
|
c.Log.Debugln("Loaded configuration:", fmt.Sprintf("%+v", config))
|
2017-08-28 01:13:45 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sets value to key in temporary configuration storage.
|
|
|
|
// If key already present in map - value will be replaced.
|
|
|
|
func (conf Configuration) SetTempValue(key, value string) {
|
2018-02-02 09:17:40 +05:00
|
|
|
tempconfig[key] = value
|
2017-08-28 01:13:45 +05:00
|
|
|
}
|