All checks were successful
check / check (pull_request) Successful in 29s
A build never emptied the output folder, so the pages of a list that got shorter stayed behind. The build now returns the files it did not write and the command prints a warning that names them. -clean empties the folder first, only when it is new, empty or holds the .motorino-out file that an earlier -clean left, and never the folder of the site or one that holds it. The default is unchanged: no extra file in a folder built without it, so the deploy (a new folder published with rsync --delete) is not affected. Refs #9
125 lines
3.8 KiB
Go
125 lines
3.8 KiB
Go
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
// Command motorino builds the kmalloc.org site from Markdown, templates and
|
|
// language files.
|
|
//
|
|
// Usage:
|
|
//
|
|
// motorino build [-site DIR] [-out DIR] [-drafts] [-clean]
|
|
// motorino new [-site DIR] "title"
|
|
// motorino version
|
|
package main
|
|
|
|
import (
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.kmalloc.org/squid/motorino/internal/build"
|
|
"git.kmalloc.org/squid/motorino/internal/content"
|
|
"git.kmalloc.org/squid/motorino/internal/site"
|
|
"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] [-drafts] [-clean] | motorino new [-site DIR] \"title\" | motorino version"
|
|
|
|
// main runs the command line and exits with its status code.
|
|
func main() {
|
|
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
|
|
}
|
|
switch args[0] {
|
|
case "version":
|
|
fmt.Fprintln(stdout, "motorino", version.String())
|
|
return 0
|
|
case "build":
|
|
return runBuild(args[1:], stdout, stderr)
|
|
case "new":
|
|
return runNew(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")
|
|
drafts := flags.Bool("drafts", false, "publish the drafts and the posts dated in the future, for a preview")
|
|
clean := flags.Bool("clean", false, "empty the output folder first, if it is new, empty or was emptied this way before")
|
|
if err := flags.Parse(args); err != nil {
|
|
if errors.Is(err, flag.ErrHelp) {
|
|
return 0
|
|
}
|
|
return 2
|
|
}
|
|
if flags.NArg() > 0 {
|
|
fmt.Fprintf(stderr, "motorino: unexpected arguments: %s\n%s\n", strings.Join(flags.Args(), " "), usage)
|
|
return 2
|
|
}
|
|
result, err := build.Build(build.Options{SiteDir: *site, OutDir: *out, Version: version.String(), Drafts: *drafts, Clean: *clean})
|
|
if err != nil {
|
|
fmt.Fprintf(stderr, "motorino: %v\n", err)
|
|
return 1
|
|
}
|
|
fmt.Fprintf(stdout, "built %d pages into %s\n", result.Pages, *out)
|
|
warnStale(stderr, *out, result.Stale)
|
|
return 0
|
|
}
|
|
|
|
// warnStale tells the files of the output folder that the build did not write,
|
|
// which stay there unless -clean is used.
|
|
func warnStale(stderr io.Writer, out string, stale []string) {
|
|
if len(stale) == 0 {
|
|
return
|
|
}
|
|
noun := "files"
|
|
if len(stale) == 1 {
|
|
noun = "file"
|
|
}
|
|
fmt.Fprintf(stderr, "motorino: warning: %d %s in %s were not written by this build, such as %s; build into a new folder or use -clean\n", len(stale), noun, out, stale[0])
|
|
}
|
|
|
|
// runNew parses the flags of the new command and writes the file of a new post.
|
|
// The author is the author key of site.json, when it has one.
|
|
func runNew(args []string, stdout, stderr io.Writer) int {
|
|
flags := flag.NewFlagSet("new", flag.ContinueOnError)
|
|
flags.SetOutput(stderr)
|
|
dir := flags.String("site", ".", "the folder of the site")
|
|
if err := flags.Parse(args); err != nil {
|
|
if errors.Is(err, flag.ErrHelp) {
|
|
return 0
|
|
}
|
|
return 2
|
|
}
|
|
if flags.NArg() != 1 {
|
|
fmt.Fprintf(stderr, "motorino: new takes one title\n%s\n", usage)
|
|
return 2
|
|
}
|
|
author := ""
|
|
if cfg, _, err := site.Load(*dir); err == nil {
|
|
author = cfg.Strings["author"]
|
|
}
|
|
rel, err := content.NewPost(*dir, flags.Arg(0), author, time.Now())
|
|
if err != nil {
|
|
fmt.Fprintf(stderr, "motorino: %v\n", err)
|
|
return 1
|
|
}
|
|
fmt.Fprintln(stdout, rel)
|
|
return 0
|
|
}
|