summaryrefslogtreecommitdiff
path: root/model/new.go
blob: f26bc8fd7a4af17aab86dcba0f074c4c2ec63e99 (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
package model

import (
	"io/fs"
	"path/filepath"
	"pik/identity"
	"strings"
)

func NewState(f fs.FS, locations []string, indexers []Indexer, runners []Runner) (*State, error) {
	st := &State{}
	for _, loc := range locations {
		_, dirName := filepath.Split(loc)
		src := &Source{
			Path:     loc,
			Identity: identity.New(dirName),
		}
		loc = strings.TrimSuffix(loc, "/")
		loc = strings.TrimPrefix(loc, "/")

		if loc == "" {
			continue
		}

		for _, indexer := range indexers {

			s, err := fs.Sub(f, loc)
			if err != nil {
				return nil, err
			}
			targets, err := indexer.Index("/"+loc, s, runners)
			if err != nil {
				return nil, err
			}
			src.Targets = append(src.Targets, targets...)
		}

		if src.Targets != nil {
			st.Sources = append(st.Sources, src)
		}

	}

	return st, nil
}