-
Notifications
You must be signed in to change notification settings - Fork 0
adding new version of find which customizes the behaviour using flag … #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
func excludeNames(name string, exclude string) bool { | ||
if exclude == "" { | ||
return false | ||
} | ||
if filepath.Base(name) == exclude { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func excludeExtensions(name string, extension string) bool { | ||
if extension == "" { | ||
return false | ||
} | ||
basename := filepath.Base(name) | ||
s := strings.Split(basename, ".") | ||
length := len(s) | ||
basenameExtension := s[length-1] | ||
if basenameExtension == extension { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func main() { | ||
|
||
excludeSocket := flag.Bool("s", false, "Sockets") | ||
excludePipes := flag.Bool("p", false, "Pipes") | ||
excludeSymLinks := flag.Bool("sl", false, "Symbolic Links") | ||
excludeDirectories := flag.Bool("d", false, "Directories") | ||
excludeFiles := flag.Bool("f", false, "Files") | ||
excludeSpecificFile := flag.String("x", "", "Files") | ||
excludeExtention := flag.String("ext", "", "Extensions") | ||
|
||
flag.Parse() | ||
flags := flag.Args() | ||
|
||
printAll := false | ||
if *excludeSocket && *excludePipes && *excludeSymLinks && *excludeDirectories && *excludeFiles { | ||
printAll = true | ||
} | ||
|
||
if !(*excludeSocket || *excludePipes || *excludeSymLinks || *excludeDirectories || *excludeFiles) { | ||
printAll = true | ||
} | ||
|
||
if len(flags) == 0 { | ||
fmt.Println("Not enough arguments!") | ||
os.Exit(1) | ||
} | ||
|
||
Path := flags[0] | ||
mohandutt134 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
walkFunction := func(path string, info os.FileInfo, err error) error { | ||
fileInfo, err := os.Stat(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if excludeNames(path, *excludeSpecificFile) { | ||
return nil | ||
} | ||
|
||
if excludeExtensions(path, *excludeExtention) { | ||
return nil | ||
} | ||
|
||
if printAll == true { | ||
fmt.Println(path) | ||
return nil | ||
} | ||
|
||
mode := fileInfo.Mode() | ||
if mode.IsRegular() && *excludeFiles { | ||
fmt.Println(path) | ||
return nil | ||
} | ||
|
||
if mode.IsDir() && *excludeDirectories { | ||
fmt.Println(path) | ||
return nil | ||
} | ||
|
||
// os.Lstat() function that gives you information about a file or directory and | ||
// the use of the Mode() function on the return value of the os.Lstat() call in order to | ||
// compare the outcome with the os.ModeSymlink constant, which is the symbolic link bit | ||
fileInfo, _ = os.Lstat(path) | ||
if fileInfo.Mode()&os.ModeSymlink != 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there are formatting issues, please use goImports on save in sublime. Google it how to do that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added goimport on save in sublime |
||
if *excludeSymLinks { | ||
fmt.Println(path) | ||
return nil | ||
} | ||
} | ||
|
||
if fileInfo.Mode()&os.ModeNamedPipe != 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add comments above each such check and explain what are you trying to do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
if *excludePipes { | ||
fmt.Println(path) | ||
return nil | ||
} | ||
} | ||
|
||
if fileInfo.Mode()&os.ModeSocket != 0 { | ||
if *excludeSocket { | ||
fmt.Println(path) | ||
return nil | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
err := filepath.Walk(Path, walkFunction) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why check for len? if you have to treat flags as positional arguments then wh use flag package at all? then you can use os.ARGS only
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since we are using flag for flags anyways and it has abstration over os.Args which does the same job
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean using flag you can get positional as well as names arguments at the same time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes using flag.Args we can take positional arguments only difference between this and os.Args is its indexing starts from zero