Demonstrate shared external configuration imports

This commit is contained in:
2026-12-05 20:57:18 +00:00
parent 6eb0a3f08f
commit f7262558c5
7 changed files with 93 additions and 1 deletions

View File

@@ -24,6 +24,7 @@ Start with one file, then choose the tasks your application needs:
| Load one file | [Happy path](#happy-path) and the [README example](../../README.md#load-a-configuration-file) |
| Supply fixed defaults and check application rules | [Defaults and application rules](#defaults-and-application-rules) |
| Add bundled defaults beneath application settings | [Native filesystem and layered input](#native-filesystem-and-layered-input) |
| Import a shared external file | [YAML illustration](../../examples/consumers/testdata/external-imports/main.yml) and the [one-load resource example](../../examples/consumers/external_imports_test.go) |
| Select files or an overlay | [Discovery](#explicit-main-file-discovery), [import defaults](#import-defaults-and-selection-inspection), and the [overlay reference](../config.md#selected-overlays) |
| Reuse settings or combine record lists | [Inheritance](#named-sibling-inheritance) and [identified sequences](#identified-prompt-sequences) |
| Handle credentials | [Typed-secret reference](../config.md#typed-secret-references), [configured startup](#configured-secrets-startup), and the [direct-directory example](../../examples/consumers/consumers_test.go) |

View File

@@ -458,6 +458,10 @@ fixtures remain accepted, and new path spellings use the normal lifecycle.
## Stage 13 — Demonstrate the feature with a maintained consumer example
Completed: the external-import fixture and consumer test demonstrate absolute
and transitive relative imports, origin-selected resource access and deliberate
Root containment. Consumer guidance links the YAML and test; consumer tests pass.
Add a small example under `examples/consumers` showing a main file importing a
shared external file using an absolute path, plus a relative import declared by
that external file. Build the host paths from temporary fixture directories in

View File

@@ -0,0 +1,76 @@
package consumers
import (
"errors"
"io/fs"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"testing"
configkit "go.maximumdirect.net/configkit"
)
func TestExternalSharedImports(t *testing.T) {
t.Parallel()
root := t.TempDir()
app, shared := filepath.Join(root, "app"), filepath.Join(root, "shared")
for _, directory := range []string{app, shared} {
if err := os.Mkdir(directory, 0700); err != nil {
t.Fatal(err)
}
}
fixture := func(name string) string {
t.Helper()
data, err := bundledFiles.ReadFile("testdata/external-imports/" + name)
if err != nil {
t.Fatal(err)
}
return string(data)
}
write := func(name, contents string) {
t.Helper()
if err := os.WriteFile(name, []byte(contents), 0600); err != nil {
t.Fatal(err)
}
}
sharedConfig := filepath.Join(shared, "config.yml")
main := filepath.Join(app, "main.yml")
write(main, strings.ReplaceAll(fixture("main.yml"), "/srv/shared/config.yml", strconv.Quote(sharedConfig)))
write(sharedConfig, fixture("config.yml"))
write(filepath.Join(shared, "extra.yml"), fixture("extra.yml"))
write(filepath.Join(shared, "message.txt"), "shared template")
type configuration struct {
Name string `yaml:"name"`
Template string `yaml:"template"`
Format string `yaml:"format"`
}
// One ordinary load handles both the absolute import and its relative import.
result, err := configkit.Load[configuration](configkit.File(main), configkit.Options[configuration]{Imports: true})
if err != nil {
t.Fatal(err)
}
if result.Config.Name != "application" || result.Config.Format != "plain" {
t.Fatalf("configuration=%#v", result.Config)
}
info, ok := result.Metadata().Lookup(configkit.Keys("template"))
if !ok || len(info.Origin.Locations) != 1 {
t.Fatalf("template origin=%#v", info)
}
context, ok := result.SourceContext(info.Origin.Locations[0].SourceID)
if !ok {
t.Fatal("template source context unavailable")
}
data, err := fs.ReadFile(context.FS, path.Join(context.BaseDir, result.Config.Template))
if err != nil || string(data) != "shared template" {
t.Fatalf("template=%q, %v", data, err)
}
// An application can deliberately retain containment at its main directory.
_, err = configkit.Load[configuration](configkit.File(main), configkit.Options[configuration]{Imports: true, Filesystem: &configkit.FilesystemContext{Root: app}})
var failure *configkit.LoadError
if !errors.As(err, &failure) || failure.Diagnostics()[0].Code != configkit.CodeImportOutsideRoot {
t.Fatalf("contained load=%v", err)
}
}

View File

@@ -9,7 +9,7 @@ import (
configkit "go.maximumdirect.net/configkit"
)
//go:embed testdata/native-source testdata/layered-bundle testdata/collection-bundle testdata/ordinary-imports testdata/source-version testdata/main-relative-bundle
//go:embed testdata/native-source testdata/layered-bundle testdata/collection-bundle testdata/ordinary-imports testdata/source-version testdata/main-relative-bundle testdata/external-imports
var bundledFiles embed.FS
// BundledConfig is a small configuration loaded directly from an embedded file.

View File

@@ -0,0 +1,5 @@
config:
imports:
- path: extra.yml
required: true
template: message.txt

View File

@@ -0,0 +1 @@
format: plain

View File

@@ -0,0 +1,5 @@
config:
imports:
- path: /srv/shared/config.yml
required: true
name: application