-
Notifications
You must be signed in to change notification settings - Fork 12
/
builtin_string.go
202 lines (166 loc) · 4.54 KB
/
builtin_string.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
201
202
package codegen
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os/exec"
"strings"
"text/template"
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/platforms"
"github.com/docker/distribution/reference"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/client/llb/sourceresolver"
specs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/openllb/hlb/errdefs"
"github.com/openllb/hlb/local"
"github.com/openllb/hlb/pkg/imageutil"
)
type Format struct{}
func (f Format) Call(ctx context.Context, cln *client.Client, val Value, opts Option, formatStr string, values ...string) (Value, error) {
var a []interface{}
for _, value := range values {
a = append(a, value)
}
return NewValue(ctx, fmt.Sprintf(formatStr, a...))
}
type Template struct{}
func (t Template) Call(ctx context.Context, cln *client.Client, val Value, opts Option, text string) (Value, error) {
tmpl, err := template.New("").Parse(text)
if err != nil {
return nil, err
}
data := map[string]interface{}{}
for _, opt := range opts {
switch o := opt.(type) {
case *TemplateField:
data[o.Name] = o.Value
}
}
buf := bytes.NewBufferString("")
err = tmpl.Execute(buf, data)
if err != nil {
return nil, err
}
return NewValue(ctx, buf.String())
}
type LocalArch struct{}
func (la LocalArch) Call(ctx context.Context, cln *client.Client, val Value, opts Option) (Value, error) {
return NewValue(ctx, local.Arch(ctx))
}
type LocalCwd struct{}
func (lc LocalCwd) Call(ctx context.Context, cln *client.Client, val Value, opts Option) (Value, error) {
cwd, err := local.Cwd(ctx)
if err != nil {
return nil, err
}
return NewValue(ctx, cwd)
}
type LocalOS struct{}
func (lo LocalOS) Call(ctx context.Context, cln *client.Client, val Value, opts Option) (Value, error) {
return NewValue(ctx, local.Os(ctx))
}
type LocalEnv struct{}
func (le LocalEnv) Call(ctx context.Context, cln *client.Client, val Value, opts Option, key string) (Value, error) {
return NewValue(ctx, local.Env(ctx, key))
}
type LocalRun struct{}
func (lr LocalRun) Call(ctx context.Context, cln *client.Client, val Value, opts Option, args ...string) (Value, error) {
var (
localRunOpts = &LocalRunOption{}
shlex = false
)
for _, opt := range opts {
switch o := opt.(type) {
case func(*LocalRunOption):
o(localRunOpts)
case *Shlex:
shlex = true
}
}
runArgs, err := ShlexArgs(args, shlex)
if err != nil {
return nil, err
}
cmd := exec.CommandContext(ctx, runArgs[0], runArgs[1:]...)
cmd.Env = local.Environ(ctx)
cmd.Dir = ModuleDir(ctx)
var buf strings.Builder
if localRunOpts.OnlyStderr {
cmd.Stderr = &buf
} else {
cmd.Stdout = &buf
}
if localRunOpts.IncludeStderr {
cmd.Stderr = &buf
}
err = cmd.Run()
if err != nil && !localRunOpts.IgnoreError {
return nil, err
}
return NewValue(ctx, strings.TrimRight(buf.String(), "\n"))
}
type Manifest struct{}
func (m Manifest) Call(ctx context.Context, cln *client.Client, val Value, opts Option, ref string) (Value, error) {
named, err := reference.ParseNormalizedNamed(ref)
if err != nil {
return nil, errdefs.WithInvalidImageRef(err, Arg(ctx, 0), ref)
}
ref = reference.TagNameOnly(named).String()
var (
resolver = imageutil.NewBufferedImageResolver()
matcher = resolver.MatchDefaultPlatform()
)
var platform *specs.Platform
for _, opt := range opts {
if p, ok := opt.(*specs.Platform); ok {
matcher = platforms.Only(*p)
platform = p
}
}
dgst, config, err := resolver.ResolveImageConfig(ctx, ref, sourceresolver.Opt{Platform: platform})
if err != nil {
return nil, err
}
if dgst == "" {
return nil, fmt.Errorf("no digest available for ref %q", ref)
}
desc, err := resolver.DigestDescriptor(ctx, dgst)
if err != nil {
return nil, err
}
switch Binding(ctx).Binds() {
case "digest":
return NewValue(ctx, dgst.String())
case "config":
return NewValue(ctx, string(config))
case "index":
switch desc.MediaType {
case images.MediaTypeDockerSchema2ManifestList,
specs.MediaTypeImageIndex:
ra, err := resolver.ReaderAt(ctx, desc)
if err != nil {
return nil, err
}
defer ra.Close()
dt := make([]byte, ra.Size())
_, err = ra.ReadAt(dt, 0)
if err != nil {
return nil, err
}
return NewValue(ctx, string(dt))
default:
return nil, Arg(ctx, 0).WithError(fmt.Errorf("has no manifest index"))
}
}
manifest, err := images.Manifest(ctx, resolver, desc, matcher)
if err != nil {
return nil, err
}
p, err := json.Marshal(manifest)
if err != nil {
return nil, err
}
return NewValue(ctx, string(p))
}