feat: add the build and version commands
This commit is contained in:
parent
6bdf2ca05f
commit
2c3975430b
4 changed files with 179 additions and 13 deletions
|
|
@ -2,21 +2,68 @@
|
|||
|
||||
// Command motorino builds the kmalloc.org site from Markdown, templates and
|
||||
// language files.
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
// motorino build [-site DIR] [-out DIR]
|
||||
// motorino version
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"git.kmalloc.org/squid/motorino/internal/build"
|
||||
"git.kmalloc.org/squid/motorino/internal/version"
|
||||
)
|
||||
|
||||
// usage is the text printed for a missing or unknown command.
|
||||
const usage = "usage: motorino build [-site DIR] [-out DIR] | motorino version"
|
||||
|
||||
// main runs the command line and exits with its status code.
|
||||
func main() {
|
||||
if len(os.Args) > 1 && os.Args[1] == "version" {
|
||||
fmt.Println("motorino", version.String())
|
||||
return
|
||||
os.Exit(run(os.Args[1:], os.Stdout, os.Stderr))
|
||||
}
|
||||
|
||||
// run executes one command line and returns the exit code: 0 for success, 1
|
||||
// for a failed command and 2 for a mistake in the command line.
|
||||
func run(args []string, stdout, stderr io.Writer) int {
|
||||
if len(args) == 0 {
|
||||
fmt.Fprintln(stderr, usage)
|
||||
return 2
|
||||
}
|
||||
fmt.Fprintln(os.Stderr, "usage: motorino version")
|
||||
os.Exit(2)
|
||||
switch args[0] {
|
||||
case "version":
|
||||
fmt.Fprintln(stdout, "motorino", version.String())
|
||||
return 0
|
||||
case "build":
|
||||
return runBuild(args[1:], stdout, stderr)
|
||||
default:
|
||||
fmt.Fprintf(stderr, "motorino: unknown command %q\n%s\n", args[0], usage)
|
||||
return 2
|
||||
}
|
||||
}
|
||||
|
||||
// runBuild parses the flags of the build command and builds the site.
|
||||
func runBuild(args []string, stdout, stderr io.Writer) int {
|
||||
flags := flag.NewFlagSet("build", flag.ContinueOnError)
|
||||
flags.SetOutput(stderr)
|
||||
site := flags.String("site", ".", "the folder of the site")
|
||||
out := flags.String("out", "public", "the folder the HTML is written to")
|
||||
if err := flags.Parse(args); err != nil {
|
||||
if errors.Is(err, flag.ErrHelp) {
|
||||
return 0
|
||||
}
|
||||
return 2
|
||||
}
|
||||
result, err := build.Build(build.Options{SiteDir: *site, OutDir: *out, Version: version.String()})
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "motorino: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
fmt.Fprintf(stdout, "built %d pages into %s\n", result.Pages, *out)
|
||||
return 0
|
||||
}
|
||||
|
|
|
|||
110
cmd/motorino/main_test.go
Normal file
110
cmd/motorino/main_test.go
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// miniSite writes the smallest site that builds: every required page in one
|
||||
// language and the templates the frame needs. It returns the folder.
|
||||
func miniSite(t *testing.T) string {
|
||||
t.Helper()
|
||||
files := map[string]string{
|
||||
"site.json": `{"base_url": "https://x.org", "languages": ["en"], "mastodon_url": "m", "motorino_url": "u"}`,
|
||||
"lang/en.json": `{"lang_name": "English", "home_link": "/", "back": "b", "news_all": "a", "news_p": "p",
|
||||
"news_empty": "e", "nojs": "n", "history_a": "h", "version": "v", "powered_by": "P", "category_h": "c",
|
||||
"nav": {"news": "News", "manifesto": "M"}}`,
|
||||
"static/style.css": "body{}\n",
|
||||
"templates/base.html": "{{header}}{{content}}{{footer}}\n",
|
||||
"templates/header.html": "<header>{{nav_news}}</header>\n",
|
||||
"templates/footer.html": "<footer>{{lead}}{{badges}}</footer>\n",
|
||||
"templates/lead-home.html": "h\n", "templates/lead-back.html": "b\n", "templates/lead-news.html": "n\n",
|
||||
"templates/badges.html": "{{feed}}\n", "templates/feed.html": "f\n",
|
||||
"templates/pages/page.html": "<h1>{{page_title}}</h1>{{body}}\n",
|
||||
"templates/post.html": "{{post_body}}\n", "templates/category.html": "{{category_posts}}\n",
|
||||
}
|
||||
for _, id := range []string{"home", "manifesto", "people", "services", "news", "history"} {
|
||||
name := id
|
||||
if id == "home" {
|
||||
name = "index"
|
||||
}
|
||||
files["content/en/"+name+".md"] = "Title: " + id + "\nID: " + id + "\nDescription: d\n-\nBody of " + id + ".\n"
|
||||
}
|
||||
dir := t.TempDir()
|
||||
for name, content := range files {
|
||||
path := filepath.Join(dir, filepath.FromSlash(name))
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
return dir
|
||||
}
|
||||
|
||||
// TestRunVersion checks that the version command prints the name and the version.
|
||||
func TestRunVersion(t *testing.T) {
|
||||
var out, errOut bytes.Buffer
|
||||
if code := run([]string{"version"}, &out, &errOut); code != 0 {
|
||||
t.Fatalf("run(version) = %d, stderr %q", code, errOut.String())
|
||||
}
|
||||
if got := strings.TrimSpace(out.String()); got != "motorino dev" {
|
||||
t.Errorf("output = %q, want %q", got, "motorino dev")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunBuildSuccess checks a build: exit 0, a summary line and the pages.
|
||||
func TestRunBuildSuccess(t *testing.T) {
|
||||
site := miniSite(t)
|
||||
outDir := filepath.Join(t.TempDir(), "public")
|
||||
var out, errOut bytes.Buffer
|
||||
code := run([]string{"build", "-site", site, "-out", outDir}, &out, &errOut)
|
||||
if code != 0 {
|
||||
t.Fatalf("run(build) = %d, stderr %q", code, errOut.String())
|
||||
}
|
||||
if want := "built 6 pages into " + outDir; !strings.Contains(out.String(), want) {
|
||||
t.Errorf("output %q does not contain %q", out.String(), want)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(outDir, "index.html")); err != nil {
|
||||
t.Errorf("no index.html in the output: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunBuildErrorExit1 checks that a failed build exits 1 and says why.
|
||||
func TestRunBuildErrorExit1(t *testing.T) {
|
||||
var out, errOut bytes.Buffer
|
||||
code := run([]string{"build", "-site", t.TempDir(), "-out", filepath.Join(t.TempDir(), "public")}, &out, &errOut)
|
||||
if code != 1 {
|
||||
t.Fatalf("run(build) = %d, want 1", code)
|
||||
}
|
||||
if !strings.HasPrefix(errOut.String(), "motorino: ") || !strings.Contains(errOut.String(), "site.json") {
|
||||
t.Errorf("stderr = %q, want a line that starts with %q and names site.json", errOut.String(), "motorino: ")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunUnknownCommand checks the usage message and exit 2.
|
||||
func TestRunUnknownCommand(t *testing.T) {
|
||||
for _, args := range [][]string{{}, {"frobnicate"}} {
|
||||
var out, errOut bytes.Buffer
|
||||
if code := run(args, &out, &errOut); code != 2 {
|
||||
t.Errorf("run(%v) = %d, want 2", args, code)
|
||||
}
|
||||
if !strings.Contains(errOut.String(), "usage:") {
|
||||
t.Errorf("run(%v) stderr = %q, want a usage line", args, errOut.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunBuildBadFlag checks that an unknown flag is a usage error.
|
||||
func TestRunBuildBadFlag(t *testing.T) {
|
||||
var out, errOut bytes.Buffer
|
||||
if code := run([]string{"build", "-nope"}, &out, &errOut); code != 2 {
|
||||
t.Errorf("run(build -nope) = %d, want 2", code)
|
||||
}
|
||||
}
|
||||
|
|
@ -65,29 +65,38 @@ type categoryEntry struct {
|
|||
posts []content.Post
|
||||
}
|
||||
|
||||
// Result summarises a build.
|
||||
type Result struct {
|
||||
// Pages is the number of HTML pages written: pages, news and categories.
|
||||
Pages int
|
||||
}
|
||||
|
||||
// Build reads the site in opts.SiteDir and writes it to opts.OutDir. It renders
|
||||
// everything in memory first, so a failure leaves the output folder untouched.
|
||||
func Build(opts Options) error {
|
||||
func Build(opts Options) (Result, error) {
|
||||
b, err := load(opts)
|
||||
if err != nil {
|
||||
return err
|
||||
return Result{}, err
|
||||
}
|
||||
if err := b.renderPages(); err != nil {
|
||||
return err
|
||||
return Result{}, err
|
||||
}
|
||||
if err := b.renderPosts(); err != nil {
|
||||
return err
|
||||
return Result{}, err
|
||||
}
|
||||
if err := b.renderCategories(); err != nil {
|
||||
return err
|
||||
return Result{}, err
|
||||
}
|
||||
b.renderFeed()
|
||||
b.files["robots.txt"] = robots(b.indexable)
|
||||
assets, err := b.readAssets()
|
||||
if err != nil {
|
||||
return err
|
||||
return Result{}, err
|
||||
}
|
||||
return b.write(assets)
|
||||
if err := b.write(assets); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
return Result{Pages: len(b.indexable)}, nil
|
||||
}
|
||||
|
||||
// load reads the configuration, the content, the templates and the style.
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ func writeFiles(t *testing.T, files map[string]string) string {
|
|||
func runBuild(t *testing.T, files map[string]string) (string, error) {
|
||||
t.Helper()
|
||||
out := filepath.Join(t.TempDir(), "public")
|
||||
err := Build(Options{SiteDir: writeFiles(t, files), OutDir: out, Version: "1.2.3"})
|
||||
_, err := Build(Options{SiteDir: writeFiles(t, files), OutDir: out, Version: "1.2.3"})
|
||||
return out, err
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue