mirror of
https://github.com/OpenListTeam/OpenList.git
synced 2025-11-25 03:15:19 +08:00
feat(command): add --config flag to set custom config path (#1479)
This commit is contained in:
@@ -2,6 +2,7 @@ package flags
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
DataDir string
|
DataDir string
|
||||||
|
ConfigPath string
|
||||||
Debug bool
|
Debug bool
|
||||||
NoPrefix bool
|
NoPrefix bool
|
||||||
Dev bool
|
Dev bool
|
||||||
|
|||||||
@@ -27,7 +27,8 @@ func Execute() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RootCmd.PersistentFlags().StringVar(&flags.DataDir, "data", "data", "data folder")
|
RootCmd.PersistentFlags().StringVar(&flags.DataDir, "data", "data", "data directory (relative paths are resolved against the current working directory)")
|
||||||
|
RootCmd.PersistentFlags().StringVar(&flags.ConfigPath, "config", "", "path to config.json (relative to current working directory; defaults to [data directory]/config.json, where [data directory] is set by --data)")
|
||||||
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")
|
||||||
|
|||||||
@@ -39,7 +39,21 @@ func InitConfig() {
|
|||||||
if !filepath.IsAbs(dataDir) {
|
if !filepath.IsAbs(dataDir) {
|
||||||
flags.DataDir = filepath.Join(pwd, flags.DataDir)
|
flags.DataDir = filepath.Join(pwd, flags.DataDir)
|
||||||
}
|
}
|
||||||
configPath := filepath.Join(flags.DataDir, "config.json")
|
// Determine config file path: use flags.ConfigPath if provided, otherwise default to <dataDir>/config.json
|
||||||
|
configPath := flags.ConfigPath
|
||||||
|
if configPath == "" {
|
||||||
|
configPath = filepath.Join(flags.DataDir, "config.json")
|
||||||
|
} else {
|
||||||
|
// if relative, resolve relative to working directory
|
||||||
|
if !filepath.IsAbs(configPath) {
|
||||||
|
if absPath, err := filepath.Abs(configPath); err == nil {
|
||||||
|
configPath = absPath
|
||||||
|
} else {
|
||||||
|
configPath = filepath.Join(pwd, configPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
configPath = filepath.Clean(configPath)
|
||||||
log.Infof("reading config file: %s", configPath)
|
log.Infof("reading config file: %s", configPath)
|
||||||
if !utils.Exists(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")
|
||||||
|
|||||||
Reference in New Issue
Block a user