fork/gdu

git clone https://git.julia.blue/fork/gdu

Julia RyanDan's preferred ignore behavior04c9958

ignore
7.6 KiB259 linesraw
1// Package common contains commong logic and interfaces used across Gdu
2// nolint: revive //Why: this is common package
3package common
4
5import (
6	"bufio"
7	"os"
8	"path/filepath"
9	"regexp"
10	"strings"
11
12	log "github.com/sirupsen/logrus"
13)
14
15// CreateIgnorePattern creates one pattern from all path patterns
16func CreateIgnorePattern(paths []string) (compiled *regexp.Regexp, err error) {
17	for i, path := range paths {
18		if _, err = regexp.Compile(path); err != nil {
19			return nil, err
20		}
21		if !filepath.IsAbs(path) {
22			absPath, err := filepath.Abs(path)
23			if err == nil {
24				paths = append(paths, absPath)
25			}
26		} else {
27			relPath, err := filepath.Rel("/", path)
28			if err == nil {
29				paths = append(paths, relPath)
30			}
31		}
32		paths[i] = "(" + path + ")"
33	}
34
35	ignore := `^(?:` + strings.Join(paths, "|") + `)$`
36	return regexp.Compile(ignore)
37}
38
39// SetIgnoreDirPaths sets paths to ignore
40func (ui *UI) SetIgnoreDirPaths(paths []string) {
41	log.Printf("Ignoring dirs %s", strings.Join(paths, ", "))
42	ui.IgnoreDirPaths = make(map[string]struct{}, len(paths)*2)
43	for _, path := range paths {
44		ui.IgnoreDirPaths[path] = struct{}{}
45		if !filepath.IsAbs(path) {
46			if absPath, err := filepath.Abs(path); err == nil {
47				ui.IgnoreDirPaths[absPath] = struct{}{}
48			}
49		} else {
50			if relPath, err := filepath.Rel("/", path); err == nil {
51				ui.IgnoreDirPaths[relPath] = struct{}{}
52			}
53		}
54	}
55}
56
57func (ui *UI) AddIgnoreDirPath(path string) {
58	log.Printf("Ignoring dir %s", path)
59	if ui.IgnoreDirPaths == nil {
60		ui.IgnoreDirPaths = make(map[string]struct{})
61	}
62	ui.IgnoreDirPaths[path] = struct{}{}
63	if !filepath.IsAbs(path) {
64		// TODO: how can abs error?
65		if absPath, err := filepath.Abs(path); err == nil {
66			ui.IgnoreDirPaths[absPath] = struct{}{}
67		}
68	} else {
69		// TODO: how can rel error?
70		if relPath, err := filepath.Rel("/", path); err == nil {
71			ui.IgnoreDirPaths[relPath] = struct{}{}
72		}
73	}
74}
75
76// SetIgnoreDirPatterns sets regular patterns of dirs to ignore
77func (ui *UI) SetIgnoreDirPatterns(paths []string) error {
78	var err error
79	log.Printf("Ignoring dir patterns %s", strings.Join(paths, ", "))
80	ui.IgnoreDirPathPatterns, err = CreateIgnorePattern(paths)
81	return err
82}
83
84// SetIgnoreFromFile sets regular patterns of dirs to ignore
85func (ui *UI) SetIgnoreFromFile(ignoreFile string) error {
86	var err error
87	var paths []string
88	log.Printf("Reading ignoring dir patterns from file '%s'", ignoreFile)
89
90	file, err := os.Open(ignoreFile)
91	if err != nil {
92		return err
93	}
94	defer file.Close()
95
96	scanner := bufio.NewScanner(file)
97	for scanner.Scan() {
98		paths = append(paths, scanner.Text())
99	}
100
101	if err := scanner.Err(); err != nil {
102		return err
103	}
104
105	ui.IgnoreDirPathPatterns, err = CreateIgnorePattern(paths)
106	return err
107}
108
109// SetIgnoreTypes sets file types to ignore
110func (ui *UI) SetIgnoreTypes(types []string) {
111	log.Printf("Ignoring file types: %s", strings.Join(types, ", "))
112	ui.IgnoreTypes = types
113}
114
115// SetIncludeTypes sets file types to include (whitelist)
116func (ui *UI) SetIncludeTypes(types []string) {
117	log.Printf("Including only file types: %s", strings.Join(types, ", "))
118	ui.IncludeTypes = types
119}
120
121// SetIgnoreHidden sets flags if hidden dirs should be ignored
122func (ui *UI) SetIgnoreHidden(value bool) {
123	log.Printf("Ignoring hidden dirs")
124	ui.IgnoreHidden = value
125}
126
127// ShouldDirBeIgnored returns true if given path should be ignored
128func (ui *UI) ShouldDirBeIgnored(name, path string) bool {
129	_, shouldIgnore := ui.IgnoreDirPaths[path]
130	if shouldIgnore {
131		log.Printf("Directory %s ignored", path)
132	}
133	return shouldIgnore
134}
135
136// ShouldDirBeIgnoredUsingPattern returns true if given path should be ignored
137func (ui *UI) ShouldDirBeIgnoredUsingPattern(name, path string) bool {
138	shouldIgnore := ui.IgnoreDirPathPatterns.MatchString(path)
139	if shouldIgnore {
140		log.Printf("Directory %s ignored", path)
141	}
142	return shouldIgnore
143}
144
145// IsHiddenDir returns if the dir name begins with dot
146func (ui *UI) IsHiddenDir(name, path string) bool {
147	shouldIgnore := name[0] == '.'
148	if shouldIgnore {
149		log.Printf("Directory %s ignored", path)
150	}
151	return shouldIgnore
152}
153
154// ShouldFileBeIgnoredByType returns true if file should be ignored based on its extension
155func (ui *UI) ShouldFileBeIgnoredByType(name string) bool {
156	if len(ui.IgnoreTypes) == 0 {
157		return false
158	}
159
160	ext := strings.ToLower(filepath.Ext(name))
161	if ext == "" {
162		return false // No extension, don't ignore
163	}
164
165	// Remove leading dot from extension
166	ext = strings.TrimPrefix(ext, ".")
167
168	for _, ignoreType := range ui.IgnoreTypes {
169		// Remove leading dot from ignoreType
170		cleanIgnoreType := strings.TrimPrefix(strings.ToLower(ignoreType), ".")
171		if cleanIgnoreType == ext {
172			log.Printf("File %s ignored by type", name)
173			return true
174		}
175	}
176	return false
177}
178
179// ShouldFileBeIncludedByType returns true if file should be included based on its extension
180func (ui *UI) ShouldFileBeIncludedByType(name string) bool {
181	if len(ui.IncludeTypes) == 0 {
182		return true // No include filter, include all
183	}
184
185	ext := strings.ToLower(filepath.Ext(name))
186	if ext == "" {
187		return false // No extension, don't include if we have include filter
188	}
189
190	// Remove leading dot from extension
191	ext = strings.TrimPrefix(ext, ".")
192
193	for _, includeType := range ui.IncludeTypes {
194		// Remove leading dot from includeType
195		cleanIncludeType := strings.TrimPrefix(strings.ToLower(includeType), ".")
196		if cleanIncludeType == ext {
197			return true
198		}
199	}
200
201	log.Printf("File %s excluded by type filter", name)
202	return false
203}
204
205// CreateIgnoreFunc returns function for detecting if dir should be ignored
206// nolint: gocyclo // Why: This function is a switch statement that is not too complex
207func (ui *UI) CreateIgnoreFunc() ShouldDirBeIgnored {
208	switch {
209	case len(ui.IgnoreDirPaths) > 0 && ui.IgnoreDirPathPatterns == nil && !ui.IgnoreHidden:
210		return ui.ShouldDirBeIgnored
211	case len(ui.IgnoreDirPaths) > 0 && ui.IgnoreDirPathPatterns != nil && !ui.IgnoreHidden:
212		return func(name, path string) bool {
213			return ui.ShouldDirBeIgnored(name, path) || ui.ShouldDirBeIgnoredUsingPattern(name, path)
214		}
215	case len(ui.IgnoreDirPaths) > 0 && ui.IgnoreDirPathPatterns != nil && ui.IgnoreHidden:
216		return func(name, path string) bool {
217			return ui.ShouldDirBeIgnored(name, path) || ui.ShouldDirBeIgnoredUsingPattern(name, path) || ui.IsHiddenDir(name, path)
218		}
219	case len(ui.IgnoreDirPaths) == 0 && ui.IgnoreDirPathPatterns != nil && ui.IgnoreHidden:
220		return func(name, path string) bool {
221			return ui.ShouldDirBeIgnoredUsingPattern(name, path) || ui.IsHiddenDir(name, path)
222		}
223	case len(ui.IgnoreDirPaths) == 0 && ui.IgnoreDirPathPatterns != nil && !ui.IgnoreHidden:
224		return ui.ShouldDirBeIgnoredUsingPattern
225	case len(ui.IgnoreDirPaths) == 0 && ui.IgnoreDirPathPatterns == nil && ui.IgnoreHidden:
226		return ui.IsHiddenDir
227	case len(ui.IgnoreDirPaths) > 0 && ui.IgnoreDirPathPatterns == nil && ui.IgnoreHidden:
228		return func(name, path string) bool {
229			return ui.ShouldDirBeIgnored(name, path) || ui.IsHiddenDir(name, path)
230		}
231	default:
232		return func(name, path string) bool { return false }
233	}
234}
235
236// CreateFileTypeFilter returns function for detecting if file should be ignored based on type
237func (ui *UI) CreateFileTypeFilter() ShouldFileBeIgnored {
238	// If we have include types, use whitelist mode
239	if len(ui.IncludeTypes) > 0 {
240		return func(name string) bool {
241			return !ui.ShouldFileBeIncludedByType(name)
242		}
243	}
244
245	// If we have ignore types, use blacklist mode
246	if len(ui.IgnoreTypes) > 0 {
247		return func(name string) bool {
248			return ui.ShouldFileBeIgnoredByType(name)
249		}
250	}
251
252	// No type filtering - return nil to indicate no filtering is needed
253	return nil
254}
255
256// IsFilteringFiles returns true if we have any file type filters set
257func (ui *UI) IsFilteringFiles() bool {
258	return len(ui.IgnoreTypes) > 0 || len(ui.IncludeTypes) > 0 || ui.FilteringFiles
259}