Skip to content

Commit

Permalink
feat: implement html server
Browse files Browse the repository at this point in the history
  • Loading branch information
Zxilly committed Apr 21, 2024
1 parent 1b04c2d commit f4bf4b2
Show file tree
Hide file tree
Showing 19 changed files with 288 additions and 47 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/build-gsa.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: goreleaser

on:
push:

permissions:
contents: write

jobs:
build-ui:
uses: ./.github/workflows/build-ui.yml

goreleaser:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-go@v4
with:
go-version: stable

- name: Download UI file
uses: actions/download-artifact@v4
with:
name: ui

- name: Move UI file
run: mv index.html internal/ui/index.html

- name: Run goreleaser
10 changes: 10 additions & 0 deletions .github/workflows/build-ui.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
paths:
- 'ui/**'
- '.github/workflows/build-ui.yml'
workflow_call:

concurrency:
group: ${{ github.ref }}-ui
Expand Down Expand Up @@ -43,6 +44,7 @@ jobs:
run: pnpm build

- name: Release
if: github.event_name == 'push'
uses: ncipollo/release-action@v1
with:
artifactErrorsFailBuild: true
Expand All @@ -51,6 +53,14 @@ jobs:
artifacts: 'ui/dist/index.html'
tag: ui
commit: master
prerelease: true

- name: Upload artifact
if: github.event_name == 'workflow_call'
uses: actions/upload-artifact@v4
with:
name: ui
path: ui/dist/index.html



44 changes: 44 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json

version: 1

before:
hooks:
- go mod tidy

builds:
- binary: gsa
ldflags:
- "-s -w"
env:
- CGO_ENABLED=0
goos:
- linux
- darwin
- windows
goarch:
- amd64
- arm64
flags: -trimpath
tags:
- embed

archives:
- format: tar.gz
name_template: >-
{{- .ProjectName }}_
{{- .Os }}_
{{- .Arch }}
format_overrides:
- goos: windows
format: zip

checksum:
name_template: 'checksums.txt'

changelog:
sort: asc
filters:
exclude:
- "^docs:"
- "^test:"
32 changes: 31 additions & 1 deletion cmd/gsa/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"github.com/Zxilly/go-size-analyzer/internal"
"github.com/Zxilly/go-size-analyzer/internal/printer"
"github.com/Zxilly/go-size-analyzer/internal/utils"
"github.com/Zxilly/go-size-analyzer/internal/web"
"github.com/ZxillyFork/go-flags"
"github.com/pkg/browser"
"log/slog"
"os"
"runtime/debug"
Expand All @@ -25,6 +27,12 @@ type Options struct {
Indent *int `long:"indent" description:"Indentation for json output"`
} `group:"Json Options"`

HtmlOptions struct {
Web bool `long:"web" description:"Start web server for html output, this option will override format to html and ignore output option"`
Listen string `long:"listen" description:"Listen address" default:":8080"`
Open bool `long:"open" description:"Open browser"`
} `group:"Html Options"`

Output string `short:"o" long:"output" description:"Write to file"`
Version bool `long:"version" description:"Show version"`

Expand Down Expand Up @@ -85,18 +93,40 @@ func main() {
Indent: options.JsonOptions.Indent,
})
case "html":
b = printer.Html(result)
default:
slog.Error(fmt.Sprintf("Invalid format: %s", options.Format))
os.Exit(1)
}

if options.Format == "html" {
if options.HtmlOptions.Web {
go web.HostServer(string(b), options.HtmlOptions.Listen)

url := utils.GetUrlFromListen(options.HtmlOptions.Listen)

fmt.Println("Server started at", url)

if options.HtmlOptions.Open {
err = browser.OpenURL(url)
if err != nil {
slog.Error(fmt.Sprintf("Error: %v", err))
return
}
}

utils.WaitSignal()
return
}
}

if options.Output != "" {
err := os.WriteFile(options.Output, b, 0644)
if err != nil {
slog.Error(fmt.Sprintf("Error: %v", err))
os.Exit(1)
}
} else {
fmt.Println(b)
fmt.Println(string(b))
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/dustin/go-humanize v1.0.1
github.com/goretk/gore v0.11.5
github.com/jedib0t/go-pretty/v6 v6.5.5
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
github.com/samber/lo v1.39.0
github.com/schollz/progressbar/v3 v3.14.2
go4.org/intern v0.0.0-20230525184215-6c62f75575cb
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZ
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
Expand All @@ -39,6 +41,7 @@ golang.org/x/arch v0.7.0 h1:pskyeJh/3AmoQ8CPE95vxHLqp1G1GfGNXTmcl9NEKTc=
golang.org/x/arch v0.7.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/exp v0.0.0-20240318143956-a85f2c67cd81 h1:6R2FC06FonbXQ8pK11/PDFY6N6LWlf9KlzibaCapmqc=
golang.org/x/exp v0.0.0-20240318143956-a85f2c67cd81/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
Expand Down
2 changes: 1 addition & 1 deletion internal/entity/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type File struct {
}

func (f *File) MarshalJSON() ([]byte, error) {
if global.ShowFileSizes {
if global.HideDetail {
size := uint64(0)
for _, fn := range f.Functions {
size += fn.Size
Expand Down
4 changes: 2 additions & 2 deletions internal/global/global.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package global

// ShowFileSizes an ugly hack to control the output format of file
// HideDetail an ugly hack to control the output format of file
// Should find a replacement for this
var ShowFileSizes = false
var HideDetail = false
14 changes: 14 additions & 0 deletions internal/printer/html.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package printer

import (
"github.com/Zxilly/go-size-analyzer/internal"
"github.com/Zxilly/go-size-analyzer/internal/ui"
"strings"
)

const ReplacedStr = `"GSA_PACKAGE_DATA"`

func Html(r *internal.Result) []byte {
json := Json(r, &JsonOption{hideDetail: true})
return []byte(strings.Replace(ui.GetTemplate(), ReplacedStr, string(json), 1))
}
4 changes: 2 additions & 2 deletions internal/printer/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
)

func Json(r *internal.Result, options *JsonOption) []byte {
if options.minify {
global.ShowFileSizes = true
if options.hideDetail {
global.HideDetail = true
}

var b []byte
Expand Down
4 changes: 2 additions & 2 deletions internal/printer/options.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package printer

type JsonOption struct {
Indent *int
minify bool
Indent *int
hideDetail bool
}

type TextOption struct {
Expand Down
1 change: 1 addition & 0 deletions internal/ui/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
index.html
14 changes: 14 additions & 0 deletions internal/ui/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build embed

package ui

import (
_ "embed"
)

//go:embed index.html
var tmpl string

func GetTemplate() string {
return tmpl
}
33 changes: 33 additions & 0 deletions internal/ui/net.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//go:build !embed

package ui

import (
"fmt"
"io"
"log/slog"
"net/http"
"os"
)

const BaseUrl = "https://github.com/Zxilly/go-size-analyzer/releases/download/ui/index.html"

func GetTemplate() string {
slog.Info("Downloading template")
resp, err := http.Get(BaseUrl)
if err != nil {
slog.Error(fmt.Sprintf("Error: %v", err))
os.Exit(1)
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)

body, err := io.ReadAll(resp.Body)
if err != nil {
slog.Error(fmt.Sprintf("Error: %v", err))
os.Exit(1)
}

return string(body)
}
17 changes: 17 additions & 0 deletions internal/utils/addr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package utils

import (
"log/slog"
"os"
"strings"
)

func GetUrlFromListen(listen string) string {
// get port from listen
parts := strings.Split(listen, ":")
if parts == nil || len(parts) < 2 {
slog.Error("invalid listen address", "listen", listen)
os.Exit(1)
}
return "http://localhost:" + parts[1]
}
45 changes: 45 additions & 0 deletions internal/utils/output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package utils

import (
"io"
"log/slog"
"os"
"sync"
)

func InitLogger(level slog.Level) {
slog.SetDefault(slog.New(slog.NewTextHandler(Stdout, &slog.HandlerOptions{
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
// remove time
if a.Key == "time" {
return slog.Attr{}
}
return a
},
Level: level,
})))
}

type SyncOutput struct {
sync.Mutex
output io.Writer
}

func (s *SyncOutput) Write(p []byte) (n int, err error) {
s.Lock()
defer s.Unlock()
return s.output.Write(p)
}

func (s *SyncOutput) SetOutput(output io.Writer) {
s.Lock()
defer s.Unlock()
s.output = output
}

var Stdout = &SyncOutput{
Mutex: sync.Mutex{},
output: os.Stderr,
}

var _ io.Writer = Stdout
17 changes: 17 additions & 0 deletions internal/utils/signal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package utils

import (
"fmt"
"os"
"os/signal"
"syscall"
)

// WaitSignal waits for a Ctrl+C signal to exit the program.
func WaitSignal() {
done := make(chan os.Signal, 1)
signal.Notify(done, syscall.SIGINT, syscall.SIGTERM)

fmt.Println("Press Ctrl+C to exit")
<-done
}
Loading

0 comments on commit f4bf4b2

Please sign in to comment.