MCPcopy
hub / github.com/runfinch/finch / Load

Function Load

pkg/config/config.go:135–174  ·  view source on GitHub ↗

Load loads Finch's configuration from a YAML file and initializes default values.

(
	fs afero.Fs,
	cfgPath string,
	log flog.Logger,
	systemDeps LoadSystemDeps,
	mem fmemory.Memory,
	ecc command.Creator,
)

Source from the content-addressed store, hash-verified

133
134// Load loads Finch's configuration from a YAML file and initializes default values.
135func Load(
136 fs afero.Fs,
137 cfgPath string,
138 log flog.Logger,
139 systemDeps LoadSystemDeps,
140 mem fmemory.Memory,
141 ecc command.Creator,
142) (*Finch, error) {
143 b, err := afero.ReadFile(fs, cfgPath)
144 if err != nil {
145 if errors.Is(err, afero.ErrFileNotFound) {
146 log.Infof("Using default values due to missing config file at %q", cfgPath)
147 defCfg := applyDefaults(&Finch{}, systemDeps, mem, ecc)
148 if err := ensureConfigDir(fs, filepath.Dir(cfgPath), log); err != nil {
149 return nil, fmt.Errorf("failed to ensure %q directory: %w", cfgPath, err)
150 }
151 if err := writeConfig(defCfg, fs, cfgPath); err != nil {
152 log.Warnf("Could not save default values to %q: %w", cfgPath, err)
153 }
154 return defCfg, nil
155 }
156 return nil, fmt.Errorf("failed to read the config file: %w", err)
157 }
158
159 var cfg Finch
160 if err := yaml.Unmarshal(b, &cfg); err != nil {
161 return nil, fmt.Errorf("failed to unmarshal config file: %w", err)
162 }
163
164 defCfg := applyDefaults(&cfg, systemDeps, mem, ecc)
165 if err := writeConfig(defCfg, fs, cfgPath); err != nil {
166 return nil, err
167 }
168
169 if err := validate(defCfg, log, systemDeps, mem); err != nil {
170 return nil, fmt.Errorf("failed to validate config file: %w", err)
171 }
172
173 return defCfg, nil
174}

Callers 3

xmainFunction · 0.92
xmainFunction · 0.92
TestLoadFunction · 0.85

Calls 7

ensureConfigDirFunction · 0.85
writeConfigFunction · 0.85
applyDefaultsFunction · 0.70
validateFunction · 0.70
InfofMethod · 0.65
ErrorfMethod · 0.65
WarnfMethod · 0.65

Tested by 1

TestLoadFunction · 0.68