Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to load related file into merged vars #25

Merged
merged 5 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cli/agnosticv.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ func isPathCatalogItem(root, p string) bool {
return false
}
}
for _, el := range config.RelatedFilesV2 {
if path.Base(p) == el.File {
return false
}
}
}

// Catalog items are yaml files only.
Expand Down Expand Up @@ -294,12 +299,27 @@ func extendMergeListWithRelated(pAbs string, mergeList []Include) []Include {
Include{path: filepath.Join(filepath.Dir(pAbs), "description.html")},
)

done := map[string]bool{}
if config.initialized {
for _, el := range config.RelatedFiles {
if _, ok := done[el]; ok {
continue
}
result = append(
result,
Include{path: filepath.Join(filepath.Dir(pAbs), el)},
)
done[el] = true
}
for _, el := range config.RelatedFilesV2 {
if _, ok := done[el.File]; ok {
continue
}
result = append(
result,
Include{path: filepath.Join(filepath.Dir(pAbs), el.File)},
)
done[el.File] = true
}
}

Expand Down
9 changes: 9 additions & 0 deletions cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ import (
yamljson "github.com/ghodss/yaml"
)

type RelatedFile struct {
File string `json:"file"`
LoadInto string `json:"load_into"`
ContentKey string `json:"content_key"`
Set map[string]interface{} `json:"set"`
}

type Config struct {
// For any leaf file, consider those in same directory as related files:
RelatedFiles []string `json:"related_files"`
RelatedFilesV2 []RelatedFile`json:"related_files_v2"`

// Plumbing variable to know when config was loaded from disk.
initialized bool
Expand All @@ -34,6 +42,7 @@ func getConf(root string) Config {
if err != nil {
log.Fatalf("Unmarshal: %v", err)
}

c.initialized = true

return c
Expand Down
9 changes: 9 additions & 0 deletions cli/fixtures/.agnosticv.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
---
related_files:
- related_file.yaml

# For any catalog item, consider those in same directory as related files:
related_files_v2:
- file: service-ready-message-template.html.j2
load_into: /__meta__/service_ready
content_key: template
set:
format: jinja2
output_format: html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jinja2 content
29 changes: 29 additions & 0 deletions cli/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,35 @@ func mergeVars(p string, mergeStrategies []MergeStrategy) (map[string]any, []Inc
}
}

// Add related file content
if config.initialized {
for _, related := range config.RelatedFilesV2 {
if related.LoadInto != "" {
if related.ContentKey == "" {
logErr.Fatalf("Related file %s has no content key", related.File)
}
dir := filepath.Dir(p)
relatedPath := filepath.Join(dir, related.File)
if !fileExists(relatedPath) {
continue
}
content := map[string]any{}
for k, v := range related.Set {
content[k] = v
}
relatedContent, err := os.ReadFile(relatedPath)
if err != nil {
logErr.Fatalf("Error reading related file %s: %v", relatedPath, err)
}

content[related.ContentKey] = string(relatedContent)
if err := SetRelative(final, related.LoadInto, content); err != nil {
logErr.Fatalf("Error SetRelative: %v", err)
}
}
}
}

return final, mergeList, nil
}

Expand Down
31 changes: 31 additions & 0 deletions cli/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,34 @@ func TestMergeStrategicMergeList(t *testing.T) {
t.Error("lists do not match", value, expected)
}
}


func TestRelativeFileLoadInto(t *testing.T) {
rootFlag = abs("fixtures")
initConf(rootFlag)
initSchemaList()
initMergeStrategies()
validateFlag = true
merged, _, err := mergeVars(
"fixtures/test/BABYLON_EMPTY_CONFIG/prod.yaml",
mergeStrategies,
)
if err != nil {
t.Fatal(err)
}

_, value, _, err := Get(merged, "/__meta__/service_ready")
if err != nil {
t.Error(err)
}
expected := map[string]any{
"format":"jinja2",
"template":"jinja2 content",
"output_format": "html",
}

if !reflect.DeepEqual(value, expected) {
t.Error("Values do not match", value, expected)
}

}
31 changes: 26 additions & 5 deletions readme.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,13 @@ another_var: value <1>
* All included files of a catalog item are automatically considered related files.
* All common files of a catalog item are automatically considered related files.
* It is possible to add custom related files using the config file; see section below.
* It is possible to load related files into the merged vars using the config file; see section below.

==== Configuration ====

You can add custom related files using the `related_files` configuration option in a `.agnosticv.yaml` configuration file. It contains a list of filenamesj that if present in the same directory of a catalog item, will be considered as related to that catalog item.
You can add custom related files using the `related_files` configuration option in a `.agnosticv.yaml` configuration file. It contains a list of filenames that if present in the same directory of a catalog item, will be considered as related to that catalog item.

You can decide to load the content of the relative file into a path by specifying the path as a link:https://www.rfc-editor.org/rfc/rfc6901[JSON Pointer] format. The destination path will be a dictionary and the content will be loaded into the 'content_key'.


Here is an example of configuration:
Expand All @@ -300,10 +303,28 @@ Here is an example of configuration:
[source,yaml]
----
# For any catalog item, consider those files in same directory as related files:
related_files:
- service-ready-message-template.html.j2
- service-ready-message-template.html
- description.txt
related_files_v2:
- file: service-ready-message-template.html.j2
load_into: /__meta__/catalog/message_templates/service_ready
content_key: template
set:
format: jinja2
output_format: html
- file: description.txt
----

When merging, that will produce:

[source,yaml]
----
__meta__:
catalog:
message_templates:
service_ready:
template: |
<<content of the file>>
format: jinja2
output_format: html
fridim marked this conversation as resolved.
Show resolved Hide resolved
----

=== Leaf files ===
Expand Down