-
Notifications
You must be signed in to change notification settings - Fork 9
/
forcecache.go
52 lines (38 loc) · 1.24 KB
/
forcecache.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
// 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 oci
import (
"context"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/std/tasks"
)
func LocalCopy(input NamedImage) NamedImage {
return MakeNamedImage(input.Description(), &forceCache{desc: input.Description(), source: input.Image()})
}
type forceCache struct {
desc string
source compute.Computable[Image]
compute.LocalScoped[Image]
}
var _ compute.Computable[Image] = &forceCache{}
func (fc *forceCache) Action() *tasks.ActionEvent {
return tasks.Action("oci.cache-image").Arg("ref", fc.desc)
}
func (fc *forceCache) Inputs() *compute.In {
return compute.Inputs().Computable("source", fc.source)
}
func (fc *forceCache) Compute(ctx context.Context, deps compute.Resolved) (Image, error) {
source := compute.MustGetDepValue(deps, fc.source, "source")
cache := compute.Cache(ctx)
var ic imageCacheable
digest, err := ic.Cache(ctx, cache, source)
if err != nil {
return nil, err
}
result, err := ic.LoadCached(ctx, cache, nil, digest)
if err != nil {
return nil, err
}
return result.Value, nil
}