-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.go
83 lines (69 loc) · 1.63 KB
/
init.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
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
)
var (
dataDir = "_data"
postsDir = "_posts"
pagesDir = "_pages"
siteDir = "_site"
themesDir = "_themes"
layoutsDir = "_layouts"
assetsDir = filepath.Join(siteDir, "assets")
dirs = []string{
dataDir,
postsDir,
pagesDir,
siteDir,
themesDir,
layoutsDir,
assetsDir,
}
ErrInitialized = errors.New("journal not initialized")
ErrBadInit = errors.New("journal not properly initialized")
InitCmd = &Command{
Usage: "init [directory]",
Short: "initializes a new journal",
Long: `init will initialize a new journal. If directory is given to the command then a
new journal will be initialized in that directory, otherwise the current
directory is used.`,
Run: initCmd,
}
)
func initialized(root string) error {
for _, dir := range dirs {
info, err := os.Stat(filepath.Join(root, dir))
if err != nil {
return ErrInitialized
}
if !info.IsDir() {
return ErrBadInit
}
}
return nil
}
func initCmd(cmd *Command, args []string) {
target := "."
if len(args) >= 2 {
target = args[1]
}
if err := initialized(target); err == nil {
fmt.Fprintf(os.Stderr, "%s %s: journal already initialized\n", cmd.Argv0, args[0])
os.Exit(1)
}
for _, dir := range dirs {
if err := os.MkdirAll(filepath.Join(target, dir), os.FileMode(0755)); err != nil {
fmt.Fprintf(os.Stderr, "%s %s: failed to initialize journal: %s\n", cmd.Argv0, args[0], err)
os.Exit(1)
}
}
cfg, err := CreateConfig(target)
if err != nil {
fmt.Fprintf(os.Stderr, "%s %s: failed to initialize journal: %s\n", cmd.Argv0, args[0], err)
os.Exit(1)
}
cfg.Close()
}