Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions chapter5/find.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package main
package main

import (

"fmt"
"path/filepath"
"os"
"path/filepath"
)

func WalkFunction(path string, info os.FileInfo, err error) error {
Expand Down Expand Up @@ -37,4 +36,4 @@ func main() {
os.Exit(1)
}

}
}
127 changes: 127 additions & 0 deletions chapter5/findwithflag.go
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 {
Copy link
Collaborator

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

Copy link
Owner Author

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

Copy link
Collaborator

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?

Copy link
Owner Author

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

fmt.Println("Not enough arguments!")
os.Exit(1)
}

Path := flags[0]

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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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

Copy link
Owner Author

Choose a reason for hiding this comment

The 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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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

Copy link
Owner Author

Choose a reason for hiding this comment

The 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)
}
}