forked from aybabtme/brog
/
assets.go
120 lines (101 loc) · 3.58 KB
/
assets.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
package brogger
import (
"fmt"
"io/ioutil"
"os"
"path"
)
const (
// ConfigFilename where to find the Brog config file.
ConfigFilename = "brog_config.json"
jsPath = "js/"
cssPath = "css/"
)
// Base templates
var allAssets = map[string]packed{
// Base CSS
"brog.css": {"brog.css", path.Join(DefaultAssetPath, cssPath), baseAssetsCssBrogCss},
"github.css": {"github.css", path.Join(DefaultAssetPath, cssPath), baseAssetsCssGithubCss},
// Base JS,
"brog.js": {"brog.js", path.Join(DefaultAssetPath, jsPath), baseAssetsJsBrogJs},
"highlight.min.js": {"highlight.min.js", path.Join(DefaultAssetPath, jsPath), baseAssetsJsHighlightMinJs},
// Base posts
"sample.md": {"sample.md", DefaultPostPath, basePostsSampleMd},
"blank.md": {"blank.md", DefaultPostPath, basePostsBlankMd},
// Base files
".gitignore": {".gitignore", "./", baseGitignore},
"README.md": {"README.md", "./", baseREADMEMd},
}
var allTemplates = map[string]packed{
// HTML templates
appTmplName: {appTmplName, DefaultTemplatePath, baseTemplatesApplicationGohtml},
indexTmplName: {indexTmplName, DefaultTemplatePath, baseTemplatesIndexGohtml},
postTmplName: {postTmplName, DefaultTemplatePath, baseTemplatesPostGohtml},
langSelectTmplName: {langSelectTmplName, DefaultTemplatePath, baseTemplatesLangselectGohtml},
styleTmplName: {styleTmplName, DefaultTemplatePath, baseTemplatesStyleGohtml},
jsTmplName: {jsTmplName, DefaultTemplatePath, baseTemplatesJavascriptGohtml},
headerTmplName: {headerTmplName, DefaultTemplatePath, baseTemplatesHeaderGohtml},
footerTmplName: {footerTmplName, DefaultTemplatePath, baseTemplatesFooterGohtml},
}
type packed struct {
filename string
destination string
data []byte
}
func (p *packed) replicate() error {
fullpath := path.Join(p.destination, p.filename)
if fileExists(fullpath) {
return fmt.Errorf("file '%s' already exists, will not overwrite", fullpath)
}
return p.rewriteFile(fullpath)
}
func (p *packed) replicateInDir(dirname string) error {
fullpath := path.Join(dirname, p.filename)
if fileExists(fullpath) {
return fmt.Errorf("file '%s' already exists, will not overwrite", fullpath)
}
return p.rewriteFile(fullpath)
}
func (p *packed) rewriteInDir(dirname string) error {
fullpath := path.Join(dirname, p.filename)
return p.rewriteFile(fullpath)
}
func (p *packed) rewriteFile(fullpath string) error {
if err := os.MkdirAll(p.destination, 0740); err != nil {
return fmt.Errorf("creating directory at '%s' failed, %v", p.destination, err)
}
return ioutil.WriteFile(fullpath, p.data, 0640)
}
// CopyBrogBinaries writes the in-memory brog assets to the current working
// directory, effectively creating a brog structure that `brog server` can use
// to serve content.
func CopyBrogBinaries() []error {
config := newDefaultConfig()
errs := []error{}
err := config.persistToFile(ConfigFilename)
if err != nil {
fmt.Errorf("persisting config file, %v", err)
}
for _, asset := range allAssets {
err := asset.replicate()
if err != nil {
errs = append(errs, err)
}
}
for _, asset := range allTemplates {
err := asset.replicate()
if err != nil {
errs = append(errs, err)
}
}
return errs
}
// CopyBlankToFilename creates a blank post at the given filename, under the asset
// path specified by conf
func CopyBlankToFilename(conf *Config, filename string) error {
if filename == "" {
return fmt.Errorf("no filename specified")
}
fullpath := path.Clean(conf.PostPath) + string(os.PathSeparator) + filename + conf.PostFileExt
return ioutil.WriteFile(fullpath, basePostsBlankMd, 0640)
}