-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
90 lines (70 loc) · 1.72 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"time"
"github.com/renatoathaydes/magnanimous/mg"
)
const (
SourceDir = "source/"
TargetDir = "target/"
)
func main() {
start := time.Now()
rootDir, globalCtx, ok := parseOptions()
if !ok {
return
}
mag := mg.Magnanimous{SourcesDir: filepath.Join(*rootDir, SourceDir), GlobalContex: *globalCtx}
webFiles, err := mag.ReadAll()
if err != nil {
log.Printf("ERROR: %s", err)
panic(err)
}
if len(webFiles.WebFiles) == 0 {
fmt.Printf("No files found in the %s directory, nothing to do.\n", mag.SourcesDir)
return
}
err = mag.WriteTo(filepath.Join(*rootDir, TargetDir), webFiles)
if err != nil {
log.Printf("ERROR: %s", err)
panic(err)
}
log.Printf("Magnanimous generated website in %s\n", time.Since(start))
}
func parseOptions() (rootDir, globalContext *string, ok bool) {
globalContext = flag.String("globalctx", "",
"Path to the global context file relative to the 'processed' directory.")
style := flag.String("style", "lovelace",
"Style name for code highlighting. See https://xyproto.github.io/splash/docs/all.html.")
help := flag.Bool("help", false, "Print usage help.")
flag.Usage = func() {
_, _ = fmt.Fprintf(os.Stderr, "Usage of %s:\n\n %s [options...] [root-directory]\n\nOptions:\n",
os.Args[0], os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
mg.SetCodeStyle(*style)
otherArgs := flag.Args()
if *help {
flag.Usage()
return nil, nil, false
}
var rootDirValue string
switch len(otherArgs) {
case 0:
rootDirValue = ""
case 1:
rootDirValue = otherArgs[0]
default:
log.Printf("ERROR: too many arguments provided")
flag.Usage()
return nil, nil, false
}
rootDir = &rootDirValue
ok = true
return
}