internal/pkg: expose underlying reader
All checks were successful
Test / Create distribution (push) Successful in 49s
Test / Sandbox (push) Successful in 2m42s
Test / ShareFS (push) Successful in 3m57s
Test / Hpkg (push) Successful in 4m37s
Test / Sandbox (race detector) (push) Successful in 5m0s
Test / Hakurei (race detector) (push) Successful in 5m54s
Test / Hakurei (push) Successful in 2m41s
Test / Flake checks (push) Successful in 1m41s

This will be fully implemented in httpArtifact in a future commit.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
2026-01-25 14:14:19 +09:00
parent 20790af71e
commit 334578fdde
8 changed files with 157 additions and 93 deletions

View File

@@ -1,6 +1,7 @@
package pkg
import (
"bytes"
"context"
"crypto/sha512"
"fmt"
@@ -34,13 +35,13 @@ type httpArtifact struct {
var _ KnownChecksum = new(httpArtifact)
var _ fmt.Stringer = new(httpArtifact)
// NewHTTPGet returns a new [File] backed by the supplied client. A GET request
// is set up for url. If c is nil, [http.DefaultClient] is used instead.
// NewHTTPGet returns a new [FileArtifact] backed by the supplied client. A GET
// request is set up for url. If c is nil, [http.DefaultClient] is used instead.
func NewHTTPGet(
c *http.Client,
url string,
checksum Checksum,
) File {
) FileArtifact {
if c == nil {
c = http.DefaultClient
}
@@ -103,15 +104,16 @@ func (a *httpArtifact) do(ctx context.Context) (data []byte, err error) {
// Cure completes the http request and returns the resulting response body read
// to EOF. Data does not interact with the filesystem.
func (a *httpArtifact) Cure(ctx context.Context) (data []byte, err error) {
func (a *httpArtifact) Cure(ctx context.Context) (r io.ReadCloser, err error) {
a.mu.Lock()
defer a.mu.Unlock()
if a.data != nil {
// validated by cache or a previous call to Data
return a.data, nil
// validated by cache or a previous call to Cure
return io.NopCloser(bytes.NewReader(a.data)), nil
}
var data []byte
if data, err = a.do(ctx); err != nil {
return
}
@@ -122,5 +124,6 @@ func (a *httpArtifact) Cure(ctx context.Context) (data []byte, err error) {
return nil, &ChecksumMismatchError{got, a.checksum}
}
a.data = data
r = io.NopCloser(bytes.NewReader(data))
return
}