249 lines
7.2 KiB
Go
249 lines
7.2 KiB
Go
package configfamily
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"os"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"gopkg.in/yaml.v3"
|
||
|
|
|
||
|
|
coreconfig "yetano.dev/yetano/core/config"
|
||
|
|
"yetano.dev/yetano/core/configcmd"
|
||
|
|
"yetano.dev/yetano/core/paths"
|
||
|
|
)
|
||
|
|
|
||
|
|
// LayerKind says which part of which file a Layer is.
|
||
|
|
type LayerKind int
|
||
|
|
|
||
|
|
const (
|
||
|
|
// SharedLayer is the top level of the family file, without the
|
||
|
|
// program sections.
|
||
|
|
SharedLayer LayerKind = iota
|
||
|
|
// SectionLayer is the program's section of the family file.
|
||
|
|
SectionLayer
|
||
|
|
// ProgramLayer is the program's own file.
|
||
|
|
ProgramLayer
|
||
|
|
// ExplicitLayer is the file --config names.
|
||
|
|
ExplicitLayer
|
||
|
|
)
|
||
|
|
|
||
|
|
// Layer is one input of a program's configuration.
|
||
|
|
type Layer struct {
|
||
|
|
Kind LayerKind
|
||
|
|
// Path is the file. A SectionLayer has the family file's path.
|
||
|
|
Path string
|
||
|
|
// Section is the program section's key for a SectionLayer, else "".
|
||
|
|
Section string
|
||
|
|
// Source is the tier the file's directory belongs to (user, system,
|
||
|
|
// ...; paths.APIOverride for --config).
|
||
|
|
Source paths.Source
|
||
|
|
// Exists reports whether the layer contributes anything: the file
|
||
|
|
// exists (for a SectionLayer: and has the section).
|
||
|
|
Exists bool
|
||
|
|
}
|
||
|
|
|
||
|
|
// Name is how a value's origin is shown: the path, with the section in
|
||
|
|
// brackets for a SectionLayer.
|
||
|
|
func (l Layer) Name() string {
|
||
|
|
if l.Kind == SectionLayer {
|
||
|
|
return l.Path + " [" + l.Section + "]"
|
||
|
|
}
|
||
|
|
return l.Path
|
||
|
|
}
|
||
|
|
|
||
|
|
// SourceName is the tier for display ("--config" for an explicit file).
|
||
|
|
func (l Layer) SourceName() string {
|
||
|
|
if l.Kind == ExplicitLayer {
|
||
|
|
return "--config"
|
||
|
|
}
|
||
|
|
return l.Source.String()
|
||
|
|
}
|
||
|
|
|
||
|
|
// Info returns l as core/configcmd's LayerInfo.
|
||
|
|
func (l Layer) Info() configcmd.LayerInfo {
|
||
|
|
return configcmd.LayerInfo{Name: l.Name(), Path: l.Path, Source: l.SourceName(), Exists: l.Exists}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Layers returns every layer of p's configuration, highest priority
|
||
|
|
// first, whether it exists or not:
|
||
|
|
//
|
||
|
|
// --config <file>
|
||
|
|
// user tier: <program>.yaml, <family>.yaml [<section>], <family>.yaml
|
||
|
|
// system tier: <program>.yaml, <family>.yaml [<section>], <family>.yaml
|
||
|
|
//
|
||
|
|
// A --config file that does not exist is an error.
|
||
|
|
func Layers(p Program, explicit string, opts ...coreconfig.Option) ([]Layer, error) {
|
||
|
|
var out []Layer
|
||
|
|
if explicit != "" {
|
||
|
|
if _, err := os.Stat(explicit); err != nil {
|
||
|
|
return nil, fmt.Errorf("config: --config %s: %w", explicit, err)
|
||
|
|
}
|
||
|
|
out = append(out, Layer{Kind: ExplicitLayer, Path: explicit, Source: paths.APIOverride, Exists: true})
|
||
|
|
}
|
||
|
|
programFiles, err := tierFiles(p, p.Filename(), opts)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
sharedFiles, err := tierFiles(p, p.Family.SharedFilename(), opts)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
if len(programFiles) != len(sharedFiles) {
|
||
|
|
return nil, fmt.Errorf("config: %d tiers for %s but %d for %s", len(programFiles), p.Filename(), len(sharedFiles), p.Family.SharedFilename())
|
||
|
|
}
|
||
|
|
for i, pf := range programFiles {
|
||
|
|
sf := sharedFiles[i]
|
||
|
|
section := Layer{Kind: SectionLayer, Path: sf.Path, Section: p.Section(), Source: sf.Source}
|
||
|
|
if sf.Exists {
|
||
|
|
data, err := coreconfig.ReadFile(sf.Path)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
_, section.Exists = data[p.Section()]
|
||
|
|
}
|
||
|
|
out = append(out,
|
||
|
|
Layer{Kind: ProgramLayer, Path: pf.Path, Source: pf.Source, Exists: pf.Exists},
|
||
|
|
section,
|
||
|
|
Layer{Kind: SharedLayer, Path: sf.Path, Source: sf.Source, Exists: sf.Exists},
|
||
|
|
)
|
||
|
|
}
|
||
|
|
return out, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// tierFiles is core/config's layer list for filename in the family's
|
||
|
|
// directories, without the working directory.
|
||
|
|
func tierFiles(p Program, filename string, opts []coreconfig.Option) ([]coreconfig.Layer, error) {
|
||
|
|
r, err := coreconfig.New(p.App, append(append([]coreconfig.Option(nil), opts...),
|
||
|
|
coreconfig.WithFilename(filename),
|
||
|
|
coreconfig.WithWorkingDirectory(false),
|
||
|
|
coreconfig.WithEnvExpansion(false))...)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return r.ResolveAll()
|
||
|
|
}
|
||
|
|
|
||
|
|
// Loaded is a program's merged configuration.
|
||
|
|
type Loaded struct {
|
||
|
|
Program Program
|
||
|
|
// Layers are all layers, highest priority first (see Layers).
|
||
|
|
Layers []Layer
|
||
|
|
// Merged is the merged configuration.
|
||
|
|
Merged map[string]any
|
||
|
|
// Origins names the layer (Layer.Name) every leaf of Merged came
|
||
|
|
// from.
|
||
|
|
Origins coreconfig.Origins
|
||
|
|
}
|
||
|
|
|
||
|
|
// Load reads and merges every existing layer of p's configuration,
|
||
|
|
// lowest priority first. explicit is the --config value ("" if not
|
||
|
|
// given). With no layer at all it returns ErrNotFound, wrapped with the
|
||
|
|
// searched files; a program that runs on built-in defaults treats that
|
||
|
|
// as "no file", not as a failure.
|
||
|
|
func Load(p Program, explicit string, opts ...coreconfig.Option) (*Loaded, error) {
|
||
|
|
layers, err := Layers(p, explicit, opts...)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
var sources []coreconfig.Source
|
||
|
|
for i := len(layers) - 1; i >= 0; i-- {
|
||
|
|
l := layers[i]
|
||
|
|
if !l.Exists {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
data, err := layerData(p.Family, l)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
sources = append(sources, coreconfig.Source{Name: l.Name(), Data: data})
|
||
|
|
}
|
||
|
|
if len(sources) == 0 {
|
||
|
|
return nil, notFound(p, layers)
|
||
|
|
}
|
||
|
|
merged, origins := coreconfig.Merge(sources...)
|
||
|
|
return &Loaded{Program: p, Layers: layers, Merged: merged, Origins: origins}, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// layerData is the part of l's file that l contributes.
|
||
|
|
func layerData(f Family, l Layer) (map[string]any, error) {
|
||
|
|
data, err := coreconfig.ReadFile(l.Path)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
switch l.Kind {
|
||
|
|
case SharedLayer:
|
||
|
|
shared := make(map[string]any, len(data))
|
||
|
|
for k, v := range data {
|
||
|
|
if !f.IsProgramSection(k) {
|
||
|
|
shared[k] = v
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return shared, nil
|
||
|
|
case SectionLayer:
|
||
|
|
v := data[l.Section]
|
||
|
|
if v == nil {
|
||
|
|
return map[string]any{}, nil
|
||
|
|
}
|
||
|
|
section, ok := v.(map[string]any)
|
||
|
|
if !ok {
|
||
|
|
return nil, fmt.Errorf("config: %s: %s: want a mapping of options", l.Path, l.Section)
|
||
|
|
}
|
||
|
|
return section, nil
|
||
|
|
}
|
||
|
|
return data, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func notFound(p Program, layers []Layer) error {
|
||
|
|
seen := map[string]bool{}
|
||
|
|
var b strings.Builder
|
||
|
|
for _, l := range layers {
|
||
|
|
if l.Kind == SectionLayer || seen[l.Path] {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
seen[l.Path] = true
|
||
|
|
b.WriteString("\n " + l.Path)
|
||
|
|
}
|
||
|
|
return fmt.Errorf("%w for %s; searched:%s", ErrNotFound, p.Name, b.String())
|
||
|
|
}
|
||
|
|
|
||
|
|
// Effective returns l as core/configcmd's Effective (for config show,
|
||
|
|
// path, validate and doctor), with l itself as its Loader.
|
||
|
|
func (l *Loaded) Effective() *configcmd.Effective {
|
||
|
|
layers := make([]configcmd.LayerInfo, len(l.Layers))
|
||
|
|
for i, layer := range l.Layers {
|
||
|
|
layers[i] = layer.Info()
|
||
|
|
}
|
||
|
|
return &configcmd.Effective{Name: l.Program.Name, Layers: layers, Merged: l.Merged, Origins: l.Origins, Loader: l}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Used returns the layers that contributed, highest priority first.
|
||
|
|
func (l *Loaded) Used() []Layer {
|
||
|
|
var out []Layer
|
||
|
|
for _, layer := range l.Layers {
|
||
|
|
if layer.Exists {
|
||
|
|
out = append(out, layer)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return out
|
||
|
|
}
|
||
|
|
|
||
|
|
// YAML returns the merged configuration as YAML, for the program's own
|
||
|
|
// parser.
|
||
|
|
func (l *Loaded) YAML() ([]byte, error) {
|
||
|
|
data, err := yaml.Marshal(l.Merged)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("config: marshal merged configuration: %w", err)
|
||
|
|
}
|
||
|
|
return data, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// Describe names the files that contributed, lowest priority first, for
|
||
|
|
// log lines and error messages ("a.yaml + b.yaml [web]").
|
||
|
|
func (l *Loaded) Describe() string {
|
||
|
|
used := l.Used()
|
||
|
|
names := make([]string, len(used))
|
||
|
|
for i, layer := range used {
|
||
|
|
names[len(used)-1-i] = layer.Name()
|
||
|
|
}
|
||
|
|
return strings.Join(names, " + ")
|
||
|
|
}
|