91 lines
3.9 KiB
Go
91 lines
3.9 KiB
Go
package configkit
|
|
|
|
import "gitea.maximumdirect.net/eric/ConfigKit/internal/engine"
|
|
|
|
// Search is an immutable ordered description of possible host-file inputs.
|
|
// It is consumed only when passed to SearchConfig and used by a load.
|
|
type Search struct{ value engine.Search }
|
|
|
|
// FilenameGroup selects filenames in order for a directory search.
|
|
// Construct groups with NamedFilenames, GenericFilenames, or CustomFilenames.
|
|
type FilenameGroup struct{ value engine.FilenameGroup }
|
|
|
|
// SearchOptions controls whether a search must select a file.
|
|
type SearchOptions struct {
|
|
// Required reports a missing-source error if the complete search is exhausted.
|
|
Required bool
|
|
}
|
|
|
|
// DirectorySearchOptions selects the filename groups used by SearchDirectory.
|
|
type DirectorySearchOptions struct {
|
|
// Filenames concatenates groups in order. A nil slice uses Named then Generic;
|
|
// a non-nil empty slice selects no filenames.
|
|
Filenames []FilenameGroup
|
|
}
|
|
|
|
// NamedFilenames selects <application>.yml followed by <application>.yaml.
|
|
func NamedFilenames() FilenameGroup { return FilenameGroup{value: engine.NamedFilenames()} }
|
|
|
|
// GenericFilenames selects config.yml, config.yaml, main.yml, and main.yaml.
|
|
func GenericFilenames() FilenameGroup { return FilenameGroup{value: engine.GenericFilenames()} }
|
|
|
|
// CustomFilenames selects literal filename components in the supplied order.
|
|
// Names cannot be empty or contain a directory component.
|
|
func CustomFilenames(names ...string) FilenameGroup {
|
|
return FilenameGroup{value: engine.CustomFilenames(names...)}
|
|
}
|
|
|
|
// SearchDirectory describes a search within directory. Omitted options or a nil
|
|
// Filenames slice uses Named then Generic groups. Explicitly empty Filenames
|
|
// selects no names and does not resolve the directory base.
|
|
func SearchDirectory(directory string, supplied ...DirectorySearchOptions) Search {
|
|
options := make([]engine.DirectorySearchOptions, len(supplied))
|
|
for index, option := range supplied {
|
|
if option.Filenames == nil {
|
|
continue
|
|
}
|
|
options[index].Filenames = make([]engine.FilenameGroup, len(option.Filenames))
|
|
for groupIndex, group := range option.Filenames {
|
|
options[index].Filenames[groupIndex] = group.value
|
|
}
|
|
}
|
|
return Search{value: engine.SearchDirectory(directory, options...)}
|
|
}
|
|
|
|
// Paths describes literal host-file candidates in the supplied order.
|
|
func Paths(paths ...string) Search { return Search{value: engine.Paths(paths...)} }
|
|
|
|
// Searches concatenates descriptions in order and snapshots their entries.
|
|
func Searches(searches ...Search) Search {
|
|
values := make([]engine.Search, len(searches))
|
|
for index, search := range searches {
|
|
values[index] = search.value
|
|
}
|
|
return Search{value: engine.Searches(values...)}
|
|
}
|
|
|
|
// UnixSystemDirectories describes the conventional Unix system directories.
|
|
// Loading on an unsupported platform fails if this preset is reached.
|
|
func UnixSystemDirectories() Search { return Search{value: engine.UnixSystemDirectories()} }
|
|
|
|
// UserConfigDirectory describes searches rooted at the Go user-config directory.
|
|
func UserConfigDirectory() Search { return Search{value: engine.UserConfigDirectory()} }
|
|
|
|
// HomeDirectory describes searches rooted at the Go home directory.
|
|
func HomeDirectory() Search { return Search{value: engine.HomeDirectory()} }
|
|
|
|
// WorkingDirectory describes application-named files in the current directory.
|
|
func WorkingDirectory() Search { return Search{value: engine.WorkingDirectory()} }
|
|
|
|
// SearchConfig creates a source that selects the first matching host file when
|
|
// consumed by Load or LoadLayered. Without Required, exhausting the search uses
|
|
// the absent-input lifecycle. Description and option errors are reported while
|
|
// loading.
|
|
func SearchConfig(application string, search Search, supplied ...SearchOptions) Source {
|
|
options := make([]engine.SearchOptions, len(supplied))
|
|
for index, option := range supplied {
|
|
options[index] = engine.SearchOptions{Required: option.Required}
|
|
}
|
|
return Source{value: engine.SearchConfig(application, search.value, options...)}
|
|
}
|