package config import ( "fmt" "os" "strings" configkit "go.maximumdirect.net/configkit" ) func resolveDocumentOperations(document *Document, resolution *configkit.Resolution, state *documentLoadState) error { if document == nil { return nil } normalizeDocumentState(document, resolution) normalizeDocumentPaths(document, resolution, state) 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") } normalizeDocumentPipelines(document, resolution, state) 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") } normalizeDocumentString(resolution, configkit.Keys("cache_directory"), &document.CacheDirectory, "trimmed cache directory") 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 } 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()) } 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()) } 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()) } if document.Debug == nil || document.Debug.Directory == nil { validation.Error(configkit.Keys("debug", "directory"), "must not be null") } else if err := validateOptionalPath("debug.directory", *document.Debug.Directory); err != nil { validation.Error(configkit.Keys("debug", "directory"), err.Error()) } validateDocumentPipelines(document, validation) } func validateWorkers(workers int) error { if workers <= 0 { return fmt.Errorf("concurrency.workers must be greater than zero") } 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 }