Skip to content

Commit ff17c30

Browse files
authored
Use go embed for templates (#725)
1 parent 3eba7b8 commit ff17c30

File tree

4 files changed

+14
-22
lines changed

4 files changed

+14
-22
lines changed

report/html/template.go report/html/template.html

+1-18
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,3 @@
1-
// (c) Copyright 2016 Hewlett Packard Enterprise Development LP
2-
//
3-
// Licensed under the Apache License, Version 2.0 (the "License");
4-
// you may not use this file except in compliance with the License.
5-
// You may obtain a copy of the License at
6-
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
9-
// Unless required by applicable law or agreed to in writing, software
10-
// distributed under the License is distributed on an "AS IS" BASIS,
11-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
// See the License for the specific language governing permissions and
13-
// limitations under the License.
14-
15-
package html
16-
17-
const templateContent = `
181
<!doctype html>
192
<html lang="en">
203
<head>
@@ -454,4 +437,4 @@
454437
);
455438
</script>
456439
</body>
457-
</html>`
440+
</html>

report/html/writer.go

+6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package html
22

33
import (
4+
5+
// use go embed to import template
6+
_ "embed"
47
"html/template"
58
"io"
69

710
"github.com/securego/gosec/v2"
811
)
912

13+
//go:embed template.html
14+
var templateContent string
15+
1016
// WriteReport write a report in html format to the output writer
1117
func WriteReport(w io.Writer, data *gosec.ReportInfo) error {
1218
t, e := template.New("gosec").Parse(templateContent)

report/text/template.go report/text/template.txt

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
package text
2-
3-
const templateContent = `Results:
1+
Results:
42
{{range $filePath,$fileErrors := .Errors}}
53
Golang errors in file: [{{ $filePath }}]:
64
{{range $index, $error := $fileErrors}}
@@ -23,4 +21,3 @@ Golang errors in file: [{{ $filePath }}]:
2321
{{- danger .Stats.NumFound }}
2422
{{- end }}
2523

26-
`

report/text/writer.go

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package text
33
import (
44
"bufio"
55
"bytes"
6+
7+
// use go embed to import template
8+
_ "embed"
69
"fmt"
710
"io"
811
"strconv"
@@ -17,6 +20,9 @@ var (
1720
errorTheme = color.New(color.FgLightWhite, color.BgRed)
1821
warningTheme = color.New(color.FgBlack, color.BgYellow)
1922
defaultTheme = color.New(color.FgWhite, color.BgBlack)
23+
24+
//go:embed template.txt
25+
templateContent string
2026
)
2127

2228
// WriteReport write a (colorized) report in text format

0 commit comments

Comments
 (0)