Skip to content

Commit 4c0e41b

Browse files
committed
Vite / React powered website
Adding output formats as options on the website
1 parent 95589f8 commit 4c0e41b

21 files changed

+3428
-1364
lines changed

cmd/wasm/main.go

+47-14
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,26 @@
44
package main
55

66
import (
7+
"bytes"
78
"fmt"
89
"io"
10+
"os"
911
"strings"
1012
"syscall/js"
1113

1214
t2html "github.com/buildkite/terminal-to-html"
13-
"github.com/fatih/color"
15+
1416
"github.com/zegl/kube-score/config"
1517
"github.com/zegl/kube-score/domain"
1618
"github.com/zegl/kube-score/parser"
19+
"github.com/zegl/kube-score/renderer/ci"
1720
"github.com/zegl/kube-score/renderer/human"
21+
"github.com/zegl/kube-score/renderer/json_v2"
22+
"github.com/zegl/kube-score/renderer/junit"
23+
"github.com/zegl/kube-score/renderer/sarif"
1824
"github.com/zegl/kube-score/score"
1925
"github.com/zegl/kube-score/score/checks"
26+
"golang.org/x/term"
2027
)
2128

2229
func main() {
@@ -33,15 +40,16 @@ func (inputReader) Name() string {
3340
}
3441

3542
func handleScore(this js.Value, inputs []js.Value) interface{} {
36-
if len(inputs) == 0 {
43+
if len(inputs) != 2 {
3744
fmt.Println("Unexpected number of arguments")
3845
return "Unexpected number of arguments"
3946
}
4047

41-
fmt.Println(inputs[0].String())
48+
inputYaml := inputs[0].String()
49+
format := inputs[1].String()
4250

4351
reader := &inputReader{
44-
Reader: strings.NewReader(inputs[0].String()),
52+
Reader: strings.NewReader(inputYaml),
4553
}
4654

4755
files := []domain.NamedReader{reader}
@@ -63,27 +71,52 @@ func handleScore(this js.Value, inputs []js.Value) interface{} {
6371

6472
allChecks := score.RegisterAllChecks(allObjs, checksConfig, runConfig)
6573

66-
card, err := score.Score(allObjs, allChecks, runConfig)
74+
scoreCard, err := score.Score(allObjs, allChecks, runConfig)
6775
if err != nil {
6876
fmt.Println(err)
6977
return string(err.Error())
7078
}
7179

72-
color.NoColor = false
73-
output, err := human.Human(card, 0, 110, true)
74-
if err != nil {
75-
fmt.Println(err)
76-
return string(err.Error())
80+
var r io.Reader
81+
switch format {
82+
case "json":
83+
r = json_v2.Output(scoreCard)
84+
case "human":
85+
termWidth, _, err := term.GetSize(int(os.Stdin.Fd()))
86+
// Assume a width of 80 if it can't be detected
87+
if err != nil {
88+
termWidth = 80
89+
}
90+
body, err := human.Human(scoreCard, 0, termWidth, true)
91+
if err != nil {
92+
fmt.Println(err)
93+
return string(err.Error())
94+
}
95+
96+
bodyBytes, err := io.ReadAll(body)
97+
if err != nil {
98+
fmt.Println(err)
99+
return string(err.Error())
100+
}
101+
102+
htmlBody := t2html.Render(bodyBytes)
103+
r = bytes.NewReader(htmlBody)
104+
case "ci":
105+
r = ci.CI(scoreCard)
106+
case "sarif":
107+
r = sarif.Output(scoreCard)
108+
case "junit":
109+
r = junit.JUnit(scoreCard)
110+
default:
111+
return fmt.Errorf("error: unknown format")
77112
}
78113

79-
body, err := io.ReadAll(output)
114+
body, err := io.ReadAll(r)
80115
fmt.Println("body", body)
81116
if err != nil {
82117
fmt.Println(err)
83118
return string(err.Error())
84119
}
85120

86-
htmlBody := t2html.Render(body)
87-
88-
return string(htmlBody)
121+
return string(body)
89122
}

vercel.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"buildCommand": "dnf install -y go && go env && go install golang.org/dl/go1.22.5@latest && /vercel/go/bin/go1.22.5 download && GOOS=js GOARCH=wasm /vercel/go/bin/go1.22.5 build -o ./web/main.wasm ./cmd/wasm/main.go",
2+
"buildCommand": "dnf install -y go && go install golang.org/dl/go1.22.5@latest && /vercel/go/bin/go1.22.5 download && GOOS=js GOARCH=wasm /vercel/go/bin/go1.22.5 build -o ./web/public/main.wasm ./cmd/wasm/main.go && cd web && pnpm install && pnpm build",
33
"outputDirectory": "./web"
44
}

web/.eslintrc.cjs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
root: true,
3+
env: { browser: true, es2020: true },
4+
extends: [
5+
'eslint:recommended',
6+
'plugin:@typescript-eslint/recommended',
7+
'plugin:react-hooks/recommended',
8+
],
9+
ignorePatterns: ['dist', '.eslintrc.cjs'],
10+
parser: '@typescript-eslint/parser',
11+
plugins: ['react-refresh'],
12+
rules: {
13+
'react-refresh/only-export-components': [
14+
'warn',
15+
{ allowConstantExport: true },
16+
],
17+
},
18+
}

web/.gitignore

+25
Original file line numberDiff line numberDiff line change
@@ -1 +1,26 @@
11
*.wasm
2+
3+
# Logs
4+
logs
5+
*.log
6+
npm-debug.log*
7+
yarn-debug.log*
8+
yarn-error.log*
9+
pnpm-debug.log*
10+
lerna-debug.log*
11+
12+
node_modules
13+
dist
14+
dist-ssr
15+
*.local
16+
17+
# Editor directories and files
18+
.vscode/*
19+
!.vscode/extensions.json
20+
.idea
21+
.DS_Store
22+
*.suo
23+
*.ntvs*
24+
*.njsproj
25+
*.sln
26+
*.sw?

web/README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ GOOS=js GOARCH=wasm go build -o ./web/main.wasm ./cmd/wasm/main.go
1212
```
1313

1414
```bash
15-
# Start a webserver serving files
16-
python3 -m http.server
15+
# Build and run web app (from this directory)
16+
pnpm install
17+
pnpm dev
1718
```
1819

1920
## Hosting

0 commit comments

Comments
 (0)