// Package configfamily locates and merges the configuration of a program // that belongs to a family of programs sharing one set of config // directories -- the yamase-* or yaiam-* programs (yamase ADR-0074). Each // program is responsible for its own file; a family-wide file is // optional and holds what several programs share. // // In each config directory of the family (core/config's tiers, without // the working directory) a program reads: // // - .yaml, its top level: options several programs share // (storage, logging, ...), minus the program sections; // - .yaml, the program's section (its name without the // "-" prefix: "web:" for yaiam-web); // - .yaml, the program's own file. // // Load merges them lowest priority first -- system tier, then user tier, // then the file --config names -- with core/config's merge rules (maps // merge key by key, scalars and lists are replaced, null removes a key). // The working directory is never read; no environment expansion is // done. Parsing and validating the merged result stays with the program // (Loaded.YAML), since its options are its own business. // // Spec and Run serve the standard "config" command on top of this: path, // validate and show work on all layers, create and upgrade on the // program's own file, and "config create --shared" writes the family // file from Family.SharedTemplate. package configfamily import ( "slices" "strings" "yetano.dev/yetano/core" coreconfig "yetano.dev/yetano/core/config" "yetano.dev/yetano/core/configcmd" ) // ErrNotFound is returned (wrapped, with the searched files) by Load // when no layer exists at all. var ErrNotFound = configcmd.ErrNotFound // Family describes a family of programs. type Family struct { // Name is the family's name: the core.Application name every // program shares (it names the config directories) and the prefix // of the program names ("yaiam" for yaiam-web). Name string // Programs are the section names of the family's programs in the // family file ("web", "authd", ...). These top-level keys are never // part of the shared layer. Programs []string // Shared is the schema (a struct value) of the options the family // file holds at its top level. Its options are valid in every layer // of every program, even one that does not read them. nil: none. Shared any // SharedTemplate is what "config create --shared" writes as the // family file. "": the command is not offered. SharedTemplate string } // SharedFilename is the family-wide file, ".yaml". func (f Family) SharedFilename() string { return f.Name + ".yaml" } // IsProgramSection reports whether key is a program section of the // family file. func (f Family) IsProgramSection(key string) bool { return slices.Contains(f.Programs, key) } // IsSharedKey reports whether key (dotted) is an option of the family's // shared schema. func (f Family) IsSharedKey(key string) bool { return f.Shared != nil && configcmd.KnownKey(f.Shared, key) } // Service returns the Program for a long-running service of the family. // Scope stays at its UserScope default, so the user config dir is // searched before the system one -- Kind and Scope are independent. func (f Family) Service(name string) Program { return Program{Family: f, App: core.Application{Name: f.Name, Kind: core.Service}, Name: name} } // CLI returns the Program for a command-line tool of the family. func (f Family) CLI(name string) Program { return Program{Family: f, App: core.Application{Name: f.Name, Kind: core.CLI}, Name: name} } // Program is one program of a family. type Program struct { Family Family // App is the family-wide core.Application (Name is Family.Name); // only its Kind differs between services and command-line tools. App core.Application // Name is the program's own name, e.g. "yaiam-web". Name string } // Filename is the program's own file, ".yaml". func (p Program) Filename() string { return p.Name + ".yaml" } // Section is the key of the program's section in the family file: its // name without the "-" prefix. func (p Program) Section() string { return strings.TrimPrefix(p.Name, p.Family.Name+"-") } // coreSpec is core/configcmd's Spec for p's own file: the family // directories without the working directory and ".yaml". func coreSpec(p Program, opts []coreconfig.Option) configcmd.Spec { opts = append(append([]coreconfig.Option(nil), opts...), coreconfig.WithWorkingDirectory(false)) return configcmd.Spec{Name: p.Name, App: p.App, Filename: p.Filename(), Options: opts} }