Skip to content

Commit 1c212f6

Browse files
committed
feat!: force to use the bin dir as the data dir (close AlistGo#2108)
- move default log path to `data/log/log.log` - replace `--conf` with `--data`
1 parent 1414190 commit 1c212f6

4 files changed

Lines changed: 31 additions & 14 deletions

File tree

cmd/flags/config.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package flags
22

33
var (
4-
Config string // config file
5-
Debug bool
6-
NoPrefix bool
7-
Dev bool
4+
DataDir string
5+
Debug bool
6+
NoPrefix bool
7+
Dev bool
8+
ForceBinDir bool
89
)

cmd/root.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ func Execute() {
2424
}
2525

2626
func init() {
27-
rootCmd.PersistentFlags().StringVar(&flags.Config, "conf", "data/config.json", "config file")
27+
rootCmd.PersistentFlags().StringVar(&flags.DataDir, "data", "data", "config file")
2828
rootCmd.PersistentFlags().BoolVar(&flags.Debug, "debug", false, "start with debug mode")
2929
rootCmd.PersistentFlags().BoolVar(&flags.NoPrefix, "no-prefix", false, "disable env prefix")
3030
rootCmd.PersistentFlags().BoolVar(&flags.Dev, "dev", false, "start with dev mode")
31+
rootCmd.PersistentFlags().BoolVar(&flags.ForceBinDir, "force-bin-dir", false, "Force to use the directory where the binary file is located as data directory")
3132
}

internal/bootstrap/config.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,28 @@ import (
1212
)
1313

1414
func InitConfig() {
15-
log.Infof("reading config file: %s", flags.Config)
16-
if !utils.Exists(flags.Config) {
15+
if flags.ForceBinDir {
16+
ex, err := os.Executable()
17+
if err != nil {
18+
utils.Log.Fatal(err)
19+
}
20+
exPath := filepath.Dir(ex)
21+
flags.DataDir = filepath.Join(exPath, "data")
22+
}
23+
configPath := filepath.Join(flags.DataDir, "config.json")
24+
log.Infof("reading config file: %s", configPath)
25+
if !utils.Exists(configPath) {
1726
log.Infof("config file not exists, creating default config file")
18-
_, err := utils.CreateNestedFile(flags.Config)
27+
_, err := utils.CreateNestedFile(configPath)
1928
if err != nil {
2029
log.Fatalf("failed to create config file: %+v", err)
2130
}
2231
conf.Conf = conf.DefaultConfig()
23-
if !utils.WriteJsonToFile(flags.Config, conf.Conf) {
32+
if !utils.WriteJsonToFile(configPath, conf.Conf) {
2433
log.Fatalf("failed to create default config file")
2534
}
2635
} else {
27-
configBytes, err := os.ReadFile(flags.Config)
36+
configBytes, err := os.ReadFile(configPath)
2837
if err != nil {
2938
log.Fatalf("reading config file error: %+v", err)
3039
}
@@ -38,7 +47,7 @@ func InitConfig() {
3847
if err != nil {
3948
log.Fatalf("marshal config error: %+v", err)
4049
}
41-
err = os.WriteFile(flags.Config, confBody, 0777)
50+
err = os.WriteFile(configPath, confBody, 0777)
4251
if err != nil {
4352
log.Fatalf("update config struct error: %+v", err)
4453
}

internal/conf/config.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package conf
22

33
import (
4+
"path/filepath"
5+
6+
"github.com/alist-org/alist/v3/cmd/flags"
47
"github.com/alist-org/alist/v3/pkg/utils/random"
58
)
69

@@ -46,21 +49,24 @@ type Config struct {
4649
}
4750

4851
func DefaultConfig() *Config {
52+
tempDir := filepath.Join(flags.DataDir, "temp")
53+
logPath := filepath.Join(flags.DataDir, "log/log.log")
54+
dbPath := filepath.Join(flags.DataDir, "data.db")
4955
return &Config{
5056
Address: "0.0.0.0",
5157
Port: 5244,
5258
JwtSecret: random.String(16),
5359
TokenExpiresIn: 48,
54-
TempDir: "data/temp",
60+
TempDir: tempDir,
5561
Database: Database{
5662
Type: "sqlite3",
5763
Port: 0,
5864
TablePrefix: "x_",
59-
DBFile: "data/data.db",
65+
DBFile: dbPath,
6066
},
6167
Log: LogConfig{
6268
Enable: true,
63-
Name: "log/log.log",
69+
Name: logPath,
6470
MaxSize: 10,
6571
MaxBackups: 5,
6672
MaxAge: 28,

0 commit comments

Comments
 (0)