mirror of https://github.com/Xhofe/alist
				
				
				
			
		
			
				
	
	
		
			91 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Go
		
	
	
			
		
		
	
	
			91 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Go
		
	
	
| package bootstrap
 | |
| 
 | |
| import (
 | |
| 	"os"
 | |
| 	"path/filepath"
 | |
| 
 | |
| 	"github.com/alist-org/alist/v3/cmd/flags"
 | |
| 	"github.com/alist-org/alist/v3/internal/conf"
 | |
| 	"github.com/alist-org/alist/v3/pkg/utils"
 | |
| 	"github.com/caarlos0/env/v6"
 | |
| 	log "github.com/sirupsen/logrus"
 | |
| )
 | |
| 
 | |
| func InitConfig() {
 | |
| 	if flags.ForceBinDir {
 | |
| 		if !filepath.IsAbs(flags.DataDir) {
 | |
| 			ex, err := os.Executable()
 | |
| 			if err != nil {
 | |
| 				utils.Log.Fatal(err)
 | |
| 			}
 | |
| 			exPath := filepath.Dir(ex)
 | |
| 			flags.DataDir = filepath.Join(exPath, flags.DataDir)
 | |
| 		}
 | |
| 	}
 | |
| 	configPath := filepath.Join(flags.DataDir, "config.json")
 | |
| 	log.Infof("reading config file: %s", configPath)
 | |
| 	if !utils.Exists(configPath) {
 | |
| 		log.Infof("config file not exists, creating default config file")
 | |
| 		_, err := utils.CreateNestedFile(configPath)
 | |
| 		if err != nil {
 | |
| 			log.Fatalf("failed to create config file: %+v", err)
 | |
| 		}
 | |
| 		conf.Conf = conf.DefaultConfig()
 | |
| 		if !utils.WriteJsonToFile(configPath, conf.Conf) {
 | |
| 			log.Fatalf("failed to create default config file")
 | |
| 		}
 | |
| 	} else {
 | |
| 		configBytes, err := os.ReadFile(configPath)
 | |
| 		if err != nil {
 | |
| 			log.Fatalf("reading config file error: %+v", err)
 | |
| 		}
 | |
| 		conf.Conf = conf.DefaultConfig()
 | |
| 		err = utils.Json.Unmarshal(configBytes, conf.Conf)
 | |
| 		if err != nil {
 | |
| 			log.Fatalf("load config error: %+v", err)
 | |
| 		}
 | |
| 		// update config.json struct
 | |
| 		confBody, err := utils.Json.MarshalIndent(conf.Conf, "", "  ")
 | |
| 		if err != nil {
 | |
| 			log.Fatalf("marshal config error: %+v", err)
 | |
| 		}
 | |
| 		err = os.WriteFile(configPath, confBody, 0777)
 | |
| 		if err != nil {
 | |
| 			log.Fatalf("update config struct error: %+v", err)
 | |
| 		}
 | |
| 	}
 | |
| 	if !conf.Conf.Force {
 | |
| 		confFromEnv()
 | |
| 	}
 | |
| 	// convert abs path
 | |
| 	if !filepath.IsAbs(conf.Conf.TempDir) {
 | |
| 		absPath, err := filepath.Abs(conf.Conf.TempDir)
 | |
| 		if err != nil {
 | |
| 			log.Fatalf("get abs path error: %+v", err)
 | |
| 		}
 | |
| 		conf.Conf.TempDir = absPath
 | |
| 	}
 | |
| 	err := os.RemoveAll(filepath.Join(conf.Conf.TempDir))
 | |
| 	if err != nil {
 | |
| 		log.Errorln("failed delete temp file:", err)
 | |
| 	}
 | |
| 	err = os.MkdirAll(conf.Conf.TempDir, 0777)
 | |
| 	if err != nil {
 | |
| 		log.Fatalf("create temp dir error: %+v", err)
 | |
| 	}
 | |
| 	log.Debugf("config: %+v", conf.Conf)
 | |
| }
 | |
| 
 | |
| func confFromEnv() {
 | |
| 	prefix := "ALIST_"
 | |
| 	if flags.NoPrefix {
 | |
| 		prefix = ""
 | |
| 	}
 | |
| 	log.Infof("load config from env with prefix: %s", prefix)
 | |
| 	if err := env.Parse(conf.Conf, env.Options{
 | |
| 		Prefix: prefix,
 | |
| 	}); err != nil {
 | |
| 		log.Fatalf("load config from env error: %+v", err)
 | |
| 	}
 | |
| }
 |