-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
129 lines (99 loc) · 2.63 KB
/
main.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
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
package main
import (
"bufio"
"errors"
"flag"
"io/fs"
"log"
"os"
"path"
"slices"
"sort"
common "github.com/jha-naman/tree-tags/common"
golang "github.com/jha-naman/tree-tags/golang"
)
var options = common.Options{}
func main() {
initOptions()
fileNames, err := getFileNames()
if err != nil {
log.Fatalf("error getting filenames: %s", err.Error())
}
tags, err := initTags(fileNames)
if err != nil {
log.Fatal("error while initialising tags:", err.Error())
}
for _, fileName := range fileNames {
tags = append(tags, golang.GetFileTags(fileName)...)
}
sort.Slice(tags, func(i, j int) bool {
return tags[i].Name < tags[j].Name
})
tagFile, err := os.Create("tags")
if err != nil {
log.Fatal("error while trying to create tag file:", err.Error())
}
writer := bufio.NewWriter(tagFile)
for _, tag := range tags {
if _, err = writer.Write(append(tag.Bytes(), []byte("\n")...)); err != nil {
log.Fatal("error while trying to write tag file:", err.Error())
}
}
if err = writer.Flush(); err != nil {
log.Fatal("error while trying to write tag file:", err.Error())
}
}
func initOptions() {
flag.BoolVar(&options.AppendMode, "a", false, "shorthand form for 'append' option")
flag.BoolVar(&options.AppendMode, "append", false, "add this flag to re-generate tags for given list of files instead of re-generating the tags file from scratch for the whole project, will remove stale tags belonging to the given list of files")
flag.Parse()
}
func getFileNames() ([]string, error) {
if options.AppendMode {
fileNames := flag.Args()
if len(fileNames) == 0 {
log.Fatal("need to supply file names when used in append mode (using -a as command line option)")
}
return fileNames, nil
}
wd, err := os.Getwd()
if err != nil {
return nil, err
}
var matchingFiles []string
fs.WalkDir(os.DirFS(wd), ".", func(filePath string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
ext := path.Ext(filePath)
if !d.IsDir() && ext == ".go" {
matchingFiles = append(matchingFiles, filePath)
}
return nil
})
return matchingFiles, nil
}
func initTags(fileNamesToSkip []string) ([]common.TagEntry, error) {
tags := []common.TagEntry{}
if !options.AppendMode {
return tags, nil
}
file, err := os.Open("tags")
if err != nil {
if !errors.Is(err, fs.ErrNotExist) {
return nil, err
}
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
text := scanner.Text()
tag, err := common.TagFromString(text)
if errors.Is(err, common.ErrStringIsAComment) {
continue
}
if !slices.Contains(fileNamesToSkip, tag.FileName) {
tags = append(tags, tag)
}
}
return tags, nil
}