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
73 changes: 73 additions & 0 deletions chapter5/cpDirStructure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
)

func main() {

Test := flag.Bool("t", false, "testing only")
flag.Parse()

args := flag.Args()
source := args[0]
destination := args[1]

if len(args) == 0 || len(args) == 1 {
fmt.Println("Not enough arguments!")
os.Exit(1)
}

permission := os.ModePerm

_, err := os.Stat(destination)

if os.IsNotExist(err) {
os.MkdirAll(destination, permission)
} else {
fmt.Println(destination, " already exist")
os.Exit(1)
}

WalkFunction := func(currentpath string, _ os.FileInfo, err error) error {

fileinfo, _ := os.Lstat(currentpath)
if fileinfo.Mode()&os.ModeSymlink != 0 {
fmt.Println(currentpath, "skipping")
return nil
}
fileinfo, err = os.Stat(currentpath)

if err != nil {
fmt.Println("*", err)
return err
}
mode := fileinfo.Mode()
if mode.IsDir() {
tempPath := strings.Replace(currentpath, source, "", 1)
pathToCreate := destination + "/" + filepath.Base(source) +
tempPath
if *Test {
fmt.Println(":", pathToCreate)
return nil
}
_, err := os.Stat(pathToCreate)
if os.IsNotExist(err) {
os.MkdirAll(pathToCreate, permission)
} else {
fmt.Println("Did not create", pathToCreate, ":", err)
}
}
return nil
}
err = filepath.Walk(source, WalkFunction)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

}
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 {
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 {
if *excludeSymLinks {
fmt.Println(path)
return nil
}
}

if fileInfo.Mode()&os.ModeNamedPipe != 0 {
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)
}
}