diff --git a/docs/consumers/README.md b/docs/consumers/README.md index de9abc5..6325384 100644 --- a/docs/consumers/README.md +++ b/docs/consumers/README.md @@ -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) | diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index 9d9d956..0a6a13d 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -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 diff --git a/examples/consumers/external_imports_test.go b/examples/consumers/external_imports_test.go new file mode 100644 index 0000000..c928f03 --- /dev/null +++ b/examples/consumers/external_imports_test.go @@ -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) + } +} diff --git a/examples/consumers/layered.go b/examples/consumers/layered.go index 0d9270a..a4e174c 100644 --- a/examples/consumers/layered.go +++ b/examples/consumers/layered.go @@ -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. diff --git a/examples/consumers/testdata/external-imports/config.yml b/examples/consumers/testdata/external-imports/config.yml new file mode 100644 index 0000000..315504d --- /dev/null +++ b/examples/consumers/testdata/external-imports/config.yml @@ -0,0 +1,5 @@ +config: + imports: + - path: extra.yml + required: true +template: message.txt diff --git a/examples/consumers/testdata/external-imports/extra.yml b/examples/consumers/testdata/external-imports/extra.yml new file mode 100644 index 0000000..734132c --- /dev/null +++ b/examples/consumers/testdata/external-imports/extra.yml @@ -0,0 +1 @@ +format: plain diff --git a/examples/consumers/testdata/external-imports/main.yml b/examples/consumers/testdata/external-imports/main.yml new file mode 100644 index 0000000..6a8c25b --- /dev/null +++ b/examples/consumers/testdata/external-imports/main.yml @@ -0,0 +1,5 @@ +config: + imports: + - path: /srv/shared/config.yml + required: true +name: application