shad-go/distbuild/pkg/artifact/client.go

43 lines
899 B
Go
Raw Normal View History

2020-04-04 17:16:36 +00:00
package artifact
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"gitlab.com/slon/shad-go/distbuild/pkg/build"
"gitlab.com/slon/shad-go/distbuild/pkg/tarstream"
)
// Download artifact from remote cache into local cache.
func Download(ctx context.Context, endpoint string, c *Cache, artifactID build.ID) error {
dir, commit, abort, err := c.Create(artifactID)
if err != nil {
return err
}
2020-04-04 21:55:07 +00:00
defer func() { _ = abort() }()
2020-04-04 17:16:36 +00:00
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint+"/artifact?id="+artifactID.String(), nil)
if err != nil {
return err
}
rsp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer rsp.Body.Close()
if rsp.StatusCode != http.StatusOK {
errStr, _ := ioutil.ReadAll(rsp.Body)
return fmt.Errorf("download: %s", errStr)
}
if err := tarstream.Receive(dir, rsp.Body); err != nil {
return err
}
return commit()
}