fork/gdu
git clone https://git.julia.blue/fork/gdu
5782be9
master
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 57// SetIgnoreDirPatterns sets regular patterns of dirs to ignore 58func (ui * UI )SetIgnoreDirPatterns (paths []string )error { 59var err error 60log .Printf ("Ignoring dir patterns %s" ,strings .Join (paths ,", " )) 61ui .IgnoreDirPathPatterns ,err = CreateIgnorePattern (paths ) 62return err 63} 64 65// SetIgnoreFromFile sets regular patterns of dirs to ignore 66func (ui * UI )SetIgnoreFromFile (ignoreFile string )error { 67var err error 68var paths []string 69log .Printf ("Reading ignoring dir patterns from file '%s'" ,ignoreFile ) 70 71file ,err := os .Open (ignoreFile ) 72if err != nil { 73return err 74 } 75defer file .Close () 76 77scanner := bufio .NewScanner (file ) 78for scanner .Scan () { 79paths = append (paths ,scanner .Text ()) 80 } 81 82if err := scanner .Err ();err != nil { 83return err 84 } 85 86ui .IgnoreDirPathPatterns ,err = CreateIgnorePattern (paths ) 87return err 88} 89 90// SetIgnoreTypes sets file types to ignore 91func (ui * UI )SetIgnoreTypes (types []string ) { 92log .Printf ("Ignoring file types: %s" ,strings .Join (types ,", " )) 93ui .IgnoreTypes = types 94} 95 96// SetIncludeTypes sets file types to include (whitelist) 97func (ui * UI )SetIncludeTypes (types []string ) { 98log .Printf ("Including only file types: %s" ,strings .Join (types ,", " )) 99ui .IncludeTypes = types 100} 101 102// SetIgnoreHidden sets flags if hidden dirs should be ignored 103func (ui * UI )SetIgnoreHidden (value bool ) { 104log .Printf ("Ignoring hidden dirs" ) 105ui .IgnoreHidden = value 106} 107 108// ShouldDirBeIgnored returns true if given path should be ignored 109func (ui * UI )ShouldDirBeIgnored (name ,path string )bool { 110_ ,shouldIgnore := ui .IgnoreDirPaths [path ] 111if shouldIgnore { 112log .Printf ("Directory %s ignored" ,path ) 113 } 114return shouldIgnore 115} 116 117// ShouldDirBeIgnoredUsingPattern returns true if given path should be ignored 118func (ui * UI )ShouldDirBeIgnoredUsingPattern (name ,path string )bool { 119shouldIgnore := ui .IgnoreDirPathPatterns .MatchString (path ) 120if shouldIgnore { 121log .Printf ("Directory %s ignored" ,path ) 122 } 123return shouldIgnore 124} 125 126// IsHiddenDir returns if the dir name begins with dot 127func (ui * UI )IsHiddenDir (name ,path string )bool { 128shouldIgnore := name [0 ]== '.' 129if shouldIgnore { 130log .Printf ("Directory %s ignored" ,path ) 131 } 132return shouldIgnore 133} 134 135// ShouldFileBeIgnoredByType returns true if file should be ignored based on its extension 136func (ui * UI )ShouldFileBeIgnoredByType (name string )bool { 137if len (ui .IgnoreTypes )== 0 { 138return false 139 } 140 141ext := strings .ToLower (filepath .Ext (name )) 142if ext == "" { 143return false // No extension, don't ignore 144 } 145 146// Remove leading dot from extension 147ext = strings .TrimPrefix (ext ,"." ) 148 149for _ ,ignoreType := range ui .IgnoreTypes { 150// Remove leading dot from ignoreType 151cleanIgnoreType := strings .TrimPrefix (strings .ToLower (ignoreType ),"." ) 152if cleanIgnoreType == ext { 153log .Printf ("File %s ignored by type" ,name ) 154return true 155 } 156 } 157return false 158} 159 160// ShouldFileBeIncludedByType returns true if file should be included based on its extension 161func (ui * UI )ShouldFileBeIncludedByType (name string )bool { 162if len (ui .IncludeTypes )== 0 { 163return true // No include filter, include all 164 } 165 166ext := strings .ToLower (filepath .Ext (name )) 167if ext == "" { 168return false // No extension, don't include if we have include filter 169 } 170 171// Remove leading dot from extension 172ext = strings .TrimPrefix (ext ,"." ) 173 174for _ ,includeType := range ui .IncludeTypes { 175// Remove leading dot from includeType 176cleanIncludeType := strings .TrimPrefix (strings .ToLower (includeType ),"." ) 177if cleanIncludeType == ext { 178return true 179 } 180 } 181 182log .Printf ("File %s excluded by type filter" ,name ) 183return false 184} 185 186// CreateIgnoreFunc returns function for detecting if dir should be ignored 187// nolint: gocyclo // Why: This function is a switch statement that is not too complex 188func (ui * UI )CreateIgnoreFunc ()ShouldDirBeIgnored { 189switch { 190case len (ui .IgnoreDirPaths )> 0 && ui .IgnoreDirPathPatterns == nil && ! ui .IgnoreHidden : 191return ui .ShouldDirBeIgnored 192case len (ui .IgnoreDirPaths )> 0 && ui .IgnoreDirPathPatterns != nil && ! ui .IgnoreHidden : 193return func (name ,path string )bool { 194return ui .ShouldDirBeIgnored (name ,path )|| ui .ShouldDirBeIgnoredUsingPattern (name ,path ) 195 } 196case len (ui .IgnoreDirPaths )> 0 && ui .IgnoreDirPathPatterns != nil && ui .IgnoreHidden : 197return func (name ,path string )bool { 198return ui .ShouldDirBeIgnored (name ,path )|| ui .ShouldDirBeIgnoredUsingPattern (name ,path )|| ui .IsHiddenDir (name ,path ) 199 } 200case len (ui .IgnoreDirPaths )== 0 && ui .IgnoreDirPathPatterns != nil && ui .IgnoreHidden : 201return func (name ,path string )bool { 202return ui .ShouldDirBeIgnoredUsingPattern (name ,path )|| ui .IsHiddenDir (name ,path ) 203 } 204case len (ui .IgnoreDirPaths )== 0 && ui .IgnoreDirPathPatterns != nil && ! ui .IgnoreHidden : 205return ui .ShouldDirBeIgnoredUsingPattern 206case len (ui .IgnoreDirPaths )== 0 && ui .IgnoreDirPathPatterns == nil && ui .IgnoreHidden : 207return ui .IsHiddenDir 208case len (ui .IgnoreDirPaths )> 0 && ui .IgnoreDirPathPatterns == nil && ui .IgnoreHidden : 209return func (name ,path string )bool { 210return ui .ShouldDirBeIgnored (name ,path )|| ui .IsHiddenDir (name ,path ) 211 } 212default : 213return func (name ,path string )bool {return false } 214 } 215} 216 217// CreateFileTypeFilter returns function for detecting if file should be ignored based on type 218func (ui * UI )CreateFileTypeFilter ()ShouldFileBeIgnored { 219// If we have include types, use whitelist mode 220if len (ui .IncludeTypes )> 0 { 221return func (name string )bool { 222return ! ui .ShouldFileBeIncludedByType (name ) 223 } 224 } 225 226// If we have ignore types, use blacklist mode 227if len (ui .IgnoreTypes )> 0 { 228return func (name string )bool { 229return ui .ShouldFileBeIgnoredByType (name ) 230 } 231 } 232 233// No type filtering - return nil to indicate no filtering is needed 234return nil 235} 236 237// IsFilteringFiles returns true if we have any file type filters set 238func (ui * UI )IsFilteringFiles ()bool { 239return len (ui .IgnoreTypes )> 0 || len (ui .IncludeTypes )> 0 || ui .FilteringFiles 240}