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

import (
	"errors"
	"github.com/ewy1/pik/flags"
	"github.com/ewy1/pik/identity"
	"github.com/ewy1/pik/spool"
	"io/fs"
	"path/filepath"
	"strings"
	"sync"
)

func NewSource(rootFs fs.FS, loc string, indexers []Indexer, runners []Runner) (*Source, []error) {
	var errs []error
	_, dirName := filepath.Split(strings.TrimSuffix(loc, "/"))
	src := &Source{
		Path:       loc,
		Identity:   identity.New(dirName),
		Whitelists: make(map[string][]string),
	}
	loc = strings.TrimSuffix(loc, "/")
	loc = strings.TrimPrefix(loc, "/")

	if loc == "" {
		return nil, nil
	}

	locationWg := sync.WaitGroup{}
	var targets = make([][]Target, len(indexers), len(indexers))
	for ti, indexer := range indexers {
		locationWg.Go(func() {
			subFs, err := fs.Sub(rootFs, loc)
			if err != nil && !errors.Is(err, fs.ErrNotExist) {
				errs = append(errs, err)
				return
			}
			result, err := indexer.Index("/"+loc, subFs, runners)
			if err != nil && !errors.Is(err, fs.ErrNotExist) {
				errs = append(errs, err)
				return
			}
			targets[ti] = result
		})
	}
	locationWg.Wait()

	for _, t := range targets {
		if t == nil {
			continue
		}
		src.Targets = append(src.Targets, t...)
	}

	return src, errs
}

func NewState(rootFs fs.FS, locations []string, data map[string]*SourceData, indexers []Indexer, runners []Runner) (*State, []error) {
	var errs []error
	st := &State{
		All: *flags.All,
	}
	wg := sync.WaitGroup{}
	var sources = make([]*Source, len(locations), len(locations))
	for i, loc := range locations {
		wg.Go(func() {
			src, err := NewSource(rootFs, loc, indexers, runners)
			errs = append(errs, err...)
			for _, e := range err {
				_, _ = spool.Warn("%v\n", e)
			}
			sources[i] = src
		})

	}
	wg.Wait()

	for _, s := range sources {
		if s == nil || s.Targets == nil {
			continue
		}
		st.Sources = append(st.Sources, s)
	}

	err := Wants(rootFs, st, data, indexers, runners)
	if err != nil {
		errs = append(errs, err)
	}
	err = Include(rootFs, st, data, indexers, runners)
	if err != nil {
		errs = append(errs, err)
	}

	return st, errs
}