Created
October 18, 2019 12:49
-
-
Save schaternik/06fdf53e7f580d4946514e1ee2751497 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io" | |
"os" | |
"path" | |
) | |
func dirTree(out io.Writer, root string, printFiles bool) error { | |
dirRead, err := os.Open(root) | |
if err != nil { | |
return err | |
} | |
dirFiles, err := dirRead.Readdir(0) | |
if err != nil { | |
return err | |
} | |
for _, file := range dirFiles { | |
filename := file.Name() | |
fullPath := path.Join(root, filename) | |
if file.IsDir() { | |
fmt.Println(fullPath) | |
dirTree(out, fullPath, printFiles) | |
} else { | |
if printFiles == true { | |
fmt.Println(fullPath) | |
} | |
} | |
} | |
return nil | |
} | |
func main() { | |
out := os.Stdout | |
if !(len(os.Args) == 2 || len(os.Args) == 3) { | |
panic("usage go run main.go . [-f]") | |
} | |
path := os.Args[1] | |
printFiles := len(os.Args) == 3 && os.Args[2] == "-f" | |
err := dirTree(out, path, printFiles) | |
if err != nil { | |
panic(err.Error()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment