MCPcopy
hub / github.com/ayn2op/discordo / Load

Function Load

internal/config/config.go:149–172  ·  view source on GitHub ↗

Load reads the configuration file and parses it.

(path string)

Source from the content-addressed store, hash-verified

147
148// Load reads the configuration file and parses it.
149func Load(path string) (*Config, error) {
150 cfg := Config{
151 Keybinds: defaultKeybinds(),
152 }
153 if err := toml.Unmarshal(defaultCfg, &cfg); err != nil {
154 return nil, fmt.Errorf("failed to unmarshal default config: %w", err)
155 }
156
157 file, err := os.Open(path)
158 switch {
159 case os.IsNotExist(err):
160 slog.Info("config file does not exist, falling back to the default config", "path", path, "err", err)
161 case err != nil:
162 return nil, fmt.Errorf("failed to open config file: %w", err)
163 default:
164 defer file.Close()
165 if _, err := toml.NewDecoder(file).Decode(&cfg); err != nil {
166 return nil, fmt.Errorf("failed to decode config: %w", err)
167 }
168 }
169
170 applyDefaults(&cfg)
171 return &cfg, nil
172}
173
174func applyDefaults(cfg *Config) {
175 if cfg.Editor == "default" {

Callers 2

RunFunction · 0.92
TestLoadFunction · 0.70

Calls 2

defaultKeybindsFunction · 0.85
applyDefaultsFunction · 0.85

Tested by 1

TestLoadFunction · 0.56