forked from cloudquery/cloudquery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
68 lines (60 loc) · 1.52 KB
/
Copy pathmain.go
File metadata and controls
68 lines (60 loc) · 1.52 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
package main
import (
"cloudquery/tablesdiff/changes"
"encoding/json"
"log"
"os"
"regexp"
"strings"
"github.com/bluekeyes/go-gitdiff/gitdiff"
)
var (
sourcePluginDocsRegex = regexp.MustCompile(`^website/tables/.*?/.*\.md$`)
)
func isPluginTableDocFile(file *gitdiff.File) bool {
if file.IsBinary {
return false
}
// Skip the README as we have everything we need from the tables files
if strings.HasSuffix(file.OldName, "README.md") || strings.HasSuffix(file.NewName, "README.md") {
return false
}
return sourcePluginDocsRegex.MatchString(file.OldName) || sourcePluginDocsRegex.MatchString(file.NewName)
}
func filterFiles(files []*gitdiff.File) []*gitdiff.File {
filtered := make([]*gitdiff.File, 0)
for _, file := range files {
if isPluginTableDocFile(file) {
filtered = append(filtered, file)
}
}
return filtered
}
func main() {
if len(os.Args) <= 2 {
log.Fatalf("Usage: %s <diff-file-path> <changes-output-file>", os.Args[0])
}
diffFile := os.Args[1]
outFile := os.Args[2]
log.Printf("Reading diff file: %s", diffFile)
patch, err := os.Open(diffFile)
if err != nil {
log.Fatal(err)
}
log.Printf("Parsing diff file: %s", diffFile)
files, _, err := gitdiff.Parse(patch)
if err != nil {
log.Fatal(err)
}
docsFiles := filterFiles(files)
changes, err := changes.GetChanges(docsFiles)
if err != nil {
log.Fatal(err)
}
log.Printf("Detected %d doc changes", len(changes))
out, err := json.MarshalIndent(changes, "", " ")
if err != nil {
log.Fatal(err)
}
os.WriteFile(outFile, out, 0644)
}