From f1992e0dc6bdda7e0a3ae6962ea5e9d27e154805 Mon Sep 17 00:00:00 2001 From: Felipe Avelar Date: Wed, 24 Mar 2021 17:35:28 +0000 Subject: [PATCH] Embedding report resource files (#2446) Signed-off-by: Felipe Avelar --- go.sum | 2 ++ pkg/report/html.go | 50 ++++++++++++++++++++++------------------------ 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/go.sum b/go.sum index dfb972b5126..ab38897f0ee 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/pkg/report/html.go b/pkg/report/html.go index 20c838c2a8d..e46b6ea92fc 100644 --- a/pkg/report/html.go +++ b/pkg/report/html.go @@ -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("") //nolint + return template.HTML("") //nolint } // PrintHTMLReport creates a report file on HTML format @@ -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)