/
u.go
161 lines (132 loc) · 3.9 KB
/
u.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
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
159
160
161
package main
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"regexp"
"strings"
"syscall"
"github.com/BurntSushi/toml"
"github.com/adrg/xdg"
"github.com/bayashi/xfg/internal/xfglangxt"
ignore "github.com/sabhiram/go-gitignore"
)
func defaultOptions() *options {
return &options{
SearchStart: ".",
Indent: defaultIndent,
GroupSeparator: defaultGroupSeparator,
ColorPathBase: "yellow",
ColorPath: "cyan",
ColorContent: "red",
}
}
func readRC(homeDir string) (*options, error) {
o := defaultOptions()
if xfgRCFilePath := os.Getenv(XFG_RC_ENV_KEY); xfgRCFilePath != "" {
if _, err := toml.DecodeFile(xfgRCFilePath, &o); err != nil {
return nil, fmt.Errorf("could not decode toml config env:%s `%s` : %w", XFG_RC_ENV_KEY, xfgRCFilePath, err)
}
return o, nil
}
xfgRCFilePath := filepath.Join(xdg.ConfigHome, XFG_RC_FILE)
_, err := toml.DecodeFile(xfgRCFilePath, &o)
if err == nil {
return o, nil
} else if !errors.Is(err, syscall.ENOENT) {
return nil, fmt.Errorf("%s : %w", xfgRCFilePath, err)
}
xfgRCFilePath = filepath.Join(homeDir, XFG_RC_FILE)
_, err = toml.DecodeFile(xfgRCFilePath, &o)
if err != nil && !errors.Is(err, syscall.ENOENT) {
return nil, fmt.Errorf("%s : %w", xfgRCFilePath, err)
}
return o, nil
}
func prepareGitIgnore(homeDir string, sPath string) *ignore.GitIgnore {
const GIT_IGNOE_FILE_NAME = ".gitignore"
// read .gitignore file in start directory to search or home directory
// There would be no .gitignore file, then `gitignore` variable will be `nil`.
gitignore, _ := ignore.CompileIgnoreFile(filepath.Join(sPath, GIT_IGNOE_FILE_NAME))
if gitignore == nil {
gitignore, _ = ignore.CompileIgnoreFile(filepath.Join(homeDir, GIT_IGNOE_FILE_NAME))
}
return gitignore
}
func prepareXfgIgnore(homeDir string, xfgignoreFilePath string) *ignore.GitIgnore {
if xfgignoreFilePath != "" {
xfgignore, _ := ignore.CompileIgnoreFile(xfgignoreFilePath)
if xfgignore != nil {
return xfgignore
}
}
const XFG_IGNOE_FILE_NAME = ".xfgignore"
// read .xfgignore file in XDG Base directory or home directory
// There would be no .xfgignore file, then `xfgignore` variable will be `nil`.
xfgignore, _ := ignore.CompileIgnoreFile(filepath.Join(xdg.ConfigHome, XFG_IGNOE_FILE_NAME))
if xfgignore == nil {
xfgignore, _ = ignore.CompileIgnoreFile(filepath.Join(homeDir, XFG_IGNOE_FILE_NAME))
}
return xfgignore
}
func isBinaryFile(fh *os.File) (bool, error) {
dat := make([]byte, 8000)
n, err := fh.Read(dat)
if err != nil {
return false, fmt.Errorf("could not read fh : %w", err)
}
for _, c := range dat[:n] {
if c == 0x00 {
return true, nil
}
}
return false, nil
}
func isRegularFile(fInfo fs.DirEntry) bool {
if fInfo.Type() != 0 {
return false
}
fi, err := fInfo.Info()
return err == nil && fi.Size() > 0
}
func validateStartPath(startPath string) error {
d, err := os.Stat(startPath)
if err != nil {
return err
}
if !d.IsDir() {
return fmt.Errorf("path `%s` should point to a directory", startPath)
}
return nil
}
func validateLanguageCondition(lang []string) error {
for _, l := range lang {
if !xfglangxt.IsSupported(l) {
return fmt.Errorf("`%s` is not supported. --type-list prints all support languages", l)
}
}
return nil
}
func isMatch(target string, included string) bool {
if target == "" || included == "" {
return false
}
return strings.Contains(target, included)
}
func isMatchRegexp(target string, re *regexp.Regexp) bool {
if target == "" || re == nil {
return false
}
return re.MatchString(target)
}
func isDefaultSkipFile(fInfo fs.DirEntry) bool {
return !fInfo.IsDir() && (fInfo.Name() == ".gitkeep" ||
strings.HasSuffix(fInfo.Name(), ".min.js") || strings.HasSuffix(fInfo.Name(), ".min.css"))
}
func isDefaultSkipDir(fInfo fs.DirEntry) bool {
return fInfo.IsDir() && (fInfo.Name() == ".git" || fInfo.Name() == ".svn" ||
fInfo.Name() == "node_modules" ||
fInfo.Name() == "vendor")
}