forked from anchore/syft
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add template output Signed-off-by: Jonas Xavier <jonasx@anchore.com> * remove dead code Signed-off-by: Jonas Xavier <jonasx@anchore.com> * fix template cli flag Signed-off-by: Jonas Xavier <jonasx@anchore.com> * implement template's own format type Signed-off-by: Jonas Xavier <jonasx@anchore.com> * simpler code Signed-off-by: Jonas Xavier <jonasx@anchore.com> * fix readme link to Go template Signed-off-by: Jonas Xavier <jonasx@anchore.com> * feedback changes Signed-off-by: Jonas Xavier <jonasx@anchore.com> * simpler func signature patter Signed-off-by: Jonas Xavier <jonasx@anchore.com> * nit Signed-off-by: Jonas Xavier <jonasx@anchore.com> * fix linter error Signed-off-by: Jonas Xavier <jonasx@anchore.com>
- Loading branch information
Showing
20 changed files
with
253 additions
and
15 deletions.
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,29 @@ | ||
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{} | ||
f.SetTemplatePath("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.