a73x

internal/site/cli.go

Ref:   Size: 2.3 KiB   History

// cli.go is the eitri-site command line: `eitri-site` (no subcommand) renders
// the static site, `eitri-site manifest` writes the release manifest. It lives
// here rather than in cmd/eitri-site so it is testable and coverage-gated
// (arch R14: main packages are wiring only).

package site

import (
	"encoding/json"
	"flag"
	"fmt"
	"os"
)

// RunCLI dispatches the eitri-site command line (everything after the binary
// name, --version excluded — that stays in cmd/eitri-site). The default path
// renders the site; the "manifest" subcommand writes the release manifest.
func RunCLI(args []string) error {
	if len(args) > 0 && args[0] == "manifest" {
		if err := runManifest(args[1:]); err != nil {
			return fmt.Errorf("manifest: %w", err)
		}
		return nil
	}
	return runBuild(args)
}

func runBuild(args []string) error {
	fs := flag.NewFlagSet("eitri-site", flag.ExitOnError)
	docs := fs.String("docs", "docs", "docs directory (markdown sources)")
	siteDir := fs.String("site", "site", "site directory (index.md, template.html, style.css)")
	dist := fs.String("dist", "", "optional dist/<version> dir with release artifacts")
	out := fs.String("out", "site/dist", "output webroot")
	base := fs.String("base", "https://eitri.sh", "base URL the site is served from (link previews need absolute URLs; empty for a local preview)")
	if err := fs.Parse(args); err != nil {
		return err
	}
	return Build(Config{DocsDir: *docs, SiteDir: *siteDir, DistDir: *dist, OutDir: *out, BaseURL: *base})
}

func runManifest(args []string) error {
	fs := flag.NewFlagSet("eitri-site manifest", flag.ExitOnError)
	ver := fs.String("version", "", "release version (vX.Y.Z)")
	dist := fs.String("dist", "", "dist/<version> dir holding the release artifacts")
	base := fs.String("base", "", "base URL artifacts are served from")
	out := fs.String("out", "", "output path (default <dist>/manifest.json)")
	if err := fs.Parse(args); err != nil {
		return err
	}
	if *ver == "" || *dist == "" || *base == "" {
		return fmt.Errorf("-version, -dist, and -base are required")
	}
	m, err := BuildManifest(*ver, *dist, *base)
	if err != nil {
		return err
	}
	raw, err := json.MarshalIndent(m, "", "  ")
	if err != nil {
		return err
	}
	path := *out
	if path == "" {
		path = *dist + "/manifest.json"
	}
	return os.WriteFile(path, append(raw, '\n'), 0o644)
}