46 lines
1.4 KiB
Go
46 lines
1.4 KiB
Go
|
|
package config
|
||
|
|
|
||
|
|
import (
|
||
|
|
"path/filepath"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
configkit "go.maximumdirect.net/configkit"
|
||
|
|
)
|
||
|
|
|
||
|
|
// resourceBase chooses the effective host source, falling back to the main
|
||
|
|
// source and then the captured invocation directory for contextless inputs.
|
||
|
|
func resourceBase(resolution *configkit.Resolution, path configkit.Path, invocationDir string) string {
|
||
|
|
info, found := resolution.EntryInfo(path)
|
||
|
|
if found && info.Origin.Layer == configkit.LayerCLI {
|
||
|
|
return invocationDir
|
||
|
|
}
|
||
|
|
if found && len(info.Origin.Locations) == 1 {
|
||
|
|
if base, ok := hostSourceBase(resolution, info.Origin.Locations[0].SourceID); ok {
|
||
|
|
return base
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if base, ok := hostSourceBase(resolution, "source-1"); ok {
|
||
|
|
return base
|
||
|
|
}
|
||
|
|
return invocationDir
|
||
|
|
}
|
||
|
|
|
||
|
|
func hostSourceBase(resolution *configkit.Resolution, sourceID string) (string, bool) {
|
||
|
|
source, ok := resolution.Source(sourceID)
|
||
|
|
if !ok || source.Kind != configkit.SourceKindFile || !filepath.IsAbs(source.Path) {
|
||
|
|
return "", false
|
||
|
|
}
|
||
|
|
if _, ok := resolution.SourceContext(sourceID); !ok {
|
||
|
|
return "", false
|
||
|
|
}
|
||
|
|
return filepath.Dir(source.Path), true
|
||
|
|
}
|
||
|
|
|
||
|
|
func resolveHostResource(resolution *configkit.Resolution, path configkit.Path, value *string, invocationDir string) {
|
||
|
|
trimmed := strings.TrimSpace(*value)
|
||
|
|
if trimmed == "" || filepath.IsAbs(trimmed) {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
configkit.Normalize(resolution, path, value, filepath.Join(resourceBase(resolution, path, invocationDir), trimmed), "resolved relative resource from its source")
|
||
|
|
}
|