-
Notifications
You must be signed in to change notification settings - Fork 9
/
validatefoundation.go
55 lines (44 loc) · 1.59 KB
/
validatefoundation.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package pkggraph
import (
"context"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/versions"
"namespacelabs.dev/foundation/schema"
)
type LoadModuleFunc func(schema.PackageName) (*Module, error)
func ValidateFoundation(what string, minimumVersion int, lmf LoadModuleFunc) error {
fn, err := lmf("namespacelabs.dev/foundation")
if err != nil {
return fnerrors.InternalError("failed to determine minimum foundation version: %w", err)
}
data, err := versions.LoadAtOrDefaults(fn.ReadOnlyFS(), "internal/versions/versions.json")
if err != nil {
return fnerrors.InternalError("failed to load foundation version data: %w", err)
}
if data.APIVersion < minimumVersion {
return fnerrors.NamespaceTooRecent(what, int32(minimumVersion), int32(data.APIVersion))
}
return nil
}
func ModuleFromLoader(ctx context.Context, pl PackageLoader) LoadModuleFunc {
return func(pn schema.PackageName) (*Module, error) {
fn, err := pl.Resolve(ctx, pn)
if err != nil {
return nil, fnerrors.InternalError("failed to load module %q: %w", pn, err)
}
return fn.Module, nil
}
}
func ModuleFromModules(mods Modules) LoadModuleFunc {
return func(pn schema.PackageName) (*Module, error) {
for _, mod := range mods.Modules() {
if mod.ModuleName() == pn.String() {
return mod, nil
}
}
return nil, fnerrors.InternalError("failed to load module: %q: not loaded", pn)
}
}