From 4c002f3d54c671d3b1a944bf14df4279abdba705 Mon Sep 17 00:00:00 2001 From: Achilleas Koutsou Date: Wed, 16 Aug 2023 16:04:47 +0200 Subject: [PATCH] upload/pulp: file upload method --- internal/upload/pulp/pulp.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/internal/upload/pulp/pulp.go b/internal/upload/pulp/pulp.go index b395a34fa..90541e6b0 100644 --- a/internal/upload/pulp/pulp.go +++ b/internal/upload/pulp/pulp.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "net/http" + "os" "github.com/osbuild/pulp-client/pulpclient" ) @@ -57,6 +58,23 @@ func readBody(r *http.Response) string { return string(b) } +// UploadFile uploads the file at the given path and returns the href of the +// new artifact. +func (cl *Client) UploadFile(path string) (string, error) { + fp, err := os.Open(path) + if err != nil { + return "", err + } + defer fp.Close() + create := cl.client.ArtifactsAPI.ArtifactsCreate(cl.ctx).File(fp) + res, resp, err := create.Execute() + if err != nil { + return "", fmt.Errorf("failed to upload file %q: %s (%s)", path, err.Error(), readBody(resp)) + } + + return res.GetPulpHref(), nil +} + // ListOSTreeRepositories returns a map (repository name -> pulp href) of // existing ostree repositories. func (cl *Client) ListOSTreeRepositories() (map[string]string, error) {