-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm2cw.go
76 lines (64 loc) · 1.3 KB
/
m2cw.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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/codegangsta/cli"
"github.com/howeyc/fsnotify"
)
func md2conf(c *cli.Context) {
arg := c.Args().First()
cmd := exec.Command("markdown2confluence", arg)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
ioutil.WriteFile(
strings.TrimSuffix(arg, filepath.Ext(arg))+".wiki",
out.Bytes(),
0644,
)
}
func main() {
app := cli.NewApp()
app.Name = "m2cw"
app.Usage = "Convert markdown file to confluence style when save md file."
app.Author = "Layzie <HIRAKI Satoru>"
app.Email = "[email protected]"
app.Version = "0.0.1"
app.Action = func(c *cli.Context) {
arg := c.Args().First()
fmt.Println("Start watching " + arg + ". <C-c> makes stop the command.")
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
err = watcher.Watch("./")
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
for {
select {
case ev := <-watcher.Event:
if ev.Name == arg {
md2conf(c)
log.Println(
"convert md to wiki ",
ev.Name+" -> "+strings.TrimSuffix(arg, filepath.Ext(arg))+".wiki",
)
}
case err := <-watcher.Error:
log.Println("error:", err)
}
}
}
app.Run(os.Args)
}