feat!: force to use the bin dir as the data dir (close #2108)

- move default log path to `data/log/log.log`
- replace `--conf` with `--data`
pull/2206/head
Noah Hsu 2022-11-01 19:14:49 +08:00
parent 141419056d
commit 1c212f6c30
4 changed files with 31 additions and 14 deletions

View File

@ -1,8 +1,9 @@
package flags package flags
var ( var (
Config string // config file DataDir string
Debug bool Debug bool
NoPrefix bool NoPrefix bool
Dev bool Dev bool
ForceBinDir bool
) )

View File

@ -24,8 +24,9 @@ func Execute() {
} }
func init() { 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.Debug, "debug", false, "start with debug mode")
rootCmd.PersistentFlags().BoolVar(&flags.NoPrefix, "no-prefix", false, "disable env prefix") 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.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")
} }

View File

@ -12,19 +12,28 @@ import (
) )
func InitConfig() { func InitConfig() {
log.Infof("reading config file: %s", flags.Config) if flags.ForceBinDir {
if !utils.Exists(flags.Config) { 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") log.Infof("config file not exists, creating default config file")
_, err := utils.CreateNestedFile(flags.Config) _, err := utils.CreateNestedFile(configPath)
if err != nil { if err != nil {
log.Fatalf("failed to create config file: %+v", err) log.Fatalf("failed to create config file: %+v", err)
} }
conf.Conf = conf.DefaultConfig() 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") log.Fatalf("failed to create default config file")
} }
} else { } else {
configBytes, err := os.ReadFile(flags.Config) configBytes, err := os.ReadFile(configPath)
if err != nil { if err != nil {
log.Fatalf("reading config file error: %+v", err) log.Fatalf("reading config file error: %+v", err)
} }
@ -38,7 +47,7 @@ func InitConfig() {
if err != nil { if err != nil {
log.Fatalf("marshal config error: %+v", err) log.Fatalf("marshal config error: %+v", err)
} }
err = os.WriteFile(flags.Config, confBody, 0777) err = os.WriteFile(configPath, confBody, 0777)
if err != nil { if err != nil {
log.Fatalf("update config struct error: %+v", err) log.Fatalf("update config struct error: %+v", err)
} }

View File

@ -1,6 +1,9 @@
package conf package conf
import ( import (
"path/filepath"
"github.com/alist-org/alist/v3/cmd/flags"
"github.com/alist-org/alist/v3/pkg/utils/random" "github.com/alist-org/alist/v3/pkg/utils/random"
) )
@ -46,21 +49,24 @@ type Config struct {
} }
func DefaultConfig() *Config { 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{ return &Config{
Address: "0.0.0.0", Address: "0.0.0.0",
Port: 5244, Port: 5244,
JwtSecret: random.String(16), JwtSecret: random.String(16),
TokenExpiresIn: 48, TokenExpiresIn: 48,
TempDir: "data/temp", TempDir: tempDir,
Database: Database{ Database: Database{
Type: "sqlite3", Type: "sqlite3",
Port: 0, Port: 0,
TablePrefix: "x_", TablePrefix: "x_",
DBFile: "data/data.db", DBFile: dbPath,
}, },
Log: LogConfig{ Log: LogConfig{
Enable: true, Enable: true,
Name: "log/log.log", Name: logPath,
MaxSize: 10, MaxSize: 10,
MaxBackups: 5, MaxBackups: 5,
MaxAge: 28, MaxAge: 28,