From 0c2b11aa16dd26d4687348d9f01ec509779c95d3 Mon Sep 17 00:00:00 2001 From: haridutt Date: Sun, 7 Oct 2018 22:08:54 +0530 Subject: [PATCH 1/6] adding new version of find which customizes the behaviour using flag package --- chapter5/findwithflag.go | 125 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 chapter5/findwithflag.go diff --git a/chapter5/findwithflag.go b/chapter5/findwithflag.go new file mode 100644 index 0000000..9532b4b --- /dev/null +++ b/chapter5/findwithflag.go @@ -0,0 +1,125 @@ +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() { + + minusS := flag.Bool("s", false, "Sockets") + minusP := flag.Bool("p", false, "Pipes") + minusSL := flag.Bool("sl", false, "Symbolic Links") + minusD := flag.Bool("d", false, "Directories") + minusF := flag.Bool("f", false, "Files") + minusX := flag.String("x", "", "Files") + minusEXT := flag.String("ext", "", "Extensions") + + flag.Parse() + flags := flag.Args() + + printAll := false + if *minusS && *minusP && *minusSL && *minusD && *minusF { + printAll = true + } + + if !(*minusS || *minusP || *minusSL || *minusD || *minusF) { + 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, *minusX) { + return nil + } + + if excludeExtensions(path, *minusEXT) { + return nil + } + + if printAll == true { + fmt.Println(path) + return nil + } + + mode := fileInfo.Mode() + if mode.IsRegular() && *minusF { + fmt.Println(path) + return nil + } + + if mode.IsDir() && *minusD { + fmt.Println(path) + return nil + } + + fileInfo, _ = os.Lstat(path) + if fileInfo.Mode()&os.ModeSymlink != 0 { + if *minusSL { + fmt.Println(path) + return nil + } + } + + if fileInfo.Mode()&os.ModeNamedPipe != 0 { + if *minusP { + fmt.Println(path) + return nil + } + } + + if fileInfo.Mode()&os.ModeSocket != 0 { + if *minusS { + fmt.Println(path) + return nil + } + } + + return nil + } + + err := filepath.Walk(Path, walkFunction) + if err != nil { + fmt.Println(err) + os.Exit(1) + } +} + From c47ed4591be39b9ef69e7186bef755ca400605c0 Mon Sep 17 00:00:00 2001 From: haridutt Date: Mon, 8 Oct 2018 07:26:22 +0530 Subject: [PATCH 2/6] comments add and variables renamed --- chapter5/findwithflag.go | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/chapter5/findwithflag.go b/chapter5/findwithflag.go index 9532b4b..de7a8b7 100644 --- a/chapter5/findwithflag.go +++ b/chapter5/findwithflag.go @@ -34,23 +34,23 @@ func excludeExtensions(name string, extension string) bool { func main() { - minusS := flag.Bool("s", false, "Sockets") - minusP := flag.Bool("p", false, "Pipes") - minusSL := flag.Bool("sl", false, "Symbolic Links") - minusD := flag.Bool("d", false, "Directories") - minusF := flag.Bool("f", false, "Files") - minusX := flag.String("x", "", "Files") - minusEXT := flag.String("ext", "", "Extensions") + 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 *minusS && *minusP && *minusSL && *minusD && *minusF { + if *excludeSocket && *excludePipes && *excludeSymLinks && *excludeDirectories && *excludeFiles { printAll = true } - if !(*minusS || *minusP || *minusSL || *minusD || *minusF) { + if !(*excludeSocket || *excludePipes || *excludeSymLinks || *excludeDirectories || *excludeFiles) { printAll = true } @@ -67,11 +67,11 @@ func main() { return err } - if excludeNames(path, *minusX) { + if excludeNames(path, *excludeSpecificFile ) { return nil } - if excludeExtensions(path, *minusEXT) { + if excludeExtensions(path, *excludeExtention) { return nil } @@ -81,33 +81,36 @@ func main() { } mode := fileInfo.Mode() - if mode.IsRegular() && *minusF { + if mode.IsRegular() && *excludeFiles { fmt.Println(path) return nil } - if mode.IsDir() && *minusD { + 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 *minusSL { + if *excludeSymLinks { fmt.Println(path) return nil } } if fileInfo.Mode()&os.ModeNamedPipe != 0 { - if *minusP { + if *excludePipes { fmt.Println(path) return nil } } if fileInfo.Mode()&os.ModeSocket != 0 { - if *minusS { + if *excludeSocket { fmt.Println(path) return nil } From 8b2bf9f072af0b19ac60c22743944e498e3e76a9 Mon Sep 17 00:00:00 2001 From: haridutt Date: Mon, 8 Oct 2018 15:34:01 +0530 Subject: [PATCH 3/6] formatting done using goimport --- chapter5/find.go | 7 +++---- chapter5/findwithflag.go | 3 +-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/chapter5/find.go b/chapter5/find.go index 4795de6..fd2783f 100644 --- a/chapter5/find.go +++ b/chapter5/find.go @@ -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 { @@ -37,4 +36,4 @@ func main() { os.Exit(1) } -} \ No newline at end of file +} diff --git a/chapter5/findwithflag.go b/chapter5/findwithflag.go index de7a8b7..acae28a 100644 --- a/chapter5/findwithflag.go +++ b/chapter5/findwithflag.go @@ -67,7 +67,7 @@ func main() { return err } - if excludeNames(path, *excludeSpecificFile ) { + if excludeNames(path, *excludeSpecificFile) { return nil } @@ -125,4 +125,3 @@ func main() { os.Exit(1) } } - From e6524eb89192bc542c8381229a227adc76257e67 Mon Sep 17 00:00:00 2001 From: haridutt Date: Tue, 9 Oct 2018 18:36:35 +0530 Subject: [PATCH 4/6] copies the directory structure of source to destination without copying files --- chapter5/cpDirStructure.go | 73 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 chapter5/cpDirStructure.go diff --git a/chapter5/cpDirStructure.go b/chapter5/cpDirStructure.go new file mode 100644 index 0000000..88aae57 --- /dev/null +++ b/chapter5/cpDirStructure.go @@ -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) + } + +} From 79f688635a7a48cd1b7a2080d8eafec839a40c0a Mon Sep 17 00:00:00 2001 From: haridutt Date: Tue, 9 Oct 2018 18:54:32 +0530 Subject: [PATCH 5/6] deleted cpDirstructure.go --- chapter5/cpDirStructure.go | 73 -------------------------------------- 1 file changed, 73 deletions(-) delete mode 100644 chapter5/cpDirStructure.go diff --git a/chapter5/cpDirStructure.go b/chapter5/cpDirStructure.go deleted file mode 100644 index 88aae57..0000000 --- a/chapter5/cpDirStructure.go +++ /dev/null @@ -1,73 +0,0 @@ -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) - } - -} From 10c2e55f3fc4fe3103367a4c6080ccd60594e0f0 Mon Sep 17 00:00:00 2001 From: haridutt Date: Tue, 9 Oct 2018 18:59:53 +0530 Subject: [PATCH 6/6] copies the directory structure of source to destination without copying files --- chapter5/cpDirStructure.go | 73 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 chapter5/cpDirStructure.go diff --git a/chapter5/cpDirStructure.go b/chapter5/cpDirStructure.go new file mode 100644 index 0000000..88aae57 --- /dev/null +++ b/chapter5/cpDirStructure.go @@ -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) + } + +}