/
init.go
77 lines (63 loc) · 1.72 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
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/pelletier/go-toml"
"github.com/urfave/cli/v2"
)
// InitProject initializes a new zettel site copies a sample config.
func (hub *Hub) InitProject() *cli.Command {
return &cli.Command{
Name: "init",
Aliases: []string{"i"},
Usage: "Initializes a new zettel site with default config.",
Action: hub.init,
ArgsUsage: "[SITENAME]",
Before: func(c *cli.Context) error {
if c.Args().First() == "" {
return errors.New("site name is missing")
}
return nil
},
}
}
func (hub *Hub) init(cliCtx *cli.Context) error {
// get a default config
siteDir := cliCtx.Args().First()
//Create a folder/directory at a full qualified path
err := os.Mkdir(siteDir, 0750)
if err != nil {
return err
}
cfg := gatherDefaultConfig()
configFile, err := toml.Marshal(cfg)
if err != nil {
return fmt.Errorf("error unmarshalling config: %s", err)
}
// persist the default config file.
if err = createFile(configFile, filepath.Join(siteDir, defaultconfigName)); err != nil {
return fmt.Errorf("error creating default config: %v", err)
}
// create content dir if it doesn't exit
contentDir := filepath.Join(siteDir, defaultPostDir)
_, err = os.Stat(contentDir)
if os.IsNotExist(err) {
_ = os.Mkdir(contentDir, 0750)
} else if err != nil {
return err
}
// persist Index file.
path := filepath.Join(siteDir, defaultPostDir, defaultindexFileName)
post, err := os.Create(path)
if err != nil {
return err
}
// render post template
if err = saveResource("index", []string{"templates/index.tmpl"}, post, nil, hub.Fs); err != nil {
return err
}
hub.Logger.Infof("Congrats! Your zettel site is created at %s", siteDir)
return nil
}