This repository was archived by the owner on Jul 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate.go
More file actions
128 lines (111 loc) · 2.96 KB
/
generate.go
File metadata and controls
128 lines (111 loc) · 2.96 KB
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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"regexp"
"sort"
"strings"
"github.com/moby/buildkit/frontend/dockerfile/dockerfile2llb"
)
type Source struct {
Type string `json:"type"`
Ref string `json:"ref"`
Replace string `json:"replace,omitempty"`
}
type Result struct {
Sources []Source `json:"sources"`
}
func Generate(ctx context.Context, dt []byte, buildArgs map[string]string) (Result, error) {
targets, err := dockerfile2llb.ListTargets(context.TODO(), dt)
if err != nil {
return Result{}, fmt.Errorf("error listing dockerfile targets: %w", err)
}
r := newResolver()
for _, target := range targets.Targets {
_, err = dockerfile2llb.Dockefile2Outline(ctx, dt, dockerfile2llb.ConvertOpt{
BuildArgs: func() map[string]string {
if len(buildArgs) > 0 {
return buildArgs
}
return nil
}(),
Target: target.Name,
MetaResolver: r,
})
if err != nil {
return Result{}, fmt.Errorf("error parsing dockerfile: %w", err)
}
}
var result Result
buf := bytes.NewBuffer(nil)
stderr := bytes.NewBuffer(nil)
type matchRule struct {
Match string `json:"match"`
Replace string `json:"replace"`
regex *regexp.Regexp
}
var matchers []matchRule
if modProg == "" && modConfig != "" {
data, err := os.ReadFile(modConfig)
if err != nil {
return Result{}, fmt.Errorf("error reading mod config: %w", err)
}
if len(data) > 0 {
if err := json.Unmarshal(data, &matchers); err != nil {
return Result{}, fmt.Errorf("error parsing mod config for builtin matcher: %w", err)
}
for i, v := range matchers {
matchers[i].regex, err = regexp.Compile(v.Match)
if err != nil {
return Result{}, fmt.Errorf("error compiling matcher regex from mod config: %w", err)
}
}
}
}
replace := func(ref string) string {
if modProg == "" {
for _, rule := range matchers {
if !rule.regex.MatchString(ref) {
continue
}
return rule.regex.ReplaceAllString(ref, rule.Replace)
}
return ""
}
buf.Reset()
stderr.Reset()
cmdWithArgs := strings.Fields(modProg)
cmdWithArgs = append(cmdWithArgs, ref)
cmd := exec.CommandContext(ctx, cmdWithArgs[0], cmdWithArgs[1:]...)
cmd.Stdout = buf
cmd.Stderr = stderr
if modConfig != "" {
cmd.Env = append(os.Environ(), "MOD_CONFIG="+modConfig)
}
if err := cmd.Run(); err != nil {
if stderr.Len() == 0 {
stderr.WriteString("<no output from program>")
}
panic(fmt.Sprintf("%s: %v", stderr, err))
}
if stderr.Len() > 0 {
io.Copy(os.Stderr, stderr)
}
return strings.TrimSpace(buf.String())
}
for _, resolved := range r.refs {
s := Source{Type: "docker-image", Ref: resolved, Replace: replace(resolved)}
debug("resolved", s.Ref, "with replacement:", s.Replace)
result.Sources = append(result.Sources, s)
}
// Sort for stable output for testing
sort.Slice(result.Sources, func(i, j int) bool {
return result.Sources[i].Ref < result.Sources[j].Ref
})
return result, nil
}