diff options
Diffstat (limited to 'internal/admin/yaml_util.go')
| -rw-r--r-- | internal/admin/yaml_util.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/internal/admin/yaml_util.go b/internal/admin/yaml_util.go index b97a4f2..0bc2b4a 100644 --- a/internal/admin/yaml_util.go +++ b/internal/admin/yaml_util.go @@ -86,6 +86,45 @@ func listYAMLFiles(dataDir, subdir string) ([]string, error) { return ids, nil } +type YAMLTree struct { + Root []string `json:"root"` + Dirs map[string][]string `json:"dirs"` +} + +func listYAMLTree(dataDir, subdir string) (*YAMLTree, error) { + tree := &YAMLTree{Root: []string{}, Dirs: map[string][]string{}} + base := filepath.Join(dataDir, subdir) + + entries, err := os.ReadDir(base) + if err != nil { + return nil, err + } + for _, e := range entries { + if !e.IsDir() && filepath.Ext(e.Name()) == ".yaml" { + tree.Root = append(tree.Root, e.Name()[:len(e.Name())-5]) + } + } + for _, e := range entries { + if e.IsDir() { + subPath := filepath.Join(base, e.Name()) + subEntries, err := os.ReadDir(subPath) + if err != nil { + continue + } + var ids []string + for _, se := range subEntries { + if !se.IsDir() && filepath.Ext(se.Name()) == ".yaml" { + ids = append(ids, se.Name()[:len(se.Name())-5]) + } + } + if len(ids) > 0 { + tree.Dirs[e.Name()] = ids + } + } + } + return tree, nil +} + func listYAMLFilesDeep(dataDir, subdir string) ([]string, error) { var ids []string base := filepath.Join(dataDir, subdir) |
