-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
67 lines (61 loc) · 1.82 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"fmt"
"io"
"log"
"os"
"github.com/crossbone-magister/tag-tree/config"
"github.com/crossbone-magister/tag-tree/data"
"github.com/crossbone-magister/tag-tree/logic"
"github.com/crossbone-magister/tag-tree/output"
"github.com/crossbone-magister/timewlib"
)
const c_HIERARCHY_PRIORITY = 0
const c_INTERVAL_PRIORITY = 1
func main() {
var err error
if raw, err := timewlib.Parse(os.Stdin); err == nil {
handleLogging(raw.Configuration)
hierarchy := config.ExtractInitialHierarchy(raw.Configuration)
if intervals, err := timewlib.Process(raw.Intervals); err == nil {
root := data.NewNode("root")
log.Println("---HIERARCHY CREATION START---")
for _, tags := range hierarchy {
logic.AddIntervalToTree(root, *timewlib.NewInterval(0, 0, 0, 0), tags, c_HIERARCHY_PRIORITY)
}
log.Println("---HIERARCHY CREATION END---")
log.Println("---INTERVALS ADDITION START---")
for _, interval := range intervals {
logic.AddIntervalToTree(root, interval, interval.Tags, c_INTERVAL_PRIORITY)
}
log.Println("---INTERVALS ADDITION END---")
logic.CalculateTreeTotal(root)
if maxDepth, err := config.RetrieveMaxDepth(raw.Configuration); err == nil {
logic.PruneToMaxDepth(root, maxDepth)
} else {
switch err.(type) {
case *config.ConfigurationKeyNotFound:
log.Println(err)
default:
fmt.Fprintf(os.Stderr, "Error reading max depth configuration: %s\n", err)
}
}
if _, ok := raw.Configuration[config.PRUNE_CONFIG_KEY]; ok {
logic.PruneEmptyBranches(root)
}
output.PrintTree(root, os.Stdout)
}
}
if err != nil {
printErrorAndExit(err)
}
}
func handleLogging(rawConfig map[string]string) {
if !config.IsDebugEnabled(rawConfig) {
log.SetOutput(io.Discard)
}
}
func printErrorAndExit(err error) {
fmt.Printf("Error while parsing: %s\n", err)
os.Exit(1)
}