Skip to content

Commit

Permalink
Setup version handling, pipelines, updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
baalimago committed Aug 18, 2024
1 parent 20d754f commit ecc9954
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 11 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Simple Go Pipeline - release

on:
push:
tags:
- 'v[0-9]+.[0-9]+.[0-9]+'
jobs:
call-workflow:
uses: baalimago/simple-go-pipeline/.github/workflows/[email protected]
with:
go-version: '1.22'
project-name: wd-40
branch: main
version-var: "github.com/baalimago/wd-40/cmd/version.BUILD_VERSION"

13 changes: 13 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Simple Go Pipeline - validate

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
call-workflow:
uses: baalimago/simple-go-pipeline/.github/workflows/validate.yml@main
with:
go-version: '1.22'
53 changes: 51 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,55 @@
# (W)eb (D)evelopment-(40)
[![Go Report Card](https://goreportcard.com/badge/github.com/baalimago/wd-40)](https://goreportcard.com/report/github.com/baalimago/wd-40)
[![wakatime](https://wakatime.com/badge/user/018cc8d2-3fd9-47ef-81dc-e4ad645d5f34/project/3bc921ec-dc23-4222-bf00-578f2eda0cbd.svg)](https://wakatime.com/badge/user/018cc8d2-3fd9-47ef-81dc-e4ad645d5f34/project/3bc921ec-dc23-4222-bf00-578f2eda0cbd)

This is a static webserver which auto-reloads HTML files on any local filechanges.
Test coverage:

This is a static webserver which hot-reloads your web-browser on any local filechanges.

## Usage
`wd-40 serve <relative directory>` or `wd-40 serve`
`wd-40 s|serve <relative directory>` or `wd-40 s|serve`

## Architecture
1. First the content of the website is copied to a temporary directory
1. At every file, the MIME type is inspected, if it's text/html, a `delta-streamer.js` script is injected
1. The web server is started, hosting the _mirrored_ content
1. The `delta-streamer.js` in turn sets up a websocket connection to wd-40
1. The original file system is monitored, on any file changes:
1. the new file is copied to the mirror
1. the file name is propagated to the browser via the websocket
1. if the browser's origin matches the recently updated file, the browser is told to reload via javascript

```
┌───────────────┐
│ Web Developer │
└───────┬───────┘
[writes <content>]
┌─────────────────────────────┐ ┌─────────────────────┐
│ website-directory/<content> │ │ file system notify │
└─────────────┬───────────────┘ └─────────┬───────────┘
│ │
│ [update mirrored content]
▼ │
┌────────────────────┐ │
│ ws-script injector │◄──────────────────────┘
└─────────┬──────────┘
┌────────────────────────┐
│ tmp-abcd1234/<content> │
└───────────┬────────────┘
[serves <content>]
│ ┌────────────────────────┐
▼ │ Browser │
┌──────────────────────────────┐ │ │
│ Web Server │ │ ┌────┐ ┌───────────┐ │
│ [localhost:<port>/<content>] │◄───[reload────┼─►│ ws │ │ <content> │ │
└──────────────────────────────┘ page] │ └────┘ └───────────┘ │
│ │
└────────────────────────┘
```
25 changes: 16 additions & 9 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"text/tabwriter"

"github.com/baalimago/wd-40/cmd/serve"
"github.com/baalimago/wd-40/cmd/version"
)

var commands = map[string]Command{
"serve": serve.Command(),
"s|serve": serve.Command(),
"v|version": version.Command(),
}

func Parse(args []string) (Command, error) {
Expand All @@ -27,11 +29,16 @@ func Parse(args []string) (Command, error) {
cmdCandidate = arg
break
}
cmd, exists := commands[cmdCandidate]
if !exists {
return nil, ArgNotFoundError(cmdCandidate)
for cmdNameWithShortcut, cmd := range commands {
for _, cmdName := range strings.Split(cmdNameWithShortcut, "|") {
exists := cmdName == cmdCandidate
if exists {
return cmd, nil
}
}
}
return cmd, nil

return nil, ArgNotFoundError(cmdCandidate)
}

func formatCommandDescriptions() string {
Expand All @@ -46,10 +53,10 @@ func formatCommandDescriptions() string {

const usage = `== Web Development 40 ==
This tool is designed to ease the web devleopment hassles by
automatically attaching a script which sets up websocket. Through
this websocket, dynamic hot reloads of file changes are streamed.. somehow
haven't figured out that part yet.
This tool is designed to enable hot reload for any statically hosted web development.
It injects a websocket script (in a mirrored version of the file) into html pages
and uses the fsnotify (cross-platform 'inotify' wrapper) packge to detect filechanges.
On filechanges, the websocket will trigger a reload of the page.
The 40 is only to enable rust-repellant properties.
Expand Down
11 changes: 11 additions & 0 deletions cmd/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ func Test_Parse(t *testing.T) {
}
})

t.Run("it should return command if second argument specifies shortcut of specific command", func(t *testing.T) {
want := serve.Command()
got, err := Parse([]string{"/some/cli/path", "s"})
if err != nil {
t.Fatalf(": %v", err)
}
if got.Describe() != want.Describe() {
t.Fatalf("expected: %v, got: %v", want, got)
}
})

t.Run("it should return error if command doesnt exist", func(t *testing.T) {
badArg := "blheruh"
want := ArgNotFoundError(badArg)
Expand Down
59 changes: 59 additions & 0 deletions cmd/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package version

import (
"context"
"errors"
"flag"
"fmt"
"runtime/debug"
)

// Set with buildflag if built in pipeline and not using go install
var (
BUILD_VERSION = ""
BUILD_CHECKSUM = ""
)

type command struct{}

// Describe the version command
func (c command) Describe() string {
return "print the version of wd-40"
}

// Flagset for version, currently empty
func (c command) Flagset() *flag.FlagSet {
return flag.NewFlagSet("version", flag.ExitOnError)
}

// Help by printing out help
func (c command) Help() string {
return "Print the version of wd-40"
}

// Run the command, printing the version using either the debugbuild or tagged version
func (c command) Run(context.Context) error {
bi, ok := debug.ReadBuildInfo()
if !ok {
return errors.New("failed to read build info")
}
version := bi.Main.Version
checksum := bi.Main.Sum
if version == "" || version == "(devel)" {
version = BUILD_VERSION
}
if checksum == "" {
checksum = BUILD_CHECKSUM
}
fmt.Printf("version: %v, go version: %v, checksum: %v\n", version, bi.GoVersion, checksum)
return nil
}

// Setup the command
func (c command) Setup() error {
return nil
}

func Command() *command {
return &command{}
}
1 change: 1 addition & 0 deletions internal/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package internal

0 comments on commit ecc9954

Please sign in to comment.