-
Notifications
You must be signed in to change notification settings - Fork 291
/
config.go
102 lines (87 loc) · 2.74 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
92
93
94
95
96
97
98
99
100
101
102
//
// (C) Copyright 2020-2021 Intel Corporation.
//
// SPDX-License-Identifier: BSD-2-Clause-Patent
//
package control
import (
"fmt"
"io/ioutil"
"os"
"path"
"gopkg.in/yaml.v2"
"github.com/daos-stack/daos/src/control/build"
"github.com/daos-stack/daos/src/control/lib/daos"
"github.com/daos-stack/daos/src/control/security"
)
const (
defaultConfigFile = "daos_control.yml"
)
// Config defines the parameters used to connect to a control API server.
type Config struct {
SystemName string `yaml:"name"`
ControlPort int `yaml:"port"`
HostList []string `yaml:"hostlist"`
TransportConfig *security.TransportConfig `yaml:"transport_config"`
Path string `yaml:"-"`
}
// DefaultConfig returns a Config populated with default values. Only
// suitable for single-node configurations.
func DefaultConfig() *Config {
localServer := fmt.Sprintf("localhost:%d", build.DefaultControlPort)
return &Config{
SystemName: build.DefaultSystemName,
ControlPort: build.DefaultControlPort,
HostList: []string{localServer},
TransportConfig: security.DefaultClientTransportConfig(),
}
}
// UserConfigPath returns the computed path to a per-user
// control configuration file, if it exists.
func UserConfigPath() string {
// If we can't determine $HOME it's weird but not fatal.
userHome, _ := os.UserHomeDir()
return path.Join(userHome, "."+defaultConfigFile)
}
// SystemConfigPath returns the computed path to the system
// control configuration file, if it exists.
func SystemConfigPath() string {
return path.Join(build.ConfigDir, defaultConfigFile)
}
// LoadConfig attempts to load a configuration by one of the following:
// 1. If the supplied path is a non-empty string, use it.
// Otherwise,
// 2. Try to load the config from the current user's home directory.
// 3. Finally, try to load the config from the system location.
func LoadConfig(cfgPath string) (*Config, error) {
if cfgPath == "" {
// Try to find either a per-user config file or use
// the system config file.
for _, cp := range []string{UserConfigPath(), SystemConfigPath()} {
if _, err := os.Stat(cp); err == nil {
cfgPath = cp
break
}
}
}
// If we still don't have a config file, return an error.
if cfgPath == "" {
return nil, ErrNoConfigFile
}
data, err := ioutil.ReadFile(cfgPath)
if err != nil {
return nil, err
}
if len(data) == 0 {
return nil, fmt.Errorf("empty config file: %s", cfgPath)
}
cfg := DefaultConfig()
if err := yaml.UnmarshalStrict(data, cfg); err != nil {
return nil, err
}
cfg.Path = cfgPath
if !daos.SystemNameIsValid(cfg.SystemName) {
return nil, fmt.Errorf("invalid system name: %q", cfg.SystemName)
}
return cfg, nil
}