mirror of
https://github.com/OpenListTeam/OpenList.git
synced 2025-09-19 04:06:18 +08:00
fix(cmd): optimize parse of command flag --data
(#777)
* Fix (cmd): optimize parse of command flag `--data` * DBFile * 优化 * os.Getwd()
This commit is contained in:
@ -15,16 +15,28 @@ import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func InitConfig() {
|
||||
// Program working directory
|
||||
func PWD() string {
|
||||
if flags.ForceBinDir {
|
||||
if !filepath.IsAbs(flags.DataDir) {
|
||||
ex, err := os.Executable()
|
||||
if err != nil {
|
||||
utils.Log.Fatal(err)
|
||||
}
|
||||
exPath := filepath.Dir(ex)
|
||||
flags.DataDir = filepath.Join(exPath, flags.DataDir)
|
||||
ex, err := os.Executable()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
pwd := filepath.Dir(ex)
|
||||
return pwd
|
||||
}
|
||||
d, err := os.Getwd()
|
||||
if err != nil {
|
||||
d = "."
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
func InitConfig() {
|
||||
pwd := PWD()
|
||||
dataDir := flags.DataDir
|
||||
if !filepath.IsAbs(dataDir) {
|
||||
flags.DataDir = filepath.Join(pwd, flags.DataDir)
|
||||
}
|
||||
configPath := filepath.Join(flags.DataDir, "config.json")
|
||||
log.Infof("reading config file: %s", configPath)
|
||||
@ -34,7 +46,7 @@ func InitConfig() {
|
||||
if err != nil {
|
||||
log.Fatalf("failed to create config file: %+v", err)
|
||||
}
|
||||
conf.Conf = conf.DefaultConfig()
|
||||
conf.Conf = conf.DefaultConfig(dataDir)
|
||||
LastLaunchedVersion = conf.Version
|
||||
conf.Conf.LastLaunchedVersion = conf.Version
|
||||
if !utils.WriteJsonToFile(configPath, conf.Conf) {
|
||||
@ -45,7 +57,7 @@ func InitConfig() {
|
||||
if err != nil {
|
||||
log.Fatalf("reading config file error: %+v", err)
|
||||
}
|
||||
conf.Conf = conf.DefaultConfig()
|
||||
conf.Conf = conf.DefaultConfig(dataDir)
|
||||
err = utils.Json.Unmarshal(configBytes, conf.Conf)
|
||||
if err != nil {
|
||||
log.Fatalf("load config error: %+v", err)
|
||||
@ -71,12 +83,17 @@ func InitConfig() {
|
||||
confFromEnv()
|
||||
}
|
||||
// convert abs path
|
||||
if !filepath.IsAbs(conf.Conf.TempDir) {
|
||||
absPath, err := filepath.Abs(conf.Conf.TempDir)
|
||||
if err != nil {
|
||||
log.Fatalf("get abs path error: %+v", err)
|
||||
convertAbsPath := func(path *string) {
|
||||
if !filepath.IsAbs(*path) {
|
||||
*path = filepath.Join(pwd, *path)
|
||||
}
|
||||
conf.Conf.TempDir = absPath
|
||||
}
|
||||
convertAbsPath(&conf.Conf.TempDir)
|
||||
convertAbsPath(&conf.Conf.BleveDir)
|
||||
convertAbsPath(&conf.Conf.Log.Name)
|
||||
convertAbsPath(&conf.Conf.Database.DBFile)
|
||||
if conf.Conf.DistDir != "" {
|
||||
convertAbsPath(&conf.Conf.DistDir)
|
||||
}
|
||||
err := os.MkdirAll(conf.Conf.TempDir, 0o777)
|
||||
if err != nil {
|
||||
|
@ -3,7 +3,6 @@ package conf
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/OpenListTeam/OpenList/v4/cmd/flags"
|
||||
"github.com/OpenListTeam/OpenList/v4/pkg/utils/random"
|
||||
)
|
||||
|
||||
@ -119,11 +118,11 @@ type Config struct {
|
||||
LastLaunchedVersion string `json:"last_launched_version"`
|
||||
}
|
||||
|
||||
func DefaultConfig() *Config {
|
||||
tempDir := filepath.Join(flags.DataDir, "temp")
|
||||
indexDir := filepath.Join(flags.DataDir, "bleve")
|
||||
logPath := filepath.Join(flags.DataDir, "log/log.log")
|
||||
dbPath := filepath.Join(flags.DataDir, "data.db")
|
||||
func DefaultConfig(dataDir string) *Config {
|
||||
tempDir := filepath.Join(dataDir, "temp")
|
||||
indexDir := filepath.Join(dataDir, "bleve")
|
||||
logPath := filepath.Join(dataDir, "log/log.log")
|
||||
dbPath := filepath.Join(dataDir, "data.db")
|
||||
return &Config{
|
||||
Scheme: Scheme{
|
||||
Address: "0.0.0.0",
|
||||
|
@ -19,7 +19,7 @@ func init() {
|
||||
if err != nil {
|
||||
panic("failed to connect database")
|
||||
}
|
||||
conf.Conf = conf.DefaultConfig()
|
||||
conf.Conf = conf.DefaultConfig("data")
|
||||
db.Init(dB)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user