Skip to content

Commit a6bb841

Browse files
committed
config
1 parent abf54ec commit a6bb841

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

cmd/root.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,15 @@ func initConfig(ctx context.Context) error {
6868
return fmt.Errorf("parse config: %w", err)
6969
}
7070

71+
conf, _ = config.EnsureDirs(conf)
7172
if conf.PoolSize <= 0 {
7273
conf.PoolSize = runtime.NumCPU()
7374
}
7475
if conf.StopTimeoutSeconds <= 0 {
7576
conf.StopTimeoutSeconds = 30 //nolint:mnd
7677
}
7778

78-
return log.SetupLog(ctx, &conf.Log, "")
79+
return log.SetupLog(ctx, conf.Log, "")
7980
}
8081

8182
// Execute is the main entry point called from main.go.

config/config.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type Config struct {
3232
// Defaults to runtime.NumCPU() if zero.
3333
PoolSize int `json:"pool_size" mapstructure:"pool_size"`
3434
// Log configuration, uses eru core's ServerLogConfig.
35-
Log coretypes.ServerLogConfig `json:"log" mapstructure:"log"`
35+
Log *coretypes.ServerLogConfig `json:"log" mapstructure:"log"`
3636
}
3737

3838
// DefaultConfig returns a Config with sensible defaults.
@@ -44,7 +44,7 @@ func DefaultConfig() *Config {
4444
CHBinary: "cloud-hypervisor",
4545
StopTimeoutSeconds: 30,
4646
PoolSize: runtime.NumCPU(),
47-
Log: coretypes.ServerLogConfig{
47+
Log: &coretypes.ServerLogConfig{
4848
Level: "info",
4949
MaxSize: 500,
5050
MaxAge: 28,
@@ -78,5 +78,22 @@ func LoadConfig(path string) (*Config, error) {
7878
if conf.StopTimeoutSeconds <= 0 {
7979
conf.StopTimeoutSeconds = 30 //nolint:mnd
8080
}
81+
return EnsureDirs(conf)
82+
}
83+
84+
func EnsureDirs(conf *Config) (*Config, error) {
85+
defaults := DefaultConfig()
86+
if conf.RootDir == "" {
87+
conf.RootDir = defaults.RootDir
88+
}
89+
if conf.RunDir == "" {
90+
conf.RunDir = defaults.RunDir
91+
}
92+
if conf.LogDir == "" {
93+
conf.LogDir = defaults.LogDir
94+
}
95+
if conf.CHBinary == "" {
96+
conf.CHBinary = defaults.CHBinary
97+
}
8198
return conf, nil
8299
}

0 commit comments

Comments
 (0)