/
local_vendir.go
59 lines (49 loc) · 1.29 KB
/
local_vendir.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
// Copyright 2024 The Carvel Authors.
// SPDX-License-Identifier: Apache-2.0
package local
import (
"os"
"path/filepath"
"strconv"
"strings"
vendirconf "github.com/vmware-tanzu/carvel-vendir/pkg/vendir/config"
)
type localVendirConf struct {
// Indexed by numeric fetch index
localPaths map[int]string
}
func newLocalVendirConf(resAnnotations map[string]string) (localVendirConf, error) {
cwdPath, err := os.Getwd()
if err != nil {
return localVendirConf{}, err
}
const (
prefix = "kctrl.carvel.dev/local-fetch-"
)
localPaths := map[int]string{}
for key, val := range resAnnotations {
if strings.HasPrefix(key, prefix) {
fetchIdx, err := strconv.Atoi(strings.TrimPrefix(key, prefix))
if err != nil {
return localVendirConf{}, err
}
localPaths[fetchIdx] = filepath.Join(cwdPath, val)
}
}
return localVendirConf{localPaths}, nil
}
func (c localVendirConf) Adjust(conf vendirconf.Config) vendirconf.Config {
for fetchIdx, localPath := range c.localPaths {
if fetchIdx >= len(conf.Directories) {
// Ignore invalid indexes
continue
}
conf.Directories[fetchIdx].Contents[0] = vendirconf.DirectoryContents{
Path: conf.Directories[fetchIdx].Contents[0].Path,
Directory: &vendirconf.DirectoryContentsDirectory{
Path: localPath,
},
}
}
return conf
}