arns-lt-tree-push-service/formatter.go
Arnas Udovicius 5f9b822a02
All checks were successful
continuous-integration/drone/push Build is passing
change port; fix categories
2022-08-17 14:32:39 +03:00

67 lines
1.4 KiB
Go

package main
import (
zord_tree "g.arns.lt/zordsdavini/zord-tree"
"strings"
)
type FSummery struct {
Name string
Category []string
Description string
Id string
Lang string
Created string
Copyright string
Tags []string
}
type TSummery struct {
Files []FSummery
Tags map[string]int
Categories map[string]int
}
type FileContent struct {
File zord_tree.File
Content string
}
func FormatTreeSummery(tree zord_tree.Tree) TSummery {
ts := TSummery{Tags: map[string]int{}, Categories: map[string]int{}}
readTree(tree, &ts, true)
return ts
}
func readTree(tree zord_tree.Tree, ts *TSummery, isRoot bool) {
for _, subtree := range tree.Dirs {
readTree(subtree, ts, false)
category := strings.Replace(subtree.Path, tree.Path, "", 1)
category = strings.Replace(category, "/", "", 1)
if isRoot {
ts.Categories[category]++
}
}
for _, file := range tree.Files {
ts.Files = append(ts.Files, FSummery{
Name: file.Name,
Category: file.Category,
Description: file.Meta["description"],
Id: file.Id,
Lang: file.Meta["lang"],
Created: file.Meta["created"],
Copyright: file.Meta["copyright"],
Tags: file.Tags,
})
for _, tag := range file.Tags {
ts.Tags[tag]++
}
}
}
func GetContent(file zord_tree.File) FileContent {
content, _ := zord_tree.ReadFileContent(file)
return FileContent{File: file, Content: content}
}