Skip to content

Commit

Permalink
fix: add changes according PR reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
olsova committed Dec 2, 2024
1 parent 0c4a378 commit ffcc7e1
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
branches:
- '**'
env:
GO_VERSION: '1.23.2'
GO_VERSION: '1.23.3'

jobs:
unittest:
Expand Down
14 changes: 7 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import (
)

func main() {
rootCmd, err := initFlags()
rootCmd, err := initRootCmd()
if err != nil {
log.Fatalf("%+v", err)
log.Fatalf("error initialising root cmd %+v", err)
}

if err = rootCmd.Execute(); err != nil {
log.Fatalf("%+v", err)
log.Fatalf("executing error %+v", err)
}
}

func initFlags() (*cobra.Command, error) {
func initRootCmd() (*cobra.Command, error) {
var fileName string
rootCmd := &cobra.Command{
Use: "scan2html",
Expand All @@ -33,18 +33,18 @@ func initFlags() (*cobra.Command, error) {
return runPlugin(fileName)
},
}
rootCmd.PersistentFlags().StringVar(&fileName, "file", "trivy-report.html", "file to save html report")
rootCmd.PersistentFlags().StringVar(&fileName, "output", "trivy-report.html", "file to save html report")
return rootCmd, nil
}

func runPlugin(fileName string) error {
inputData, err := io.ReadAll(os.Stdin)
if err != nil {
return xerrors.Errorf("error reading trivy output: %v\n", err)
return xerrors.Errorf("error reading trivy output: %w", err)
}
err = render.Render(fileName, inputData)
if err != nil {
return err
return xerrors.Errorf("error rendering trivy output: %w", err)
}

return nil
Expand Down
11 changes: 5 additions & 6 deletions render/render.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package render

import (
"bytes"
_ "embed"
"encoding/json"
"os"
Expand All @@ -15,8 +14,8 @@ import (
var htmlTmpl []byte

func Render(fileName string, inputData []byte) error {
var output types.Report
if err := json.NewDecoder(bytes.NewReader(inputData)).Decode(&output); err != nil {
var report types.Report
if err := json.Unmarshal(inputData, &report); err != nil {
return xerrors.Errorf("error decoding body: %v\n", err)
}

Expand All @@ -25,13 +24,13 @@ func Render(fileName string, inputData []byte) error {
return xerrors.Errorf("error parsing template: %v\n", err)
}

file, err := os.Create(fileName)
output, err := os.Create(fileName)
if err != nil {
return xerrors.Errorf("error creating file: %v\n", err)
}
defer file.Close()
defer output.Close()

if err := tmpl.Execute(file, output); err != nil {
if err := tmpl.Execute(output, report); err != nil {
return xerrors.Errorf("error executing template: %v\n", err)
}

Expand Down
5 changes: 1 addition & 4 deletions render/render_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package render

import (
"log"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -47,9 +46,7 @@ func TestRender(t *testing.T) {
expected, err := os.ReadFile(tt.goldenPath)
require.NoError(t, err)

log.Println(string(expected) == string(actual))

assert.Equal(t, string(expected), string(actual))
assert.Equal(t, expected, actual)
})
}
}
1 change: 0 additions & 1 deletion render/template/html.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="index.css">
<title>Trivy Report</title>

<style>
Expand Down

0 comments on commit ffcc7e1

Please sign in to comment.