package main import ( "fmt" "os" "path/filepath" "sort" "strings" ) // printTree renders the entry tree under sub in the same shape pass does (via // `tree`): directories and entries sorted together, `.gpg` suffixes stripped, // dotfiles hidden. func printTree(s *store, sub string) error { root := filepath.Join(s.dir, sub) if fi, err := os.Stat(root); err != nil || !fi.IsDir() { return errf("Error: %s is not in the password store.", sub) } if sub == "" { fmt.Println("Password Store") } else { fmt.Println(sub) } return printBranch(root, "") } func printBranch(dir, prefix string) error { ents, err := os.ReadDir(dir) if err != nil { return err } var items []os.DirEntry for _, e := range ents { if strings.HasPrefix(e.Name(), ".") { continue } if !e.IsDir() && !strings.HasSuffix(e.Name(), ".gpg") { continue } items = append(items, e) } sort.Slice(items, func(i, j int) bool { return items[i].Name() < items[j].Name() }) for i, e := range items { last := i == len(items)-1 branch, extend := "├── ", "│ " if last { branch, extend = "└── ", " " } name := e.Name() if !e.IsDir() { name = strings.TrimSuffix(name, ".gpg") } fmt.Println(prefix + branch + name) if e.IsDir() { if err := printBranch(filepath.Join(dir, e.Name()), prefix+extend); err != nil { return err } } } return nil }