-
Notifications
You must be signed in to change notification settings - Fork 293
[butane]: Gomplate integration #2298
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
Open
vic1707
wants to merge
6
commits into
coreos:main
Choose a base branch
from
vic1707:gomplate-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,180,226
−11
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2702b1f
install `gomplate`
vic1707 7c11614
use gomplate as the templating engine
vic1707 68574f0
vendor: update vulnerable dependencies
vic1707 4be491a
chore: missing headers
vic1707 e6fb0ae
chore: reorder imports
vic1707 50ff622
feat: add `--gomplate-config` flag
vic1707 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
The diff you're trying to view is too large. We only load the first 3000 changed files.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // Copyright 2026 Red Hat, Inc | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package util | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "errors" | ||
| "io" | ||
| "os" | ||
|
|
||
| "github.com/hairyhenderson/gomplate/v5" | ||
| ) | ||
|
|
||
| var ( | ||
| EnableGomplate = false | ||
|
|
||
| GomplateConfigPath = ".gomplate.yaml" | ||
| renderer = gomplate.NewRenderer(gomplate.RenderOptions{}) | ||
| renderContext = context.Background() | ||
| ) | ||
|
|
||
| func parseGomplateConfig() (*gomplate.Config, error) { | ||
| f, err := os.Open(GomplateConfigPath) | ||
| if err != nil { | ||
| if errors.Is(err, os.ErrNotExist) { | ||
| return nil, nil | ||
| } | ||
| return nil, err | ||
| } | ||
| defer f.Close() | ||
|
|
||
| return gomplate.Parse(f) | ||
| } | ||
|
|
||
| func InitGomplateRenderer() error { | ||
| config, err := parseGomplateConfig() | ||
| if err != nil { | ||
| renderer = nil | ||
| return err | ||
| } | ||
|
|
||
| if config != nil { | ||
| if config.Experimental { | ||
| renderContext = gomplate.SetExperimental(renderContext) | ||
| } | ||
|
|
||
| // Inspired by `gomplate.bindPlugins` | ||
| funcMap := map[string]any{} | ||
| for pluginName, plugin := range config.Plugins { | ||
| // default the timeout to the one in the config | ||
| timeout := config.PluginTimeout | ||
| if plugin.Timeout != 0 { | ||
| timeout = plugin.Timeout | ||
| } | ||
|
|
||
| funcMap[pluginName] = gomplate.PluginFunc(renderContext, plugin.Cmd, gomplate.PluginOpts{ | ||
| Timeout: timeout, | ||
| Pipe: plugin.Pipe, | ||
| Stderr: config.Stderr, | ||
| Args: plugin.Args, | ||
| }) | ||
| } | ||
|
|
||
| renderer = gomplate.NewRenderer(gomplate.RenderOptions{ | ||
| Funcs: funcMap, | ||
| Datasources: config.DataSources, | ||
| Context: config.Context, | ||
| Templates: config.Templates, | ||
| LDelim: config.LDelim, | ||
| RDelim: config.RDelim, | ||
| MissingKey: config.MissingKey, | ||
| }) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func GomplateReadLocalFile(file *os.File) ([]byte, error) { | ||
| fileContent, err := io.ReadAll(file) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if !EnableGomplate { | ||
| return fileContent, nil | ||
| } | ||
|
|
||
| var buf bytes.Buffer | ||
| err = renderer.Render(renderContext, file.Name(), string(fileContent), &buf) | ||
| return buf.Bytes(), err | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| // Copyright 2026 Red Hat, Inc | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package util | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| ) | ||
|
|
||
| func preserveGlobals(t *testing.T) func() { | ||
| t.Helper() | ||
| oldEnableGomplate := EnableGomplate | ||
| oldConfigPath := GomplateConfigPath | ||
| oldRenderer := renderer | ||
| oldContext := renderContext | ||
|
|
||
| return func() { | ||
| EnableGomplate = oldEnableGomplate | ||
| GomplateConfigPath = oldConfigPath | ||
| renderer = oldRenderer | ||
| renderContext = oldContext | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| func initGomplate(t *testing.T, gomplateConfig string) error { | ||
| t.Helper() | ||
| EnableGomplate = true | ||
|
|
||
| if gomplateConfig != "" { | ||
| tmpDir := t.TempDir() | ||
| configPath := filepath.Join(tmpDir, ".gomplate.yaml") | ||
|
|
||
| err := os.WriteFile(configPath, []byte(gomplateConfig), 0644) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| GomplateConfigPath = configPath | ||
| } else { | ||
| GomplateConfigPath = "" | ||
| } | ||
|
|
||
| return InitGomplateRenderer() | ||
| } | ||
|
|
||
| func evalTemplate(t *testing.T, template string) (string, error) { | ||
| t.Helper() | ||
|
|
||
| tmpFile, err := os.CreateTemp("", "template-*.tmpl") | ||
| if err != nil { | ||
| t.Fatalf("failed to create temp file: %v", err) | ||
| } | ||
| defer os.Remove(tmpFile.Name()) | ||
| defer tmpFile.Close() | ||
|
|
||
| if _, err := tmpFile.WriteString(template); err != nil { | ||
| t.Fatalf("failed to write to temp file: %v", err) | ||
| } | ||
| _, err = tmpFile.Seek(0, 0) | ||
| if err != nil { | ||
| t.Fatalf("failed to return to begining of temp file: %v", err) | ||
| } | ||
|
|
||
| output, err := GomplateReadLocalFile(tmpFile) | ||
| return string(output), err | ||
| } | ||
|
|
||
| func TestInvalidGomplateConfig(t *testing.T) { | ||
| defer preserveGlobals(t)() | ||
| if err := initGomplate(t, "not: valid: config: ["); err == nil { | ||
| t.Fatalf("gomplate initialization should have failed: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestInvalidTemplate(t *testing.T) { | ||
| defer preserveGlobals(t)() | ||
| if err := initGomplate(t, "#empty config"); err != nil { | ||
| t.Fatalf("failed to write config: %v", err) | ||
| } | ||
| _, err := evalTemplate(t, "{{ .NonExistentField }}") | ||
|
|
||
| if err == nil { | ||
| t.Fatalf("expected error for missing key, got nil") | ||
| } | ||
| } | ||
|
|
||
| func TestNoCustomConfig(t *testing.T) { | ||
| defer preserveGlobals(t)() | ||
| if err := initGomplate(t, "#empty config"); err != nil { | ||
| t.Fatalf("failed to write config: %v", err) | ||
| } | ||
| rendered, err := evalTemplate(t, `{{ "foobarbazquxquux" | strings.Abbrev 9 }}`) | ||
|
|
||
| if err != nil { | ||
| t.Fatalf("unexpected error: %+v\n", err) | ||
| } | ||
|
|
||
| if rendered != "foobar..." { | ||
| t.Fatalf("Invalid rendered template, got: '%s'\n", rendered) | ||
| } | ||
| } | ||
|
|
||
| func TestGomplateConfigApplication(t *testing.T) { | ||
| defer preserveGlobals(t)() | ||
| // Create a mock HTTP server that returns a fixed JSON response | ||
| ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| resp := map[string]string{"hello": "Hello"} | ||
| w.Header().Set("Content-Type", "application/json") | ||
| err := json.NewEncoder(w).Encode(resp) | ||
| if err != nil { | ||
| t.Fatalf("json encoding failed: %v", err) | ||
| } | ||
| })) | ||
| defer ts.Close() | ||
|
|
||
| configContent := ` | ||
| leftDelim: ($( | ||
| rightDelim: )$) | ||
| context: | ||
| data: | ||
| url: ` + ts.URL | ||
| if err := initGomplate(t, configContent); err != nil { | ||
| t.Fatalf("failed to write config: %v", err) | ||
| } | ||
| rendered, err := evalTemplate(t, "($( .data.hello )$)!") | ||
|
|
||
| if err != nil { | ||
| t.Fatalf("unexpected error: %+v\n", err) | ||
| } | ||
| if rendered != "Hello!" { | ||
| t.Fatalf("Invalid rendered template, got: '%s'\n", rendered) | ||
| } | ||
| } | ||
|
|
||
| func TestGomplateDisabled(t *testing.T) { | ||
| defer preserveGlobals(t)() | ||
| EnableGomplate = false | ||
|
|
||
| expected := "some raw content" | ||
| rendered, err := evalTemplate(t, expected) | ||
|
|
||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if rendered != expected { | ||
| t.Fatalf("Invalid rendered template, got: '%s'\n", rendered) | ||
| } | ||
| } | ||
|
|
||
| func TestMissingGomplateConfigFile(t *testing.T) { | ||
| defer preserveGlobals(t)() | ||
| EnableGomplate = true | ||
| GomplateConfigPath = "/nonexistent/path/.gomplate.yaml" | ||
|
|
||
| err := InitGomplateRenderer() | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestGomplatePlugins(t *testing.T) { | ||
| defer preserveGlobals(t)() | ||
|
|
||
| configContent := ` | ||
| plugins: | ||
| echo: | ||
| cmd: /bin/echo | ||
| args: | ||
| - foo | ||
| ` | ||
| if err := initGomplate(t, configContent); err != nil { | ||
| t.Fatalf("failed to write config: %v", err) | ||
| } | ||
| // we also ensure no built-in functions got erased | ||
| rendered, err := evalTemplate(t, `{{ echo "bar" | strings.Trunc 6 }}`) | ||
|
|
||
| if err != nil { | ||
| t.Fatalf("unexpected error: %+v\n", err) | ||
| } | ||
| if rendered != "foo ba" { | ||
| t.Fatalf("Invalid rendered template, got: '%s'\n", rendered) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.