diff --git a/cmd/flags/config.go b/cmd/flags/config.go index 01161b18..5a790af2 100644 --- a/cmd/flags/config.go +++ b/cmd/flags/config.go @@ -1,8 +1,9 @@ package flags var ( - Config string // config file - Debug bool - NoPrefix bool - Dev bool + DataDir string + Debug bool + NoPrefix bool + Dev bool + ForceBinDir bool ) diff --git a/cmd/root.go b/cmd/root.go index 5172fa41..d922500b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -24,8 +24,9 @@ func Execute() { } func init() { - rootCmd.PersistentFlags().StringVar(&flags.Config, "conf", "data/config.json", "config file") + rootCmd.PersistentFlags().StringVar(&flags.DataDir, "data", "data", "config file") rootCmd.PersistentFlags().BoolVar(&flags.Debug, "debug", false, "start with debug mode") rootCmd.PersistentFlags().BoolVar(&flags.NoPrefix, "no-prefix", false, "disable env prefix") rootCmd.PersistentFlags().BoolVar(&flags.Dev, "dev", false, "start with dev mode") + rootCmd.PersistentFlags().BoolVar(&flags.ForceBinDir, "force-bin-dir", false, "Force to use the directory where the binary file is located as data directory") } diff --git a/internal/bootstrap/config.go b/internal/bootstrap/config.go index 930105ff..134066a2 100644 --- a/internal/bootstrap/config.go +++ b/internal/bootstrap/config.go @@ -12,19 +12,28 @@ import ( ) func InitConfig() { - log.Infof("reading config file: %s", flags.Config) - if !utils.Exists(flags.Config) { + if flags.ForceBinDir { + ex, err := os.Executable() + if err != nil { + utils.Log.Fatal(err) + } + exPath := filepath.Dir(ex) + flags.DataDir = filepath.Join(exPath, "data") + } + 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(flags.Config) + _, err := utils.CreateNestedFile(configPath) if err != nil { log.Fatalf("failed to create config file: %+v", err) } conf.Conf = conf.DefaultConfig() - if !utils.WriteJsonToFile(flags.Config, conf.Conf) { + if !utils.WriteJsonToFile(configPath, conf.Conf) { log.Fatalf("failed to create default config file") } } else { - configBytes, err := os.ReadFile(flags.Config) + configBytes, err := os.ReadFile(configPath) if err != nil { log.Fatalf("reading config file error: %+v", err) } @@ -38,7 +47,7 @@ func InitConfig() { if err != nil { log.Fatalf("marshal config error: %+v", err) } - err = os.WriteFile(flags.Config, confBody, 0777) + err = os.WriteFile(configPath, confBody, 0777) if err != nil { log.Fatalf("update config struct error: %+v", err) } diff --git a/internal/conf/config.go b/internal/conf/config.go index 932300ff..aa36dd9d 100644 --- a/internal/conf/config.go +++ b/internal/conf/config.go @@ -1,6 +1,9 @@ package conf import ( + "path/filepath" + + "github.com/alist-org/alist/v3/cmd/flags" "github.com/alist-org/alist/v3/pkg/utils/random" ) @@ -46,21 +49,24 @@ type Config struct { } func DefaultConfig() *Config { + tempDir := filepath.Join(flags.DataDir, "temp") + logPath := filepath.Join(flags.DataDir, "log/log.log") + dbPath := filepath.Join(flags.DataDir, "data.db") return &Config{ Address: "0.0.0.0", Port: 5244, JwtSecret: random.String(16), TokenExpiresIn: 48, - TempDir: "data/temp", + TempDir: tempDir, Database: Database{ Type: "sqlite3", Port: 0, TablePrefix: "x_", - DBFile: "data/data.db", + DBFile: dbPath, }, Log: LogConfig{ Enable: true, - Name: "log/log.log", + Name: logPath, MaxSize: 10, MaxBackups: 5, MaxAge: 28,