From d2803affc10755fe711fad7d7b62c4636935fbc4 Mon Sep 17 00:00:00 2001 From: David Stark Date: Mon, 9 Dec 2019 18:23:48 +0000 Subject: [PATCH] testing --- playground/3-find-files/files.go | 51 +++++++++++++++++++++++++++++++ playground/3-find-files/test1.log | 2 ++ playground/3-find-files/test2.log | 0 3 files changed, 53 insertions(+) create mode 100644 playground/3-find-files/files.go create mode 100644 playground/3-find-files/test1.log create mode 100644 playground/3-find-files/test2.log diff --git a/playground/3-find-files/files.go b/playground/3-find-files/files.go new file mode 100644 index 0000000..c2d6371 --- /dev/null +++ b/playground/3-find-files/files.go @@ -0,0 +1,51 @@ +package main + +import ( + "fmt" + "log" + "os" + "os/user" + "path/filepath" + "strings" +) + +func TrimSuffix(s, suffix string) string { + if strings.HasSuffix(s, suffix) { + s = s[:len(s)-len(suffix)] + } + return s +} + +func main() { + var searchPath []string + searchPath = append(searchPath, + "/home/starkers/.local/share/Steam/steamapps/compatdata/8500/pfx/drive_c/users/steamuser/My Documents/EVE/logs/Chatlogs") + + usr, _ := user.Current() + dir := usr.HomeDir + + for _, d := range searchPath { + + // if has trailing slash, remove it + d = TrimSuffix(d, "/") + + // do a unix like expansion if the path starts like `~/` + if strings.HasPrefix(d, "~/") { + d = filepath.Join(dir, d[2:]) + } + f, err := os.Open(d) + if err != nil { + log.Fatal(err) + } + files, err := f.Readdir(-1) + f.Close() + if err != nil { + log.Fatal(err) + } + + for _, file := range files { + fmt.Printf("%v/%v\n", d, file.Name()) + } + } + +} diff --git a/playground/3-find-files/test1.log b/playground/3-find-files/test1.log new file mode 100644 index 0000000..bfc04a9 --- /dev/null +++ b/playground/3-find-files/test1.log @@ -0,0 +1,2 @@ +11 +22 diff --git a/playground/3-find-files/test2.log b/playground/3-find-files/test2.log new file mode 100644 index 0000000..e69de29