Files
filestash/server/common/config_state.go
Mickael Kerjean 9171b6fb5e refactoring (config): separate config loading and saving
The idea is that we can now plug and play different logic for loading
and saving configuration data via a go generator built from plugins
2022-02-04 00:51:57 +11:00

52 lines
1.4 KiB
Go

package common
/*
* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNI
* WARNING - CHANGE IN THIS FILE CAN SILENTLY BREAK OTHER INSTALLATION - WARNING
* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARN
*
* Some contributors wanted to be able to load and persist config in other system
* like S3 and provide custom encryption layer on top of it. Those contributors have
* custom plugins which run generators that override this file before the build is
* generated. Indeed for that specific use case we couldn't extend the runtime plugin
* mechanism so had to fallback to this approach which would set the config loader at
* build time, hence this warning.
*/
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
var (
configPath string = filepath.Join(GetCurrentDir(), CONFIG_PATH+"config.json")
)
func LoadConfig() ([]byte, error) {
file, err := os.OpenFile(configPath, os.O_RDONLY, os.ModePerm)
if err != nil {
return nil, err
}
cFile, err := ioutil.ReadAll(file)
file.Close()
if err != nil {
return nil, err
}
return cFile, nil
}
func SaveConfig(v []byte) error {
file, err := os.Create(configPath)
if err != nil {
return fmt.Errorf(
"Filestash needs to be able to create/edit its own configuration which it can't at the moment. "+
"Change the permission for filestash to create and edit `%s`",
configPath,
)
}
file.Write(PrettyPrint([]byte(v)))
return file.Close()
}