/
statika.go
84 lines (72 loc) · 2.26 KB
/
statika.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
/*statika
statika is a simple SSG (Static Site Generator) written in go.
Examples:
$ statika -d <project-dir>
$ statika -d simple.com -c # Clean the output
$ statika -d simple.com -b # Manually build
$ statika -d simple.com -w # Watch for changes then build
$ statika -d simple.com -s # Run a local development server
*/
package main
import (
"fmt"
_ "github.com/flosch/pongo2-addons"
"github.com/jessevdk/go-flags"
"github.com/joho/godotenv"
"log"
"path/filepath"
"statika/models"
"statika/tasks"
"statika/util"
)
const version = "0.0.1"
var (
cfg *models.Config
opts = struct {
ProjectDir string `short:"d" long:"project_dir" description:"The project directory (if not using PROJECT_DIR in .env)." required:"true"`
Server []bool `short:"s" long:"server" description:"Run development server"`
Build []bool `short:"b" long:"build" description:"Build the site"`
Watch []bool `short:"w" long:"watch" description:"Watch for file changes and build site automatically"`
Clean []bool `short:"c" long:"clean" description:"Clean build directory"`
Verbose []bool `short:"v" long:"Verbose" description:"Enable Verbose logging"`
}{}
)
func init() {
_, err := flags.Parse(&opts)
util.Check(err)
cfg = &models.Config{}
cfg.Verbose = len(opts.Verbose) > 0
if cfg.Verbose {
fmt.Println("Version: ", version)
}
if opts.ProjectDir != "" {
cfg.ProjectDir = opts.ProjectDir
} else {
cfg.ProjectDir = ""
}
err = godotenv.Load(filepath.Join(cfg.ProjectDir, ".env"))
util.Check(err)
cfg.SrcDir = filepath.Join(cfg.ProjectDir, "src/")
cfg.OutputDir = filepath.Join(cfg.ProjectDir, "output/")
cfg.StaticDir = filepath.Join(cfg.SrcDir, "static/")
cfg.TemplatesDir = filepath.Join(cfg.SrcDir, "templates/html")
cfg.ContentDir = filepath.Join(cfg.SrcDir, "content/")
cfg.StaticDir = filepath.Join(cfg.SrcDir, "static/")
}
func main() {
if len(opts.Server) > 0 {
fmt.Println("Serving...")
tasks.Server(cfg)
} else if len(opts.Watch) > 0 {
fmt.Println("Watching...")
tasks.Watch(cfg)
} else if len(opts.Build) > 0 {
fmt.Println("Building...")
tasks.Build(cfg)
} else if len(opts.Clean) > 0 {
fmt.Println("Cleaning...")
tasks.Clean(cfg.OutputDir)
} else {
log.Fatal("Please specify a flag to run task: server, watch, build, clean")
}
}