-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
8 changed files
with
241 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,6 @@ node_modules/ | |
.idea/ | ||
.vscode/ | ||
dist/ | ||
first.html | ||
second.html | ||
**/.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters