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

use go embed for templates #725

Merged
merged 1 commit into from Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 1 addition & 18 deletions report/html/template.go → report/html/template.html
@@ -1,20 +1,3 @@
// (c) Copyright 2016 Hewlett Packard Enterprise Development LP
//
// 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 html

const templateContent = `
<!doctype html>
<html lang="en">
<head>
Expand Down Expand Up @@ -454,4 +437,4 @@
);
</script>
</body>
</html>`
</html>
6 changes: 6 additions & 0 deletions report/html/writer.go
@@ -1,12 +1,18 @@
package html

import (

// use go embed to import template
_ "embed"
ccojocar marked this conversation as resolved.
Show resolved Hide resolved
ccojocar marked this conversation as resolved.
Show resolved Hide resolved
"html/template"
"io"

"github.com/securego/gosec/v2"
)

//go:embed template.html
var templateContent string

// WriteReport write a report in html format to the output writer
func WriteReport(w io.Writer, data *gosec.ReportInfo) error {
t, e := template.New("gosec").Parse(templateContent)
Expand Down
5 changes: 1 addition & 4 deletions report/text/template.go → report/text/template.txt
@@ -1,6 +1,4 @@
package text

const templateContent = `Results:
Results:
{{range $filePath,$fileErrors := .Errors}}
Golang errors in file: [{{ $filePath }}]:
{{range $index, $error := $fileErrors}}
Expand All @@ -23,4 +21,3 @@ Golang errors in file: [{{ $filePath }}]:
{{- danger .Stats.NumFound }}
{{- end }}

`
6 changes: 6 additions & 0 deletions report/text/writer.go
Expand Up @@ -3,6 +3,9 @@ package text
import (
"bufio"
"bytes"

// use go embed to import template
_ "embed"
ccojocar marked this conversation as resolved.
Show resolved Hide resolved
"fmt"
"io"
"strconv"
Expand All @@ -17,6 +20,9 @@ var (
errorTheme = color.New(color.FgLightWhite, color.BgRed)
warningTheme = color.New(color.FgBlack, color.BgYellow)
defaultTheme = color.New(color.FgWhite, color.BgBlack)

//go:embed template.txt
templateContent string
)

// WriteReport write a (colorized) report in text format
Expand Down