-
Notifications
You must be signed in to change notification settings - Fork 291
/
config.go
91 lines (73 loc) · 1.8 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// (C) Copyright 2019-2024 Intel Corporation.
//
// SPDX-License-Identifier: BSD-2-Clause-Patent
//
package main
import (
"os"
"path"
"github.com/daos-stack/daos/src/control/build"
"github.com/daos-stack/daos/src/control/server/config"
)
type cfgLoader interface {
loadConfig() error
configPath() string
configOptional() bool
}
type optionalCfgLoader interface {
setOptional()
}
type cliOverrider interface {
setCLIOverrides() error
}
type cfgCmd struct {
config *config.Server
configIsOptional bool
IgnoreConfig bool `long:"ignore-config" description:"Ignore parameters set in config file when running command"`
ConfigPath string `short:"o" long:"config" description:"Server config file path"`
}
func (c *cfgCmd) configPath() string {
if c.config == nil {
return ""
}
return c.config.Path
}
func (c *cfgCmd) loadConfig() error {
if c.IgnoreConfig {
c.config = nil
return nil
}
// Don't load a new config if there's already one present. If the caller really wants to
// reload, it can do that explicitly.
if c.config != nil {
return nil
}
setInArgs := false
if c.ConfigPath != "" {
setInArgs = true
} else {
// Set config path to build directory if not supplied in command args.
c.ConfigPath = path.Join(build.ConfigDir, defaultConfigFile)
}
c.config = config.DefaultServer()
if err := c.config.SetPath(c.ConfigPath); err != nil {
if os.IsNotExist(err) && c.configOptional() && !setInArgs {
// Situation permitted where for an optCfgCmd -o has not been set and no
// file exists at the default path.
c.ConfigPath = ""
return nil
}
return err
}
return c.config.Load()
}
func (c *cfgCmd) configOptional() bool {
return c.configIsOptional
}
type optCfgCmd struct {
cfgCmd
}
func (oc *optCfgCmd) setOptional() {
oc.configIsOptional = true
}