summaryrefslogtreecommitdiff
path: root/model/source.go
blob: 3a5fe10f034ad222a9eb374941af6b3736c332ba (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
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
package model

import (
	"pik/identity"
	"pik/paths"
	"pik/spool"
)

type Source struct {
	identity.Identity
	Tags
	Path    string
	Targets []Target
}

type HydratedSource struct {
	*Source
	HydratedTargets []HydratedTarget
	Aliases         []string
	Icon            string
}

func (s *Source) Label() string {
	return s.Identity.Full
}

func (s *HydratedSource) Label() string {
	if len(s.Aliases) > 0 {
		return s.Aliases[0]
	}
	return s.Identity.Full
}

func (s *Source) Hydrate(hydrators []Hydrator) *HydratedSource {
	hs := &HydratedSource{
		Source:          s,
		HydratedTargets: make([]HydratedTarget, 0, len(s.Targets)),
	}
	for _, h := range hydrators {
		err := h.Hydrate(s, hs)
		if err != nil {
			spool.Warn("%v", err)
		}
	}
	for _, t := range s.Targets {
		if !t.Visible() {
			continue
		}
		ht, err := t.Hydrate(s)
		if err != nil {
			spool.Warn("%v", err)
			continue
		}
		hs.HydratedTargets = append(hs.HydratedTargets, ht)
	}
	return hs
}

func (s *Source) ShortPath() string {
	return paths.ReplaceHome(s.Path)
}