Skip to content

Commit

Permalink
Embedding report resource files (Checkmarx#2446)
Browse files Browse the repository at this point in the history
Signed-off-by: Felipe Avelar <[email protected]>
  • Loading branch information
felipe-avelar authored Mar 24, 2021
1 parent 982b102 commit f1992e0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,7 @@ golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20170114055629-f2499483f923/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -1566,6 +1567,7 @@ golang.org/x/tools v0.0.0-20200505023115-26f46d2f7ef8/go.mod h1:EkVYQZoAsY45+roY
golang.org/x/tools v0.0.0-20200512131952-2bc93b1c0c88/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200616133436-c1934b75d054/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.1.0 h1:po9/4sTYwZU9lPhi1tOrb4hCv3qrhiQ77LZfGa2OjwY=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
50 changes: 24 additions & 26 deletions pkg/report/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,51 @@ package report

import (
"bytes"
"fmt"
_ "embed" // used for embedding report static files
"html/template"
"os"
"path/filepath"
"runtime"
"strings"

"github.com/rs/zerolog/log"
"github.com/tdewolff/minify/v2"
minifyCSS "github.com/tdewolff/minify/v2/css"
minifyHtml "github.com/tdewolff/minify/v2/html"
)

const (
templateFile = "report.tmpl"
var (
//go:embed template/html/report.tmpl
htmlTemplate string
//go:embed template/html/report.css
cssTemplate string
//go:embed template/html/github.svg
githubSVG string
//go:embed template/html/info.svg
infoSVG string
//go:embed template/html/vulnerability_fill.svg
vulnerabilityFillSVG string
//go:embed template/html/vulnerability_out.svg
vulnerabilityOutSVG string
)

var templatePath = ""
var svgMap = map[string]string{
"github.svg": githubSVG,
"info.svg": infoSVG,
"vulnerability_fill.svg": vulnerabilityFillSVG,
"vulnerability_out.svg": vulnerabilityOutSVG,
}

func includeSVG(name string) template.HTML {
svg, err := os.ReadFile(filepath.Join(templatePath, name))
if err != nil {
log.Err(err).Msgf("failed to open svg: %s", name)
return ""
}
return template.HTML(string(svg)) //nolint
return template.HTML(svgMap[name]) //nolint
}

func includeCSS(name string) template.HTML {
css, err := os.ReadFile(filepath.Join(templatePath, name))
if err != nil {
log.Err(err).Msgf("failed to open svg: %s", name)
return ""
}
minifier := minify.New()
minifier.AddFunc("text/css", minifyCSS.Minify)
cssMinified, err := minifier.Bytes("text/css", css)
cssMinified, err := minifier.String("text/css", cssTemplate)
if err != nil {
return ""
}
return template.HTML("<style>" + string(cssMinified) + "</style>") //nolint
return template.HTML("<style>" + cssMinified + "</style>") //nolint
}

// PrintHTMLReport creates a report file on HTML format
Expand All @@ -51,17 +55,11 @@ func PrintHTMLReport(path, filename string, body interface{}) error {
filename += ".html"
}

_, templatePathFromStack, _, ok := runtime.Caller(0)
if !ok {
return fmt.Errorf("report error: Report template not found")
}
templatePath = templatePathFromStack
templateFuncs["includeSVG"] = includeSVG
templateFuncs["includeCSS"] = includeCSS

fullPath := filepath.Join(path, filename)
templatePath = filepath.Join(filepath.Dir(templatePath), "template", "html")
t := template.Must(template.New(templateFile).Funcs(templateFuncs).ParseFiles(filepath.Join(templatePath, templateFile)))
t := template.Must(template.New("report.tmpl").Funcs(templateFuncs).Parse(htmlTemplate))

_ = os.MkdirAll(path, os.ModePerm)
f, err := os.OpenFile(filepath.Clean(fullPath), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm)
Expand Down

0 comments on commit f1992e0

Please sign in to comment.