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
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
|
|
|
"os/user"
|
|
|
|
"strings"
|
|
|
|
|
2019-12-15 22:17:01 +05:00
|
|
|
"go.dev.pztrn.name/flagger"
|
2021-11-21 16:16:10 +05:00
|
|
|
configstruct "go.dev.pztrn.name/opensaps/config/struct"
|
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 {
|
2021-11-21 16:16:10 +05:00
|
|
|
// ToDo: fix it!
|
|
|
|
// nolint:goerr113
|
2018-02-02 09:17:40 +05:00
|
|
|
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 {
|
2022-06-29 13:16:43 +05:00
|
|
|
ctx.Log.Fatal().Err(err).Msg("Failed to get current user data")
|
2018-02-02 09:17:40 +05:00
|
|
|
}
|
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() {
|
2022-06-29 13:16:43 +05:00
|
|
|
ctx.Log.Info().Msg("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
|
|
|
|
2022-06-29 13:16:43 +05:00
|
|
|
_ = ctx.Flagger.AddFlag(&flagConfigpath)
|
2017-08-28 01:13:45 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initializes configuration root path for later usage.
|
|
|
|
func (conf Configuration) initializeConfigurationFilePath() {
|
2022-06-29 13:16:43 +05:00
|
|
|
ctx.Log.Debug().Msg("Asking flagger about configuration root path supplied by user...")
|
2017-08-28 01:13:45 +05:00
|
|
|
|
2022-06-29 13:16:43 +05:00
|
|
|
configpath, err := ctx.Flagger.GetStringValue("config")
|
2018-02-02 09:17:40 +05:00
|
|
|
if err != nil {
|
2022-06-29 13:16:43 +05:00
|
|
|
ctx.Log.Fatal().Msg("Something went wrong - Flagger doesn't know about \"-config\" parameter!")
|
2018-02-02 09:17:40 +05:00
|
|
|
}
|
2017-08-28 01:13:45 +05:00
|
|
|
|
2022-06-29 13:16:43 +05:00
|
|
|
ctx.Log.Info().Msg("Will use configuration file: '" + configpath + "'")
|
2018-02-02 09:17:40 +05:00
|
|
|
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() {
|
2022-06-29 13:16:43 +05:00
|
|
|
ctx.Log.Info().Msg("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 {
|
2022-06-29 13:16:43 +05:00
|
|
|
ctx.Log.Fatal().
|
|
|
|
Msg("Failed to get configuration file path from internal temporary configuration storage! OpenSAPS is BROKEN!")
|
2018-02-02 09:17:40 +05:00
|
|
|
}
|
2019-12-15 22:17:01 +05:00
|
|
|
|
2022-06-29 13:16:43 +05:00
|
|
|
ctx.Log.Info().Msgf("Loading configuration from '%s'...", configpath)
|
2018-02-02 09:17:40 +05:00
|
|
|
|
|
|
|
// 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 {
|
2022-06-29 13:16:43 +05:00
|
|
|
ctx.Log.Fatal().Msgf("Error occurred while reading configuration file: %s", err1.Error())
|
2018-02-02 09:17:40 +05:00
|
|
|
}
|
|
|
|
|
2022-06-29 13:16:43 +05:00
|
|
|
// nolint:exhaustruct
|
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 {
|
2022-06-29 13:16:43 +05:00
|
|
|
ctx.Log.Fatal().Msgf("Failed to parse configuration file: %s", err2.Error())
|
2018-02-02 09:17:40 +05:00
|
|
|
}
|
2019-12-15 22:17:01 +05:00
|
|
|
|
2022-06-29 13:16:43 +05:00
|
|
|
ctx.Log.Debug().Msgf("Loaded configuration: %+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
|
|
|
}
|