Skip to content

Commit a20c202

Browse files
authored
fix(cmd): optimize parse of command flag --data (#777)
* Fix (cmd): optimize parse of command flag `--data` * DBFile * 优化 * os.Getwd()
1 parent a92b5eb commit a20c202

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

internal/bootstrap/config.go

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,28 @@ import (
1515
log "github.com/sirupsen/logrus"
1616
)
1717

18-
func InitConfig() {
18+
// Program working directory
19+
func PWD() string {
1920
if flags.ForceBinDir {
20-
if !filepath.IsAbs(flags.DataDir) {
21-
ex, err := os.Executable()
22-
if err != nil {
23-
utils.Log.Fatal(err)
24-
}
25-
exPath := filepath.Dir(ex)
26-
flags.DataDir = filepath.Join(exPath, flags.DataDir)
21+
ex, err := os.Executable()
22+
if err != nil {
23+
log.Fatal(err)
2724
}
25+
pwd := filepath.Dir(ex)
26+
return pwd
27+
}
28+
d, err := os.Getwd()
29+
if err != nil {
30+
d = "."
31+
}
32+
return d
33+
}
34+
35+
func InitConfig() {
36+
pwd := PWD()
37+
dataDir := flags.DataDir
38+
if !filepath.IsAbs(dataDir) {
39+
flags.DataDir = filepath.Join(pwd, flags.DataDir)
2840
}
2941
configPath := filepath.Join(flags.DataDir, "config.json")
3042
log.Infof("reading config file: %s", configPath)
@@ -34,7 +46,7 @@ func InitConfig() {
3446
if err != nil {
3547
log.Fatalf("failed to create config file: %+v", err)
3648
}
37-
conf.Conf = conf.DefaultConfig()
49+
conf.Conf = conf.DefaultConfig(dataDir)
3850
LastLaunchedVersion = conf.Version
3951
conf.Conf.LastLaunchedVersion = conf.Version
4052
if !utils.WriteJsonToFile(configPath, conf.Conf) {
@@ -45,7 +57,7 @@ func InitConfig() {
4557
if err != nil {
4658
log.Fatalf("reading config file error: %+v", err)
4759
}
48-
conf.Conf = conf.DefaultConfig()
60+
conf.Conf = conf.DefaultConfig(dataDir)
4961
err = utils.Json.Unmarshal(configBytes, conf.Conf)
5062
if err != nil {
5163
log.Fatalf("load config error: %+v", err)
@@ -71,12 +83,17 @@ func InitConfig() {
7183
confFromEnv()
7284
}
7385
// convert abs path
74-
if !filepath.IsAbs(conf.Conf.TempDir) {
75-
absPath, err := filepath.Abs(conf.Conf.TempDir)
76-
if err != nil {
77-
log.Fatalf("get abs path error: %+v", err)
86+
convertAbsPath := func(path *string) {
87+
if !filepath.IsAbs(*path) {
88+
*path = filepath.Join(pwd, *path)
7889
}
79-
conf.Conf.TempDir = absPath
90+
}
91+
convertAbsPath(&conf.Conf.TempDir)
92+
convertAbsPath(&conf.Conf.BleveDir)
93+
convertAbsPath(&conf.Conf.Log.Name)
94+
convertAbsPath(&conf.Conf.Database.DBFile)
95+
if conf.Conf.DistDir != "" {
96+
convertAbsPath(&conf.Conf.DistDir)
8097
}
8198
err := os.MkdirAll(conf.Conf.TempDir, 0o777)
8299
if err != nil {

internal/conf/config.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package conf
33
import (
44
"path/filepath"
55

6-
"github.com/OpenListTeam/OpenList/v4/cmd/flags"
76
"github.com/OpenListTeam/OpenList/v4/pkg/utils/random"
87
)
98

@@ -119,11 +118,11 @@ type Config struct {
119118
LastLaunchedVersion string `json:"last_launched_version"`
120119
}
121120

122-
func DefaultConfig() *Config {
123-
tempDir := filepath.Join(flags.DataDir, "temp")
124-
indexDir := filepath.Join(flags.DataDir, "bleve")
125-
logPath := filepath.Join(flags.DataDir, "log/log.log")
126-
dbPath := filepath.Join(flags.DataDir, "data.db")
121+
func DefaultConfig(dataDir string) *Config {
122+
tempDir := filepath.Join(dataDir, "temp")
123+
indexDir := filepath.Join(dataDir, "bleve")
124+
logPath := filepath.Join(dataDir, "log/log.log")
125+
dbPath := filepath.Join(dataDir, "data.db")
127126
return &Config{
128127
Scheme: Scheme{
129128
Address: "0.0.0.0",

internal/op/storage_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func init() {
1919
if err != nil {
2020
panic("failed to connect database")
2121
}
22-
conf.Conf = conf.DefaultConfig()
22+
conf.Conf = conf.DefaultConfig("data")
2323
db.Init(dB)
2424
}
2525

0 commit comments

Comments
 (0)