-
Notifications
You must be signed in to change notification settings - Fork 593
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 template output #1051
Merged
Merged
add template output #1051
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b7a1395
add template output
jonasagx 787de6b
remove dead code
jonasagx b76e736
fix template cli flag
jonasagx 0eff022
implement template's own format type
jonasagx 04a2cd3
simpler code
jonasagx 68eb426
fix readme link to Go template
jonasagx 915c792
feedback changes
jonasagx f46432f
simpler func signature patter
jonasagx be56791
nit
jonasagx 4184436
fix linter error
jonasagx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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 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,49 @@ | ||
package template | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"reflect" | ||
"text/template" | ||
|
||
"github.com/Masterminds/sprig/v3" | ||
"github.com/mitchellh/go-homedir" | ||
) | ||
|
||
func makeTemplateExecutor(templateFilePath string) (*template.Template, error) { | ||
if templateFilePath == "" { | ||
return nil, errors.New("no template file: please provide a template path") | ||
} | ||
|
||
expandedPathToTemplateFile, err := homedir.Expand(templateFilePath) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to expand path %s", templateFilePath) | ||
} | ||
|
||
templateContents, err := os.ReadFile(expandedPathToTemplateFile) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to get template content: %w", err) | ||
} | ||
|
||
templateName := expandedPathToTemplateFile | ||
tmpl, err := template.New(templateName).Funcs(funcMap).Parse(string(templateContents)) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to parse template: %w", err) | ||
} | ||
|
||
return tmpl, nil | ||
} | ||
|
||
// These are custom functions available to template authors. | ||
var funcMap = func() template.FuncMap { | ||
f := sprig.HermeticTxtFuncMap() | ||
f["getLastIndex"] = func(collection interface{}) int { | ||
if v := reflect.ValueOf(collection); v.Kind() == reflect.Slice { | ||
return v.Len() - 1 | ||
} | ||
|
||
return 0 | ||
} | ||
return f | ||
}() |
This file contains 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,28 @@ | ||
package template | ||
|
||
import ( | ||
"flag" | ||
"testing" | ||
|
||
"github.com/anchore/syft/internal/formats/common/testutils" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var updateTmpl = flag.Bool("update-tmpl", false, "update the *.golden files for json encoders") | ||
|
||
func TestFormatWithOption(t *testing.T) { | ||
f := OutputFormat{templateFilePath: "test-fixtures/csv.template"} | ||
|
||
testutils.AssertEncoderAgainstGoldenSnapshot(t, | ||
f, | ||
testutils.DirectoryInput(t), | ||
*updateTmpl, | ||
) | ||
|
||
} | ||
|
||
func TestFormatWithoutOptions(t *testing.T) { | ||
f := Format() | ||
err := f.Encode(nil, testutils.DirectoryInput(t)) | ||
assert.ErrorContains(t, err, "no template file: please provide a template path") | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: This is often done on the same line in Go