2026-09-13 16:26:18 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2026-11-21 22:49:41 +00:00
|
|
|
"os"
|
2026-09-13 16:26:18 +00:00
|
|
|
"strings"
|
|
|
|
|
|
2026-11-17 20:42:25 +00:00
|
|
|
configkit "go.maximumdirect.net/configkit"
|
2026-09-13 16:26:18 +00:00
|
|
|
)
|
|
|
|
|
|
2026-09-13 16:35:46 +00:00
|
|
|
func resolveDocumentOperations(document *Document, resolution *configkit.Resolution, state *documentLoadState) error {
|
2026-09-13 16:26:18 +00:00
|
|
|
if document == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
normalizeDocumentState(document, resolution)
|
2026-09-13 16:48:44 +00:00
|
|
|
normalizeDocumentPaths(document, resolution, state)
|
2026-11-21 22:49:41 +00:00
|
|
|
if document.CacheDirectory != nil && *document.CacheDirectory == "" {
|
|
|
|
|
root, err := DefaultCacheRoot(os.UserCacheDir)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
// Empty is an explicit selection of the per-user default, not a physical root.
|
|
|
|
|
configkit.Normalize(resolution, configkit.Keys("cache_directory"), &document.CacheDirectory, documentValue(root), "selected per-user cache directory")
|
2026-09-14 00:40:40 +00:00
|
|
|
}
|
2026-11-21 22:49:41 +00:00
|
|
|
normalizeDocumentPipelines(document, resolution, state)
|
2026-09-13 16:26:18 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func normalizeDocumentState(document *Document, resolution *configkit.Resolution) {
|
|
|
|
|
if document.Output != nil {
|
|
|
|
|
normalizeDocumentString(resolution, configkit.Keys("output", "directory"), &document.Output.Directory, "trimmed output directory")
|
|
|
|
|
}
|
2026-11-21 22:49:41 +00:00
|
|
|
normalizeDocumentString(resolution, configkit.Keys("cache_directory"), &document.CacheDirectory, "trimmed cache directory")
|
2026-09-13 16:26:18 +00:00
|
|
|
if document.Debug != nil {
|
|
|
|
|
normalizeDocumentString(resolution, configkit.Keys("debug", "directory"), &document.Debug.Directory, "trimmed debug directory")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func normalizeDocumentString(resolution *configkit.Resolution, path configkit.Path, target **string, reason string) {
|
|
|
|
|
if target == nil || *target == nil || !resolution.IsExplicit(path) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
configkit.Normalize(resolution, path, target, documentValue(strings.TrimSpace(**target)), reason)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func validateDocumentOperations(document *Document, validation *configkit.Validation) {
|
|
|
|
|
if document == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-11-21 22:49:41 +00:00
|
|
|
if document.Concurrency == nil || document.Concurrency.Workers == nil {
|
|
|
|
|
validation.Error(configkit.Keys("concurrency", "workers"), "must be a positive integer")
|
|
|
|
|
} else if err := validateWorkers(*document.Concurrency.Workers); err != nil {
|
|
|
|
|
validation.Error(configkit.Keys("concurrency", "workers"), err.Error())
|
2026-09-13 16:26:18 +00:00
|
|
|
}
|
|
|
|
|
if document.Output == nil || document.Output.Directory == nil {
|
|
|
|
|
validation.Error(configkit.Keys("output", "directory"), "must not be empty")
|
|
|
|
|
} else if err := validateRequiredPath("output.directory", *document.Output.Directory); err != nil {
|
|
|
|
|
validation.Error(configkit.Keys("output", "directory"), err.Error())
|
|
|
|
|
}
|
2026-11-21 22:49:41 +00:00
|
|
|
if document.CacheDirectory == nil {
|
|
|
|
|
validation.Error(configkit.Keys("cache_directory"), "must not be null")
|
|
|
|
|
} else if err := validateOptionalPath("cache_directory", *document.CacheDirectory); err != nil {
|
|
|
|
|
validation.Error(configkit.Keys("cache_directory"), err.Error())
|
2026-09-13 16:26:18 +00:00
|
|
|
}
|
|
|
|
|
if document.Debug == nil || document.Debug.Directory == nil {
|
2026-11-21 22:49:41 +00:00
|
|
|
validation.Error(configkit.Keys("debug", "directory"), "must not be null")
|
|
|
|
|
} else if err := validateOptionalPath("debug.directory", *document.Debug.Directory); err != nil {
|
2026-09-13 16:26:18 +00:00
|
|
|
validation.Error(configkit.Keys("debug", "directory"), err.Error())
|
|
|
|
|
}
|
2026-11-21 22:49:41 +00:00
|
|
|
validateDocumentPipelines(document, validation)
|
2026-09-13 16:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-21 22:49:41 +00:00
|
|
|
func validateWorkers(workers int) error {
|
|
|
|
|
if workers <= 0 {
|
|
|
|
|
return fmt.Errorf("concurrency.workers must be greater than zero")
|
2026-09-13 16:26:18 +00:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func validateRequiredPath(name, value string) error {
|
|
|
|
|
if strings.TrimSpace(value) == "" {
|
|
|
|
|
return fmt.Errorf("%s must not be empty", name)
|
|
|
|
|
}
|
|
|
|
|
return validateOptionalPath(name, value)
|
|
|
|
|
}
|
|
|
|
|
func validateOptionalPath(name, value string) error {
|
|
|
|
|
if strings.ContainsRune(value, '\x00') {
|
|
|
|
|
return fmt.Errorf("%s must not contain NUL", name)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|