Skip to content

Commit 525f26d

Browse files
authored
feat(command): add --config flag to set custom config path (#1479)
1 parent a0fcfa3 commit 525f26d

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

cmd/flags/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package flags
22

33
var (
44
DataDir string
5+
ConfigPath string
56
Debug bool
67
NoPrefix bool
78
Dev bool

cmd/root.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ func Execute() {
2727
}
2828

2929
func init() {
30-
RootCmd.PersistentFlags().StringVar(&flags.DataDir, "data", "data", "data folder")
30+
RootCmd.PersistentFlags().StringVar(&flags.DataDir, "data", "data", "data directory (relative paths are resolved against the current working directory)")
31+
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)")
3132
RootCmd.PersistentFlags().BoolVar(&flags.Debug, "debug", false, "start with debug mode")
3233
RootCmd.PersistentFlags().BoolVar(&flags.NoPrefix, "no-prefix", false, "disable env prefix")
3334
RootCmd.PersistentFlags().BoolVar(&flags.Dev, "dev", false, "start with dev mode")

internal/bootstrap/config.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,21 @@ func InitConfig() {
3939
if !filepath.IsAbs(dataDir) {
4040
flags.DataDir = filepath.Join(pwd, flags.DataDir)
4141
}
42-
configPath := filepath.Join(flags.DataDir, "config.json")
42+
// Determine config file path: use flags.ConfigPath if provided, otherwise default to <dataDir>/config.json
43+
configPath := flags.ConfigPath
44+
if configPath == "" {
45+
configPath = filepath.Join(flags.DataDir, "config.json")
46+
} else {
47+
// if relative, resolve relative to working directory
48+
if !filepath.IsAbs(configPath) {
49+
if absPath, err := filepath.Abs(configPath); err == nil {
50+
configPath = absPath
51+
} else {
52+
configPath = filepath.Join(pwd, configPath)
53+
}
54+
}
55+
}
56+
configPath = filepath.Clean(configPath)
4357
log.Infof("reading config file: %s", configPath)
4458
if !utils.Exists(configPath) {
4559
log.Infof("config file not exists, creating default config file")

0 commit comments

Comments
 (0)