-
Notifications
You must be signed in to change notification settings - Fork 0
/
pallets-repositories.go
200 lines (182 loc) · 5.59 KB
/
pallets-repositories.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package cli
import (
"fmt"
"os"
"path"
"path/filepath"
"sort"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
"github.com/PlanktoScope/forklift/internal/app/forklift"
"github.com/PlanktoScope/forklift/pkg/core"
)
func GetRepoCache(
wpath string, pallet *forklift.FSPallet, ensureCache bool,
) (*forklift.LayeredRepoCache, *forklift.RepoOverrideCache, error) {
cache := &forklift.LayeredRepoCache{}
override, err := makeRepoOverrideCacheFromPallet(pallet)
if err != nil {
return nil, nil, err
}
cache.Overlay = override
workspace, err := forklift.LoadWorkspace(wpath)
if err != nil {
return nil, nil, err
}
fsCache, err := workspace.GetRepoCache()
if err != nil && override == nil {
return nil, nil, err
}
cache.Underlay = fsCache
if ensureCache && !fsCache.Exists() {
repoReqs, err := pallet.LoadFSRepoReqs("**")
if err != nil {
return nil, nil, errors.Wrap(err, "couldn't check whether the pallet requires any repos")
}
if len(repoReqs) > 0 {
return nil, nil, errors.New("you first need to cache the repos specified by your pallet")
}
}
return cache, override, nil
}
func makeRepoOverrideCacheFromPallet(
pallet *forklift.FSPallet,
) (*forklift.RepoOverrideCache, error) {
palletAsRepo, err := core.LoadFSRepo(pallet.FS, ".")
if err != nil {
// The common case is that the pallet is not a repo (and thus can't be loaded as one), so we
// mask the error:
return nil, nil
}
return forklift.NewRepoOverrideCache(
[]*core.FSRepo{palletAsRepo}, map[string][]string{
// In a pallet which is a repo, the implicit repo requirement is for an empty version string
palletAsRepo.Path(): {""},
},
)
}
// Print
func PrintPalletRepos(indent int, pallet *forklift.FSPallet) error {
loadedRepos, err := pallet.LoadFSRepoReqs("**")
if err != nil {
return errors.Wrapf(err, "couldn't identify repos")
}
sort.Slice(loadedRepos, func(i, j int) bool {
return forklift.CompareGitRepoReqs(
loadedRepos[i].RepoReq.GitRepoReq, loadedRepos[j].RepoReq.GitRepoReq,
) < 0
})
for _, repo := range loadedRepos {
IndentedPrintf(indent, "%s\n", repo.Path())
}
return nil
}
func PrintRepoInfo(
indent int, pallet *forklift.FSPallet, cache forklift.PathedRepoCache, repoPath string,
) error {
req, err := pallet.LoadFSRepoReq(repoPath)
if err != nil {
return errors.Wrapf(
err, "couldn't load repo version lock definition %s from pallet %s",
repoPath, pallet.FS.Path(),
)
}
printRepoReq(indent, req.RepoReq)
indent++
version := req.VersionLock.Version
cachedRepo, err := cache.LoadFSRepo(repoPath, version)
if err != nil {
return errors.Wrapf(
err, "couldn't find repo %s@%s in cache, please update the local cache of repos",
repoPath, version,
)
}
IndentedPrintf(indent, "Forklift version: %s\n", cachedRepo.Def.ForkliftVersion)
fmt.Println()
if core.CoversPath(cache, cachedRepo.FS.Path()) {
IndentedPrintf(
indent, "Path in cache: %s\n", core.GetSubdirPath(cache, cachedRepo.FS.Path()),
)
} else {
IndentedPrintf(indent, "Absolute path (replacing any cached copy): %s\n", cachedRepo.FS.Path())
}
IndentedPrintf(indent, "Description: %s\n", cachedRepo.Def.Repo.Description)
readme, err := cachedRepo.LoadReadme()
if err != nil {
return errors.Wrapf(
err, "couldn't load readme file for repo %s@%s from cache", repoPath, version,
)
}
IndentedPrintln(indent, "Readme:")
const widthLimit = 100
PrintReadme(indent+1, readme, widthLimit)
return nil
}
func printRepoReq(indent int, req forklift.RepoReq) {
IndentedPrintf(indent, "Repo: %s\n", req.Path())
indent++
IndentedPrintf(indent, "Locked repo version: %s\n", req.VersionLock.Version)
}
// Add
func AddRepoRequirements(
indent int, pallet *forklift.FSPallet, cachePath string, repoQueries []string,
) error {
if err := validateGitRepoQueries(repoQueries); err != nil {
return errors.Wrap(err, "one or more repo queries is invalid")
}
resolved, err := resolveQueriesUsingLocalMirrors(0, cachePath, repoQueries)
if err != nil {
return err
}
fmt.Println()
fmt.Printf("Saving configurations to %s...\n", pallet.FS.Path())
for _, repoQuery := range repoQueries {
req, ok := resolved[repoQuery]
if !ok {
return errors.Errorf("couldn't find configuration for %s", repoQuery)
}
reqsReposFS, err := pallet.GetRepoReqsFS()
if err != nil {
return err
}
repoReqPath := path.Join(reqsReposFS.Path(), req.Path(), forklift.VersionLockDefFile)
marshaled, err := yaml.Marshal(req.VersionLock.Def)
if err != nil {
return errors.Wrapf(err, "couldn't marshal repo requirement from %s", repoReqPath)
}
if err := forklift.EnsureExists(filepath.FromSlash(path.Dir(repoReqPath))); err != nil {
return errors.Wrapf(
err, "couldn't make directory %s", filepath.FromSlash(path.Dir(repoReqPath)),
)
}
const perm = 0o644 // owner rw, group r, public r
if err := os.WriteFile(filepath.FromSlash(repoReqPath), marshaled, perm); err != nil {
return errors.Wrapf(
err, "couldn't save repo requirement to %s", filepath.FromSlash(repoReqPath),
)
}
}
return nil
}
// Download
func DownloadRequiredRepos(
indent int, pallet *forklift.FSPallet, cachePath string,
) (changed bool, err error) {
loadedRepoReqs, err := pallet.LoadFSRepoReqs("**")
if err != nil {
return false, errors.Wrapf(err, "couldn't identify repos")
}
changed = false
for _, req := range loadedRepoReqs {
downloaded, err := DownloadLockedGitRepoUsingLocalMirror(
indent, cachePath, req.Path(), req.VersionLock,
)
changed = changed || downloaded
if err != nil {
return false, errors.Wrapf(
err, "couldn't download %s at commit %s", req.Path(), req.VersionLock.Def.ShortCommit(),
)
}
}
return changed, nil
}