-
Notifications
You must be signed in to change notification settings - Fork 0
/
genmd.go
47 lines (42 loc) · 925 Bytes
/
genmd.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
package genmd
import (
"fmt"
"io/ioutil"
"log"
"path/filepath"
"github.com/jpillora/md-tmpl/mdtmpl"
"github.com/wxio/wx/internal/types"
)
type genmd struct {
rt *types.Root
//
Filename string `opts:"mode=arg"`
WorkingDir string
Preview bool
}
// New constructor for subcommand
func New(rt *types.Root) interface{} {
return &genmd{
WorkingDir: ".",
rt: rt,
}
}
func (gen *genmd) Run() error {
fp := filepath.Join(gen.rt.CWD, gen.WorkingDir, gen.Filename)
if b, err := ioutil.ReadFile(fp); err != nil {
return err
} else {
if gen.Preview {
for i, c := range mdtmpl.Commands(b) {
fmt.Printf("%18s#%d %s\n", gen.Filename, i+1, c)
}
return nil
}
b = mdtmpl.ExecuteIn(b, filepath.Join(gen.rt.CWD, gen.WorkingDir))
if err := ioutil.WriteFile(fp, b, 0655); err != nil {
return err
}
log.Printf("executed templates and rewrote '%s'", gen.Filename)
return nil
}
}