util: Improve tree printing function

This makes it behave more like the core GNU tree util.
This commit is contained in:
James Shubin
2024-02-21 13:30:14 -05:00
parent 733d7fb55f
commit d117cb8ed5
4 changed files with 12 additions and 5 deletions

View File

@@ -29,8 +29,15 @@ import (
// FsTree returns a string representation of the file system tree similar to the
// well-known `tree` command.
func FsTree(fs afero.Fs, name string) (string, error) {
str := ".\n" // top level dir
s, err := stringify(fs, path.Clean(name), []bool{})
clean := path.Clean(name)
str := clean + "/\n" // named dir
if name == "" {
str = ".\n" // relative dir
}
if name == "/" {
str = "/\n" // root dir
}
s, err := stringify(fs, clean, []bool{})
if err != nil {
return "", err
}