-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
158 lines (139 loc) · 3.35 KB
/
main.go
File metadata and controls
158 lines (139 loc) · 3.35 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"path/filepath"
cp "github.com/otiai10/copy"
_ "flag"
"log"
"os"
"github.com/tidwall/gjson"
_ "github.com/tidwall/gjson"
"github.com/yuin/goldmark"
_ "github.com/yuin/goldmark"
"github.com/yuin/goldmark/parser"
_ "github.com/yuin/goldmark/parser"
"go.abhg.dev/goldmark/frontmatter"
_ "go.abhg.dev/goldmark/frontmatter"
"golang.org/x/exp/slices"
_ "golang.org/x/exp/slices"
)
/* `tag: publish` or `draft: false` */
func isPublishCheck(frontMatter string) bool {
// empty front matter
if frontMatter == "" {
return false
} else {
draftGj := gjson.Parse(frontMatter).Get("draft")
var isDraft bool
err := json.Unmarshal([]byte(draftGj.String()), &isDraft)
if err != nil {
isDraft = false
// log.Panic(err)
} else {
isDraft = draftGj.Bool()
}
tagsGj := gjson.Parse(frontMatter).Get("tags")
var tags []string
var isPublishTagContain bool
err = json.Unmarshal([]byte(tagsGj.String()), &tags)
if err != nil {
isPublishTagContain = false
log.Panic(err)
} else {
isPublishTagContain = slices.Contains(tags, "publish")
}
isPublish := !(isDraft || isPublishTagContain)
if isPublish {
return false
} else {
return true
}
}
}
func fileFilter(filePath string) (bool, error) {
bytes, err := os.ReadFile(filePath)
if err != nil {
return false, fmt.Errorf("err open file: %w", err)
}
var formats []frontmatter.Format
md := goldmark.New(
goldmark.WithExtensions(
&frontmatter.Extender{
Formats: formats,
},
),
)
ctx := parser.NewContext()
if err := md.Convert([]byte(bytes), io.Discard, parser.WithContext(ctx)); err != nil {
return false, fmt.Errorf("convert error: %s\n%w", filePath, err)
}
var fm string
if data := frontmatter.Get(ctx); data != nil {
var meta map[string]any
if err := data.Decode(&meta); err != nil {
return false, fmt.Errorf("decode error: %s\n%w", filePath, err)
}
formatted, err := json.MarshalIndent(meta, "", " ")
if err != nil {
return false, fmt.Errorf("format error: %s\n%w", filePath, err)
}
fm = string(formatted)
}
return !isPublishCheck(fm), nil
}
func main() {
src, dest := argCheck()
//println(src, "\n", dest)
err := cp.Copy(
src, dest,
cp.Options{
Skip: func(info os.FileInfo, src, dest string) (bool, error) {
if info.IsDir() || filepath.Ext(src) != ".md" {
fmt.Println(src)
return false, nil
} else {
isSkip, err := fileFilter(filepath.Join(src))
if !isSkip {
fmt.Println(src)
}
return isSkip, err
}
},
},
)
if err != nil {
log.Panic(err)
}
}
func argCheck() (string, string) {
var helpFlag bool
var cpSrc string
var cpDest string
flag.BoolVar(&helpFlag, "h", false, "show help message")
flag.BoolVar(&helpFlag, "help", false, "show help message")
flag.StringVar(&cpSrc, "src", "", "cp source")
flag.StringVar(&cpDest, "dest", "", "cp destination")
flag.Parse()
if helpFlag {
printHelpAndExit()
}
args := flag.Args()
if len(args) != 2 {
printHelpAndExit()
}
cpSrc, cpDest = args[0], args[1]
return cpSrc, cpDest
}
func printHelpAndExit() {
helpStr := `fmfcp is Front Matter Filter CoPy tool.
check ".md" file & frontmatter contains "tag: publish" or "draft: true" are copy.
not ".md" file are just copy.
usage: fmfcp $src $dest`
fmt.Println(helpStr)
// fmt.Println("---")
// flag.PrintDefaults()
os.Exit(0)
}