Skip to content

Commit

Permalink
Vite / React powered website
Browse files Browse the repository at this point in the history
Adding output formats as options on the website
  • Loading branch information
zegl committed Jul 12, 2024
1 parent 9e3fa5c commit a2e31d8
Show file tree
Hide file tree
Showing 21 changed files with 3,428 additions and 1,364 deletions.
61 changes: 47 additions & 14 deletions cmd/wasm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,26 @@
package main

import (
"bytes"
"fmt"
"io"
"os"
"strings"
"syscall/js"

t2html "github.com/buildkite/terminal-to-html"
"github.com/fatih/color"

"github.com/zegl/kube-score/config"
"github.com/zegl/kube-score/domain"
"github.com/zegl/kube-score/parser"
"github.com/zegl/kube-score/renderer/ci"
"github.com/zegl/kube-score/renderer/human"
"github.com/zegl/kube-score/renderer/json_v2"
"github.com/zegl/kube-score/renderer/junit"
"github.com/zegl/kube-score/renderer/sarif"
"github.com/zegl/kube-score/score"
"github.com/zegl/kube-score/score/checks"
"golang.org/x/term"
)

func main() {
Expand All @@ -33,15 +40,16 @@ func (inputReader) Name() string {
}

func handleScore(this js.Value, inputs []js.Value) interface{} {
if len(inputs) == 0 {
if len(inputs) != 2 {
fmt.Println("Unexpected number of arguments")
return "Unexpected number of arguments"
}

fmt.Println(inputs[0].String())
inputYaml := inputs[0].String()
format := inputs[1].String()

reader := &inputReader{
Reader: strings.NewReader(inputs[0].String()),
Reader: strings.NewReader(inputYaml),
}

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

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

card, err := score.Score(allObjs, allChecks, runConfig)
scoreCard, err := score.Score(allObjs, allChecks, runConfig)
if err != nil {
fmt.Println(err)
return string(err.Error())
}

color.NoColor = false
output, err := human.Human(card, 0, 110, true)
if err != nil {
fmt.Println(err)
return string(err.Error())
var r io.Reader
switch format {
case "json":
r = json_v2.Output(scoreCard)
case "human":
termWidth, _, err := term.GetSize(int(os.Stdin.Fd()))
// Assume a width of 80 if it can't be detected
if err != nil {
termWidth = 80
}
body, err := human.Human(scoreCard, 0, termWidth, true)
if err != nil {
fmt.Println(err)
return string(err.Error())
}

bodyBytes, err := io.ReadAll(body)
if err != nil {
fmt.Println(err)
return string(err.Error())
}

htmlBody := t2html.Render(bodyBytes)
r = bytes.NewReader(htmlBody)
case "ci":
r = ci.CI(scoreCard)
case "sarif":
r = sarif.Output(scoreCard)
case "junit":
r = junit.JUnit(scoreCard)
default:
return fmt.Errorf("error: unknown format")
}

body, err := io.ReadAll(output)
body, err := io.ReadAll(r)
fmt.Println("body", body)
if err != nil {
fmt.Println(err)
return string(err.Error())
}

htmlBody := t2html.Render(body)

return string(htmlBody)
return string(body)
}
2 changes: 1 addition & 1 deletion vercel.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"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",
"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",
"outputDirectory": "./web"
}
18 changes: 18 additions & 0 deletions web/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
root: true,
env: { browser: true, es2020: true },
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:react-hooks/recommended',
],
ignorePatterns: ['dist', '.eslintrc.cjs'],
parser: '@typescript-eslint/parser',
plugins: ['react-refresh'],
rules: {
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
}
25 changes: 25 additions & 0 deletions web/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
*.wasm

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
5 changes: 3 additions & 2 deletions web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ GOOS=js GOARCH=wasm go build -o ./web/main.wasm ./cmd/wasm/main.go
```

```bash
# Start a webserver serving files
python3 -m http.server
# Build and run web app (from this directory)
pnpm install
pnpm dev
```

## Hosting
Expand Down
Loading

0 comments on commit a2e31d8

Please sign in to comment.