Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ jobs:
- name: Verify modules
run: make verify

- name: Install Tailwind CSS
env:
TAILWIND_VERSION: v4.3.5 # keep in sync with the header of static/app.css
run: |
curl -sSfL -o "$RUNNER_TEMP/tailwindcss" \
"https://github.com/tailwindlabs/tailwindcss/releases/download/${TAILWIND_VERSION}/tailwindcss-linux-x64"
chmod +x "$RUNNER_TEMP/tailwindcss"

- name: CSS is up to date
run: make css-check TAILWIND="$RUNNER_TEMP/tailwindcss"

- name: Lint
run: make lint

Expand Down
25 changes: 24 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
# golangci-lint must be installed and in PATH (CI installs it via
# golangci/golangci-lint-action). Override with: make lint GOLANGCI_LINT=/path/to/bin
GOLANGCI_LINT ?= golangci-lint
# The Tailwind CSS v4 standalone CLI is only needed to rebuild or check
# static/app.css. CI pins its version; use the same one locally, since
# css-check compares the output byte for byte.
# Override with: make css TAILWIND=/path/to/tailwindcss
TAILWIND ?= tailwindcss

APP := fromscope
CMD := .

.DEFAULT_GOAL := help

.PHONY: help require-golangci-lint fmt lint test cover vuln tidy-check verify check build clean
.PHONY: help require-golangci-lint fmt lint test cover vuln tidy-check verify check build require-tailwind css css-check clean

help: ## Show this help
@grep -E '^[a-zA-Z_-]+:.*?## ' $(MAKEFILE_LIST) | \
Expand All @@ -20,6 +25,13 @@ require-golangci-lint:
exit 1; \
}

require-tailwind:
@command -v $(TAILWIND) >/dev/null 2>&1 || { \
echo "error: '$(TAILWIND)' not found in PATH." >&2; \
echo "Install it: https://tailwindcss.com/docs/installation/tailwind-cli" >&2; \
exit 1; \
}

fmt: require-golangci-lint ## Format code (gofumpt + goimports)
$(GOLANGCI_LINT) fmt

Expand Down Expand Up @@ -47,5 +59,16 @@ check: tidy-check verify lint test vuln ## Run every quality check (used by CI)
build: ## Build the binary
go build -trimpath -o $(APP) $(CMD)

css: require-tailwind ## Build static/app.css from input.css and the templates
$(TAILWIND) -i input.css -o static/app.css --minify

css-check: require-tailwind ## Fail if static/app.css is out of date with the templates
@tmp=$$(mktemp) || exit 1; trap 'rm -f "$$tmp"' EXIT; \
$(TAILWIND) -i input.css -o "$$tmp" --minify || exit 1; \
cmp -s "$$tmp" static/app.css || { \
echo "error: static/app.css is out of date, run: make css" >&2; \
exit 1; \
}

clean: ## Remove build artifacts
rm -f $(APP) cover.out
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@ load any CSV and offer filtering and grouping on arbitrary columns.
Early development. What works today:

- Loading a CSV file with a header row into memory.
- Dropping rows whose `tiene_from` column is `no`.
- Dropping rows whose `tiene_from` column is `no`, then hiding that column.
- Serving the resulting table as a web page on `-addr` (default `:8080`).

Planned:

- Web interface to browse the data (served on `-addr`, default `:8080`).
- Filters and groupings by column.
- Support for arbitrary CSV formats.

## Expected CSV format

- First line is a header with the column names (surrounding spaces are trimmed).
- Every row must have the same number of fields as the header.
- A `tiene_from` column must exist; rows where it equals `no` are discarded.
- A `tiene_from` column must exist; rows where it equals `no` are discarded,
and the column itself is not shown in the web view.

Example:

Expand All @@ -56,12 +57,14 @@ go build
./fromscope -csv path/to/file.csv
```

Then open <http://localhost:8080> in your browser.

### Flags

| Flag | Default | Description |
|---------|---------|-------------------------------------|
| `-csv` | — | CSV file to load (required) |
| `-addr` | `:8080` | TCP address to listen on (planned) |
| `-addr` | `:8080` | TCP address to listen on |

## Development

Expand All @@ -73,8 +76,20 @@ make help # list the available targets
make fmt # format the code (fixes most lint issues)
make check # run every quality check, the same as CI
make build # build the fromscope binary
make css # rebuild static/app.css after changing classes in templates/
```

The stylesheet is built with the [Tailwind CSS](https://tailwindcss.com/docs/installation/tailwind-cli)
v4 standalone CLI (override the binary with `make css TAILWIND=/path/to/tailwindcss`).
The generated `static/app.css` is committed, so building the Go binary does not
require Tailwind. CI runs `make css-check` to make sure it matches the templates;
use the same Tailwind version as CI (pinned in `.github/workflows/ci.yml`), since
the output is compared byte for byte.

[htmx](https://htmx.org/) 4.0.2 is vendored in `static/htmx.min.js` (the
minified file does not include its version). To update it, replace the file
with the `dist/htmx.min.js` of the new release and change the version here.

## License

[MIT](LICENSE)
17 changes: 17 additions & 0 deletions data.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,20 @@ func (t *Table) DropRows(column, value string) (int, error) {

return before - len(t.Rows), nil
}

// DropColumn removes the named column from Columns and from every row.
// It returns an error, leaving the table unchanged, if the column doesn't exist.
func (t *Table) DropColumn(column string) error {
idx := t.ColumnIndex(column)
if idx == -1 {
return fmt.Errorf("column %q not found", column)
}

t.Columns = slices.Delete(t.Columns, idx, idx+1)

for i, row := range t.Rows {
t.Rows[i] = slices.Delete(row, idx, idx+1)
}

return nil
}
65 changes: 65 additions & 0 deletions data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,68 @@ func TestDropRows(t *testing.T) {
}
})
}

func TestDropColumn(t *testing.T) {
newTable := func() *Table {
return &Table{
Columns: []string{"repo", "has_from", "image"},
Rows: [][]string{
{"a", "yes", "alpine"},
{"b", "no", "debian"},
},
}
}

tests := []struct {
name string
column string
wantColumns []string
wantRows [][]string
}{
{
name: "middle column",
column: "has_from",
wantColumns: []string{"repo", "image"},
wantRows: [][]string{{"a", "alpine"}, {"b", "debian"}},
},
{
name: "first column",
column: "repo",
wantColumns: []string{"has_from", "image"},
wantRows: [][]string{{"yes", "alpine"}, {"no", "debian"}},
},
{
name: "last column",
column: "image",
wantColumns: []string{"repo", "has_from"},
wantRows: [][]string{{"a", "yes"}, {"b", "no"}},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
table := newTable()

if err := table.DropColumn(tt.column); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !slices.Equal(table.Columns, tt.wantColumns) {
t.Errorf("columns = %q, want %q", table.Columns, tt.wantColumns)
}
if !slices.EqualFunc(table.Rows, tt.wantRows, slices.Equal) {
t.Errorf("rows = %q, want %q", table.Rows, tt.wantRows)
}
})
}

t.Run("unknown column", func(t *testing.T) {
table := newTable()

if err := table.DropColumn("missing"); err == nil {
t.Fatal("expected an error, got nil")
}
if len(table.Columns) != 3 || len(table.Rows[0]) != 3 {
t.Errorf("table changed: columns = %q, rows = %q", table.Columns, table.Rows)
}
})
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module github.com/angelabad/fromscope

go 1.27.0

toolchain go1.27.4

tool golang.org/x/vuln/cmd/govulncheck

require (
Expand Down
65 changes: 65 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"bytes"
"embed"
"html/template"
"log"
"net/http"
)

// assets holds the HTML templates and the static files (CSS, JS),
// embedded in the binary so it runs without any files next to it.
//
//go:embed templates static
var assets embed.FS

// tmpl holds every page template, parsed once at startup.
// Templates are named after their file, e.g. "index.html".
var tmpl = template.Must(template.ParseFS(assets, "templates/*.html"))

// server holds the state shared by the HTTP handlers.
type server struct {
table *Table // read-only after startup, safe for concurrent requests
}

// newServer returns a server that serves the given table.
func newServer(t *Table) *server {
return &server{t}
}

// routes returns the HTTP handler with every route registered.
func (s *server) routes() http.Handler {
router := http.NewServeMux()

router.HandleFunc("GET /{$}", s.handleIndex) // {$} matches "/" only, anything else is a 404
router.Handle("GET /static/", http.FileServer(http.FS(assets)))

return router
}

// handleIndex renders the full page with every row of the table.
func (s *server) handleIndex(w http.ResponseWriter, _ *http.Request) {
render(w, "index.html", s.table)
}

// render executes the named template with data and writes it to w.
// The output is buffered first, so a template error replies with a
// clean 500 instead of a half-written page with a 200 status.
func render(w http.ResponseWriter, name string, data any) {
var buf bytes.Buffer

if err := tmpl.ExecuteTemplate(&buf, name, data); err != nil {
log.Printf("rendering %s: %v", name, err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "text/html; charset=utf-8")
_, err := w.Write(buf.Bytes())
if err != nil {
// Headers are already sent, so all we can do is log it
// (usually the client closed the connection).
log.Printf("writing response: %v", err)
}
}
Loading