refactor: flatten nested config and tool fallback logic

This commit is contained in:
sam committed 2026-08-16 01:20:48 +10:00
1 parent 79a4db5362
commit 63cff4fe53
3 files changed
+107 -83

No files matched your search

+23 -18
View File
@@ -80,30 +80,35 @@ func (l *Loader) Resolve(basePath, overlayPath string) (*Document, error) {
if err != nil {
return nil, fmt.Errorf("%w: config source %s: %w", ErrUnreadable, basePath, err)
}
if err := validateOverlayPath(overlayPath, basePath, info.IsDir()); err != nil {
return nil, err
}
if info.IsDir() {
if overlayPath != "" {
overlayInfo, statErr := os.Stat(overlayPath)
if statErr != nil {
return nil, fmt.Errorf("%w: config overlay %s: %w", ErrUnreadable, overlayPath, statErr)
}
if !overlayInfo.IsDir() {
return nil, fmt.Errorf("config overlay %s must be a directory when config source %s is a directory", overlayPath, basePath)
}
}
return l.Dir(basePath, overlayPath)
}
if overlayPath != "" {
overlayInfo, statErr := os.Stat(overlayPath)
if statErr != nil {
return nil, fmt.Errorf("%w: config overlay %s: %w", ErrUnreadable, overlayPath, statErr)
}
if overlayInfo.IsDir() {
return nil, fmt.Errorf("config overlay %s must be a file when config source %s is a file", overlayPath, basePath)
}
}
return l.overlayFile(basePath, overlayPath)
}
// validateOverlayPath checks that overlayPath, when non-empty, exists and has
// the same filesystem kind as basePath: a directory overlay for a directory
// source and a file overlay for a file source.
func validateOverlayPath(overlayPath, basePath string, baseIsDir bool) error {
if overlayPath == "" {
return nil
}
overlayInfo, err := os.Stat(overlayPath)
if err != nil {
return fmt.Errorf("%w: config overlay %s: %w", ErrUnreadable, overlayPath, err)
}
if overlayInfo.IsDir() != baseIsDir {
if baseIsDir {
return fmt.Errorf("config overlay %s must be a directory when config source %s is a directory", overlayPath, basePath)
}
return fmt.Errorf("config overlay %s must be a file when config source %s is a file", overlayPath, basePath)
}
return nil
}
// Overlay loads basePath, then decodes overlayPath into the same value so
// only the fields the overlay sets are replaced -- every field it omits keeps
// the base value. This lets a deployment-specific file (config.docker.toml)