Skip to content

Commit

Permalink
migrate to go from bash (#45)
Browse files Browse the repository at this point in the history
* refactor: migrate plugin from bash to golang

* feat: add help message

* build: add goreleaser

* refactor: add makeTrivyJsonReport function

* build: remove deprecated --rm-dist, add other os support

* refactor: change goreleaser binary name
  • Loading branch information
malikovmm committed Apr 13, 2023
1 parent 8cb153b commit 1427403
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 62 deletions.
35 changes: 20 additions & 15 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ on:
paths:
- "plugin.yaml"

permissions:
contents: write

jobs:
check-version:
name: "Check version"
Expand Down Expand Up @@ -45,27 +48,29 @@ jobs:
name: template
path: dist/*.html

release-content:
name: "Create GitHub Release"
release:
runs-on: ubuntu-latest
needs: [check-version, build]
needs: [build, check-version]
if: ${{ needs.check-version.outputs.next_version != '' }}
permissions:
contents: write
steps:
- name: Checkout
- name: Checkout code
uses: actions/checkout@v3
- name: Find and Replace
run: sed -i 's/__VERSION__/${{needs.check-version.outputs.next_version}}/g' scan2html
with:
fetch-depth: 0
- uses: actions/download-artifact@v3
with:
name: template
path: .
- name: Compress
id: compress
run: tar -czvf scan2html.tar.gz first.html second.html LICENSE scan2html plugin.yaml
- name: Release
uses: softprops/action-gh-release@v0.1.15
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '1.19.0'
- run: git tag v${{ needs.check-version.outputs.next_version }}
- name: GoReleaser
uses: goreleaser/goreleaser-action@v4
with:
files: scan2html.tar.gz
tag_name: v${{needs.check-version.outputs.next_version}}
version: latest
args: release -f=goreleaser.yml --clean --timeout 60m
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GORELEASER_CURRENT_TAG: v${{ needs.check-version.outputs.next_version }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ node_modules/
.idea/
.vscode/
dist/
first.html
second.html
**/.DS_Store
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/afdesk/scan2html

go 1.19

require golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 h1:ooxPy7fPvB4kwsA2h+iBNHkAbp/4JxTSwCmvdjEYmug=
golang.org/x/exp v0.0.0-20230321023759-10a507213a29/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
55 changes: 55 additions & 0 deletions goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
project_name: scan2html
builds:
-
main: main.go
binary: scan2html
ldflags:
- -s -w
- "-extldflags '-static'"
- -X main.version={{.Version}}
goos:
- darwin
- linux
- freebsd
- windows
goarch:
- amd64
- 386
- arm
- arm64
- s390x
- ppc64le
goarm:
- 7

ignore:
- goos: darwin
goarch: 386
- goos: freebsd
goarch: arm
- goos: freebsd
goarch: arm64
- goos: windows
goarch: 386
- goos: windows
goarch: arm
- goos: windows
goarch: arm64
- goos: windows
goarch: s390x
- goos: windows
goarch: ppc64le

archives:
-
format: tar.gz
name_template: "{{.ProjectName}}_{{.Version}}_{{.Os}}-{{.Arch}}"
files:
- README.md
- LICENSE
- plugin.yaml
- first.html
- second.html
format_overrides:
- goos: windows
format: zip
99 changes: 99 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package main

import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"time"

"golang.org/x/exp/slices"
)

var (
tempJsonFileName = "scan2html-report-temp.json"
version = "dev"
)

func main() {
if slices.Contains(os.Args, "-h") || slices.Contains(os.Args, "--help") {
helpMessage()
}

tempFileName := filepath.Join(os.TempDir(), tempJsonFileName)
defer removeFile(tempFileName)
if err := makeTrivyJsonReport(tempFileName); err != nil {
log.Fatalf("failed to make trivy report: %v", err)
}

firstHTML, err := readPluginFile("first.html")
if err != nil {
log.Fatalf("failed to read html file: %v", err)
}

reportJson, err := os.ReadFile(tempFileName)
if err != nil {
log.Fatalf("failed to read json file: %v", err)
}

secondHTML, err := readPluginFile("second.html")
if err != nil {
log.Fatalf("failed to read html file: %v", err)
}

createdAt := time.Now().Unix()
argsStr := strings.Join(os.Args[1:len(os.Args)-1], " ")
output := []byte(fmt.Sprintf("const trivyData = %s;\nconst createdAt = %d;\nconst args = \"%s\";\n%s",
reportJson, createdAt, argsStr, secondHTML))

err = os.WriteFile(os.Args[len(os.Args)-1], append(firstHTML, output...), 0600)
if err != nil {
log.Fatalf("failed to write output file: %v", err)
}
}

func removeFile(file string) {
if err := os.Remove(file); err != nil {
log.Fatalf("failed to remove file %v", err)
}
}
func readPluginFile(fileName string) ([]byte, error) {
ex, err := os.Executable()
if err != nil {
return nil, err
}
return os.ReadFile(filepath.Join(filepath.Dir(ex), fileName))
}

func helpMessage() {
_, err := fmt.Printf(`
scan2html v%s
Usage: trivy scan2html [-h,--help] command target filename
A Trivy plugin that scans and output the results to a html file.
Options:
-h, --help Show usage.
Examples:
# Scan 'alpine:latest' image
trivy scan2html image alpine:latest result.html
# Scan local folder
trivy scan2html fs . result.html
`, version)
if err != nil {
log.Fatalf("Failed to display help message %v", err)
}
os.Exit(0)
}

func makeTrivyJsonReport(outputFileName string) error {
trivyCommand := os.Args[1 : len(os.Args)-1]
cmdArgs := append(trivyCommand, "--format", "json", "--output", outputFileName)
cmd := exec.Command("trivy", cmdArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err
}
return nil
}
60 changes: 58 additions & 2 deletions plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,63 @@ usage: scan targets into a html file
description: |-
A Trivy plugin that scans and outputs the results to a html file.
trivy scan2html [-h,--help] command target filename
platforms:
-
uri: https://github.com/afdesk/scan2html/releases/download/v0.1.4/scan2html.tar.gz
- selector:
os: linux
arch: amd64
uri: https://github.com/afdesk/scan2html/releases/download/v0.1.3/scan2html_0.1.3_linux-amd64.tar.gz
bin: ./scan2html
- selector:
os: linux
arch: arm
uri: https://github.com/afdesk/scan2html/releases/download/v0.1.3/scan2html_0.1.3_linux-arm.tar.gz
bin: ./scan2html
- selector:
os: linux
arch: arm64
uri: https://github.com/afdesk/scan2html/releases/download/v0.1.3/scan2html_0.1.3_linux-arm64.tar.gz
bin: ./scan2html
- selector:
os: linux
arch: s390x
uri: https://github.com/afdesk/scan2html/releases/download/v0.1.3/scan2html_0.1.3_linux-s390x.tar.gz
bin: ./scan2html
- selector:
os: linux
arch: ppc64le
uri: https://github.com/afdesk/scan2html/releases/download/v0.1.3/scan2html_0.1.3_linux-ppc64le.tar.gz
bin: ./scan2html
- selector:
os: linux
arch: 386
uri: https://github.com/afdesk/scan2html/releases/download/v0.1.3/scan2html_0.1.3_linux-386.tar.gz
bin: ./scan2html

- selector:
os: darwin
arch: amd64
uri: https://github.com/afdesk/scan2html/releases/download/v0.1.3/scan2html_0.1.3_darwin-amd64.tar.gz
bin: ./scan2html
- selector:
os: darwin
arch: arm64
uri: https://github.com/afdesk/scan2html/releases/download/v0.1.3/scan2html_0.1.3_darwin-arm64.tar.gz
bin: ./scan2html

- selector:
os: freebsd
arch: 386
uri: https://github.com/afdesk/scan2html/releases/download/v0.1.3/scan2html_0.1.3_freebsd-386.tar.gz
bin: ./scan2html
- selector:
os: freebsd
arch: amd64
uri: https://github.com/afdesk/scan2html/releases/download/v0.1.3/scan2html_0.1.3_freebsd-amd64.tar.gz
bin: ./scan2html

- selector:
os: windows
arch: amd64
uri: https://github.com/afdesk/scan2html/releases/download/v0.1.3/scan2html_0.1.3_windows-amd64.zip
bin: ./scan2html
45 changes: 0 additions & 45 deletions scan2html

This file was deleted.

0 comments on commit 1427403

Please sign in to comment.