-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (48 loc) · 1.35 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"context"
"flag"
"fmt"
"io"
"os"
"os/signal"
"syscall"
"gitlab.com/krichprollsch/mblog/gen"
)
const (
exitOK = 0
exitFail = 1
)
// main starts interruptable context and runs the program.
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
defer cancel()
err := run(ctx, os.Args, os.Stdout, os.Stderr)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(exitFail)
}
os.Exit(exitOK)
}
// run configures the flags and generates static web pages.
func run(ctx context.Context, args []string, stdout, stderr io.Writer) error {
// declare runtime flag parameters.
// TODO use env var by default is set.
flags := flag.NewFlagSet(args[0], flag.ExitOnError)
var (
in = flags.String("in", "in", "input markdown directory")
out = flags.String("out", "out", "output html directory")
tmpl = flags.String("tmpl", "tmpl", "templates directory")
)
// usage func declaration.
flags.Usage = func() {
fmt.Fprintf(stdout, "%s is micro blogging static generator\n", args[0])
fmt.Fprintf(stdout, "usage:\n")
fmt.Fprintf(stdout, "\t%s [-tmpl <templates dir>] [-in <input dir>] [-out <output dir>]\tgenerates the html.\n", args[0])
}
if err := flags.Parse(args[1:]); err != nil {
return err
}
g := gen.New(os.DirFS(*tmpl), os.DirFS(*in), *out)
return g.Run(ctx)
}