From e0c720681bbce364910df241658dc987851e579a Mon Sep 17 00:00:00 2001 From: Ophestra Date: Wed, 4 Feb 2026 14:41:18 +0900 Subject: internal/pkg: standardise artifact IR This should hopefully provide good separation between the artifact curing backend implementation and the (still work in progress) language. Making the IR parseable also guarantees uniqueness of the representation. Signed-off-by: Ophestra --- internal/pkg/net.go | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) (limited to 'internal/pkg/net.go') diff --git a/internal/pkg/net.go b/internal/pkg/net.go index b8fb8fb3..1f37a92d 100644 --- a/internal/pkg/net.go +++ b/internal/pkg/net.go @@ -19,8 +19,8 @@ type httpArtifact struct { // closing the [io.ReadCloser] returned by Cure. checksum unique.Handle[Checksum] - // doFunc is the Do method of [http.Client] supplied by the caller. - doFunc func(req *http.Request) (*http.Response, error) + // client is the address of the caller-supplied [http.Client]. + client *http.Client } var _ KnownChecksum = new(httpArtifact) @@ -33,10 +33,7 @@ func NewHTTPGet( url string, checksum Checksum, ) FileArtifact { - if c == nil { - c = http.DefaultClient - } - return &httpArtifact{url: url, checksum: unique.Make(checksum), doFunc: c.Do} + return &httpArtifact{url: url, checksum: unique.Make(checksum), client: c} } // Kind returns the hardcoded [Kind] constant. @@ -44,8 +41,17 @@ func (*httpArtifact) Kind() Kind { return KindHTTPGet } // Params writes the backing url string. Client is not represented as it does // not affect [Cache.Cure] outcome. -func (a *httpArtifact) Params(ctx *IContext) { - ctx.GetHash().Write([]byte(a.url)) +func (a *httpArtifact) Params(ctx *IContext) { ctx.WriteString(a.url) } + +func init() { + register(KindHTTPGet, func(r *IRReader) Artifact { + url := r.ReadString() + checksum, ok := r.Finalise() + if !ok { + panic(ErrExpectedChecksum) + } + return NewHTTPGet(nil, url, checksum.Value()) + }) } // Dependencies returns a nil slice. @@ -80,8 +86,13 @@ func (a *httpArtifact) Cure(r *RContext) (rc io.ReadCloser, err error) { } req.Header.Set("User-Agent", "Hakurei/1.1") + c := a.client + if c == nil { + c = http.DefaultClient + } + var resp *http.Response - if resp, err = a.doFunc(req); err != nil { + if resp, err = c.Do(req); err != nil { return } -- cgit v1.3.1