Skip to content

Commit a55da35

Browse files
committed
Rework digest detection
Signed-off-by: Matej Vašek <[email protected]>
1 parent 1d253cb commit a55da35

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

hack/update-builder.go

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -189,43 +189,45 @@ func buildBuilderImage(ctx context.Context, variant, arch string) (string, error
189189
}(rc)
190190

191191
pr, pw := io.Pipe()
192-
digestCh := make(chan string, 1)
192+
r := io.TeeReader(rc, pw)
193+
193194
go func() {
194-
var (
195-
jm jsonmessage.JSONMessage
196-
dec = json.NewDecoder(pr)
197-
err error
198-
)
199-
for {
200-
err = dec.Decode(&jm)
201-
if err != nil {
202-
if errors.Is(err, io.EOF) {
203-
break
204-
}
205-
panic(err)
206-
}
207-
if jm.Error != nil {
208-
continue
209-
}
195+
fd := os.Stdout.Fd()
196+
isTerminal := term.IsTerminal(int(os.Stdout.Fd()))
197+
e := jsonmessage.DisplayJSONMessagesStream(pr, os.Stderr, fd, isTerminal, nil)
198+
_ = pr.CloseWithError(e)
199+
}()
210200

211-
re := regexp.MustCompile(`\sdigest: (?P<hash>sha256:[a-zA-Z0-9]+)\s`)
212-
matches := re.FindStringSubmatch(jm.Status)
213-
if len(matches) == 2 {
214-
digestCh <- matches[1]
201+
var (
202+
digest string
203+
jm jsonmessage.JSONMessage
204+
dec = json.NewDecoder(r)
205+
re = regexp.MustCompile(`\sdigest: (?P<hash>sha256:[a-zA-Z0-9]+)\s`)
206+
)
207+
for {
208+
err = dec.Decode(&jm)
209+
if err != nil {
210+
if errors.Is(err, io.EOF) {
211+
break
215212
}
213+
return "", err
214+
}
215+
if jm.Error != nil {
216+
continue
216217
}
217-
}()
218-
r := io.TeeReader(rc, pw)
219218

220-
fd := os.Stdout.Fd()
221-
isTerminal := term.IsTerminal(int(os.Stdout.Fd()))
222-
err = jsonmessage.DisplayJSONMessagesStream(r, os.Stderr, fd, isTerminal, nil)
223-
_ = pw.Close()
224-
if err != nil {
225-
return "", err
219+
matches := re.FindStringSubmatch(jm.Status)
220+
if len(matches) == 2 {
221+
digest = matches[1]
222+
_, _ = io.Copy(io.Discard, r)
223+
break
224+
}
226225
}
227226

228-
return <-digestCh, nil
227+
if digest == "" {
228+
return "", fmt.Errorf("digest not found")
229+
}
230+
return digest, nil
229231
}
230232

231233
var d string

0 commit comments

Comments
 (0)