-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpost.go
73 lines (66 loc) · 1.53 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
)
var postCmd = &Command{
UsageLine: "post [filename] [title]",
Short: "create a new post",
Long: `
create a markdown format post.
after then, edit the markdown file and compile it.
`,
}
type Mapper map[string]interface{}
func init() {
postCmd.Run = postApp
AddCommand(postCmd)
}
func create_post(name, title string) {
filename := filepath.Join(".pd", "posts", name+".md")
_, err := os.Stat(filename)
if err == nil || !os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "post file %s already exist? \n", filename)
return
}
err = os.MkdirAll(filepath.Dir(filename), os.ModePerm)
if err != nil {
fmt.Fprintf(os.Stderr, "mkdir %s error:%s\n", filename, err.Error())
return
}
t := time.Now()
y, m, _ := t.Date()
mdata := Mapper{}
mdata["title"] = title
mdata["date"] = t.Format("2006-01-02 15:04:05")
mdata["permalink"] = fmt.Sprintf("/%d/%d/%s.html", y, m, title)
mdata["description"] = ""
mdata["category"] = "默认"
b, err := json.MarshalIndent(mdata, "", " ")
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
return
}
err = ioutil.WriteFile(filename, b, os.ModePerm)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
return
}
fmt.Printf("create file %s.\n", filename)
}
func postApp(cmd *Command, args []string) {
if len(args) == 0 {
fmt.Fprintln(os.Stderr, "[ERRO] Argument [filename] is missing")
os.Exit(2)
}
name := args[0]
title := name
if len(args) > 1 {
title = args[1]
}
create_post(name, title)
}