summaryrefslogtreecommitdiff
path: root/internal/app/app.go
blob: 4544f0f46a84ed37e2d3f13f3cb0200e8ce3af04 (plain)
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
package app

import (
	"context"
	"sync"

	"codeberg.org/snonux/loadbars/internal/collector"
	"codeberg.org/snonux/loadbars/internal/config"
	"codeberg.org/snonux/loadbars/internal/display"
)

// Run starts the loadbars application: collectors and display.
// It blocks until the user quits (e.g. 'q' key).
func Run(cfg *config.Config) error {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	scriptPath := ScriptPath()
	store := NewStore()

	var wg sync.WaitGroup
	for _, host := range cfg.Hosts {
		h := host
		wg.Add(1)
		go func() {
			defer wg.Done()
			_ = collector.Run(ctx, h, cfg, store, scriptPath)
		}()
	}

	err := display.Run(ctx, cfg, store)
	cancel()
	wg.Wait()
	return err
}