🎇 创建默认配置文件

This commit is contained in:
微凉
2021-05-26 19:14:58 +08:00
parent 3827d3ed67
commit a6bbff2199

View File

@ -1,13 +1,11 @@
package bootstrap package bootstrap
import ( import (
"bufio"
"github.com/Xhofe/alist/conf" "github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/utils" "github.com/Xhofe/alist/utils"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"io/ioutil" "io/ioutil"
"os"
"strings" "strings"
) )
@ -16,8 +14,9 @@ func ReadConf(config string) bool {
log.Infof("读取配置文件...") log.Infof("读取配置文件...")
if !utils.Exists(config) { if !utils.Exists(config) {
log.Infof("找不到配置文件:%s", config) log.Infof("找不到配置文件:%s", config)
Write() if !Write(config) {
return false return false
}
} }
confFile, err := ioutil.ReadFile(config) confFile, err := ioutil.ReadFile(config)
if err != nil { if err != nil {
@ -34,15 +33,16 @@ func ReadConf(config string) bool {
conf.Origins = strings.Split(conf.Conf.Server.SiteUrl, ",") conf.Origins = strings.Split(conf.Conf.Server.SiteUrl, ",")
return true return true
} }
func Write() { func Write(path string) bool {
log.Infof("创建默认配置文件") log.Infof("创建默认配置文件")
filePath :="conf.yml" file, err := utils.CreatNestedFile(path)
file,err :=os.OpenFile(filePath,os.O_WRONLY | os.O_CREATE,0644)
if err != nil { if err != nil {
log.Infof("文件创建失败...") log.Errorf("无法创建配置文件, %s", err)
return return false
} }
defer file.Close() defer func() {
_ = file.Close()
}()
str := ` str := `
info: info:
title: AList #标题 title: AList #标题
@ -75,7 +75,10 @@ database:
type: sqlite3 type: sqlite3
dBFile: alist.db dBFile: alist.db
` `
writer := bufio.NewWriter(file) _, err = file.WriteString(str)
writer.WriteString(str) if err != nil {
writer.Flush() log.Errorf("无法写入配置文件, %s", err)
return false
}
return true
} }