-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
99 lines (84 loc) · 1.91 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"fmt"
"sync"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
"github.com/jon4hz/gmotd/config"
"github.com/jon4hz/gmotd/context"
"github.com/jon4hz/gmotd/message"
"github.com/muesli/termenv"
)
func main() {
ctx := context.New()
defer ctx.Cancel()
for _, m := range message.Message {
d, ok := m.(message.Defaulter)
if !ok {
continue
}
d.Default(ctx)
}
err := config.Load(ctx.Config)
if err != nil {
log.Fatal(err)
}
// maybe make this a config option.
lipgloss.SetColorProfile(termenv.TrueColor)
var (
sectionResults = make(map[string]string)
wg sync.WaitGroup
doneC = make(chan struct{})
)
go func() {
for _, section := range message.Message {
if !section.Enabled(ctx) {
continue
}
wg.Add(1)
go func(section message.Section) {
defer wg.Done()
if err := section.Gather(ctx); err != nil {
log.Error("Section gather returned error!", "section", section.String(), "err", err)
return
}
if msg := section.Print(ctx); msg != "" {
sectionResults[section.String()] = msg + "\n"
}
}(section)
}
wg.Wait()
close(doneC)
}()
select {
case <-ctx.Done():
log.Fatal("Error: context expired")
return
case <-doneC:
}
var messages []string
printed := make(map[string]struct{})
// check if specific order is set
if len(ctx.Config.Order) > 0 {
for _, section := range ctx.Config.Order {
if result, ok := sectionResults[section]; ok {
messages = append(messages, result)
printed[section] = struct{}{}
}
}
}
// print the rest
for _, section := range message.Message {
if !section.Enabled(ctx) {
continue
}
// skip if already printed
if _, ok := printed[section.String()]; ok {
continue
}
if msg, ok := sectionResults[section.String()]; ok {
messages = append(messages, msg)
}
}
fmt.Println(lipgloss.JoinVertical(lipgloss.Left, messages...))
}