-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpost.go
103 lines (77 loc) · 1.92 KB
/
post.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
package main
import (
"flag"
"os"
"path/filepath"
"time"
)
var PostCmd = &Command{
Usage: "post <title>",
Short: "create a new jrnl post",
Long: `post will open up the editor specified via the EDITOR environment variable for
writing a post.
The -l flag can be given to specify a layout to use for the new post.
The -p flag can be given to specify the parent for the new post.`,
Run: postCmd,
}
type Post struct {
*Page
}
func NewPost(title string) Post {
return Post{
Page: NewPage(title),
}
}
func (p *Post) Slug() string { return p.MetaData.CreatedAt.Format("2006-01-02") + "-" + p.Page.Slug() }
func (p *Post) CreatedAt() time.Time { return p.MetaData.CreatedAt.Time }
func (p *Post) UpdatedAt() time.Time { return p.MetaData.UpdatedAt.Time }
func (p *Post) URL() string {
url := "/" + p.Slug()
if p.MetaData.Parent != "" {
return p.MetaData.Parent + url
}
return url
}
func (p *Post) Path() string {
return filepath.Join(postDir, p.Slug()) + ".md"
}
func (p *Post) File() (*os.File, error) {
return os.OpenFile(p.Path(), pageMask, pagePerm)
}
func (p *Post) Touch() error {
f, err := p.File()
if err != nil {
return err
}
defer f.Close()
p.MetaData.UpdatedAt.Time = time.Now()
if err := p.Encode(f); err != nil {
return err
}
return nil
}
func postCmd(cmd *Command, args []string) error {
if err := Initialized("."); err != nil {
return err
}
var layout, parent string
fs := flag.NewFlagSet(cmd.Argv0, flag.ExitOnError)
fs.StringVar(&layout, "l", "post", "the layout of the new post")
fs.StringVar(&parent, "p", "", "the parent of the new post")
fs.Parse(args)
args = fs.Args()
if len(args) == 0 {
return ErrUsage
}
p := NewPost(args[0])
p.MetaData.Layouts = append(p.MetaData.Layouts, layout)
p.MetaData.Parent = parent
p.MetaData.CreatedAt.Time = time.Now()
if err := p.Touch(); err != nil {
return err
}
if err := OpenInEditor(p.Path()); err != nil {
return err
}
return nil
}