2021-10-27 14:45:36 +00:00
|
|
|
package bootstrap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/Xhofe/alist/conf"
|
|
|
|
"github.com/Xhofe/alist/utils"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"io/ioutil"
|
2022-01-27 15:48:29 +00:00
|
|
|
"os"
|
2021-10-27 14:45:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// InitConf init config
|
|
|
|
func InitConf() {
|
2022-02-08 08:22:53 +00:00
|
|
|
err := os.MkdirAll("data/temp", 0700)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("create temp dir error: %s", err.Error())
|
|
|
|
}
|
2021-10-27 14:45:36 +00:00
|
|
|
log.Infof("reading config file: %s", conf.ConfigFile)
|
|
|
|
if !utils.Exists(conf.ConfigFile) {
|
|
|
|
log.Infof("config file not exists, creating default config file")
|
2021-11-13 06:23:41 +00:00
|
|
|
_, err := utils.CreatNestedFile(conf.ConfigFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to create config file")
|
|
|
|
}
|
2021-10-27 14:45:36 +00:00
|
|
|
conf.Conf = conf.DefaultConfig()
|
|
|
|
if !utils.WriteToJson(conf.ConfigFile, conf.Conf) {
|
|
|
|
log.Fatalf("failed to create default config file")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
config, err := ioutil.ReadFile(conf.ConfigFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("reading config file error:%s", err.Error())
|
|
|
|
}
|
|
|
|
conf.Conf = new(conf.Config)
|
2022-01-03 12:25:22 +00:00
|
|
|
err = utils.Json.Unmarshal(config, conf.Conf)
|
2021-10-27 14:45:36 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("load config error: %s", err.Error())
|
|
|
|
}
|
|
|
|
log.Debugf("config:%+v", conf.Conf)
|
2022-01-07 12:29:20 +00:00
|
|
|
// update config.json struct
|
2022-01-14 12:10:35 +00:00
|
|
|
confBody, err := utils.Json.MarshalIndent(conf.Conf, "", " ")
|
2022-01-07 12:29:20 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("marshal config error:%s", err.Error())
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(conf.ConfigFile, confBody, 0777)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("update config struct error: %s", err.Error())
|
|
|
|
}
|
2021-10-27 14:45:36 +00:00
|
|
|
}
|