mirror of https://github.com/Xhofe/alist
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
parent
141419056d
commit
1c212f6c30
|
@ -1,8 +1,9 @@
|
|||
package flags
|
||||
|
||||
var (
|
||||
Config string // config file
|
||||
DataDir string
|
||||
Debug bool
|
||||
NoPrefix bool
|
||||
Dev bool
|
||||
ForceBinDir bool
|
||||
)
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue