-
Notifications
You must be signed in to change notification settings - Fork 41
/
gostatic.go
158 lines (130 loc) · 4.06 KB
/
gostatic.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// (c) 2012 Alexander Solovyov
// under terms of ISC license
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
flags "github.com/jessevdk/go-flags"
hotreload "github.com/piranha/gostatic/hotreload"
gostatic "github.com/piranha/gostatic/lib"
"github.com/piranha/gostatic/processors"
)
const (
// ExitCodeOk is used when the application exits without error.
ExitCodeOk = 0
// ExitCodeInvalidFlags is used when invalid flags are passed.
ExitCodeInvalidFlags = 1
// ExitCodeInvalidConfig is used when an invalid configuration file is given.
ExitCodeInvalidConfig = 2
// ExitCodeOther is used in all other situations.
ExitCodeOther = 127
)
// Opts contains the flags which have been parsed by go-flags.
type Opts struct {
ShowProcessors bool `long:"processors" description:"show page processors"`
ShowConfig bool `long:"show-config" description:"print config as JSON"`
ShowSummary bool `long:"summary" description:"print all pages on stdout"`
InitExample *string `short:"i" long:"init" description:"create example site"`
DumpPage string `short:"d" long:"dump" description:"print page metadata as JSON (pass path to source or target file)"`
// checked in Page.Changed()
Force bool `short:"f" long:"force" description:"force building all pages"`
Watch bool `short:"w" long:"watch" description:"serve site on HTTP, rebuild on changes and hot reload HTML in browser"`
NoHotreload bool `long:"no-hotreload" description:"disable hot reload during --watch"`
Port string `short:"p" long:"port" default:"8000" description:"port to serve on"`
Verbose bool `short:"v" long:"verbose" description:"enable verbose output"`
Version bool `short:"V" long:"version" description:"show version and exit"`
}
var opts Opts
func main() {
argparser := flags.NewParser(&opts,
flags.PrintErrors|flags.PassDoubleDash|flags.HelpFlag)
argparser.Usage = "[OPTIONS] path/to/config\n\nBuild a site."
args, err := argparser.Parse()
if err != nil {
if _, ok := err.(*flags.Error); ok {
return
}
errhandle(fmt.Errorf("unknown error: %v", err))
os.Exit(ExitCodeOther)
}
if opts.ShowSummary && opts.Watch {
errhandle(fmt.Errorf("--summary and --watch do not mix together well"))
os.Exit(ExitCodeOther)
}
if opts.Verbose {
gostatic.DEBUG = true
}
if opts.Version {
out("gostatic %s\n", gostatic.VERSION)
return
}
if opts.InitExample != nil {
target, _ := os.Getwd()
if len(*opts.InitExample) > 0 {
// If an absolute path was given, use verbatim. Otherwise rebase path
// on top of current working directory.
if strings.HasPrefix(*opts.InitExample, "/") {
target = *opts.InitExample
} else {
target = filepath.Join(target, *opts.InitExample)
}
}
gostatic.WriteExample(target)
return
}
if opts.ShowProcessors {
processors.DefaultProcessors.ProcessorSummary()
return
}
if len(args) == 0 {
argparser.WriteHelp(os.Stderr)
os.Exit(ExitCodeInvalidFlags)
return
}
// config, err := gostatic.NewSiteConfig(args[0])
// if err != nil {
// errhandle(fmt.Errorf("invalid config file '%s': %v", args[0], err))
// os.Exit(ExitCodeInvalidConfig)
// }
site := gostatic.NewSite(args[0], processors.DefaultProcessors)
if opts.Force {
site.ForceRefresh = true
}
if opts.ShowConfig {
x, err := json.MarshalIndent(site.SiteConfig, "", " ")
errhandle(err)
fmt.Fprintln(os.Stderr, string(x))
return
}
if len(opts.DumpPage) > 0 {
page := site.PageBySomePath(opts.DumpPage)
if page == nil {
out("Page '%s' not found (supply source or destination path)\n",
opts.DumpPage)
return
}
dump, err := json.MarshalIndent(page, "", " ")
errhandle(err)
out("%s\n", dump)
return
}
if opts.ShowSummary {
site.Summary()
} else {
site.Render()
}
if opts.Watch {
err := hotreload.Watch([]string{site.SiteConfig.Source}, site.SiteConfig.Templates,
func() {
site.Reconfig()
site.Render()
})
errhandle(err)
out("Starting server at *:%s...\n", opts.Port)
err = hotreload.ServeHTTP(site.SiteConfig.Output, opts.Port, !opts.NoHotreload)
errhandle(err)
}
}