62 lines
1.3 KiB
Go
62 lines
1.3 KiB
Go
package helpers
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/juju/errors"
|
|
"gopkg.in/yaml.v3"
|
|
|
|
"git.home.m-and-m.ovh/mderasse/gouick/helpers/models"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
const configFile = ".gouick.yml"
|
|
|
|
// ReadConfig will search the config file in the given path, read it and return a Config object.
|
|
func ReadConfig(path string) (*models.Config, error) {
|
|
|
|
configFilePath := filepath.Join(path, configFile)
|
|
|
|
log.Debugf("Opening configuration file in %s", configFilePath)
|
|
|
|
//nolint:gosec // we did compute the file path
|
|
f, err := os.ReadFile(configFilePath)
|
|
if err != nil {
|
|
return nil, errors.Trace(err)
|
|
}
|
|
|
|
var config models.Config
|
|
|
|
log.Debug("Unmarshal config file")
|
|
if err := yaml.Unmarshal(f, &config); err != nil {
|
|
return nil, errors.Trace(err)
|
|
}
|
|
|
|
return &config, nil
|
|
}
|
|
|
|
// WriteConfig will write the new or updated config.
|
|
func WriteConfig(path string, config *models.Config) error {
|
|
|
|
configFilePath := filepath.Join(path, configFile)
|
|
|
|
log.Debug("Marshal config into YAML Format")
|
|
|
|
data, err := yaml.Marshal(config)
|
|
if err != nil {
|
|
return errors.Trace(err)
|
|
}
|
|
|
|
log.Debugf("Writing config to %s", configFilePath)
|
|
|
|
err = ioutil.WriteFile(configFilePath, data, 0600)
|
|
if err != nil {
|
|
return errors.Trace(err)
|
|
}
|
|
|
|
return nil
|
|
}
|