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