-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpull.go
More file actions
78 lines (65 loc) · 2.28 KB
/
Copy pathpull.go
File metadata and controls
78 lines (65 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package bundle
import (
"context"
"encoding/base64"
"fmt"
"io"
"path/filepath"
"xo/src/api"
"xo/src/massdriver"
"xo/src/telemetry"
"github.com/massdriver-cloud/massdriver-sdk-go/massdriver/client"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
oras "oras.land/oras-go/v2"
"oras.land/oras-go/v2/registry/remote"
"oras.land/oras-go/v2/registry/remote/auth"
"oras.land/oras-go/v2/registry/remote/retry"
)
func PullV0(ctx context.Context, client *massdriver.MassdriverClient, outFile io.Writer) error {
_, span := otel.Tracer("xo").Start(ctx, "BundlePull")
telemetry.SetSpanAttributes(span)
defer span.End()
bundleBytes, getErr := api.GetBundleSourceCode(client.GQLCLient, client.Specification.BundleID, client.Specification.OrganizationID)
if getErr != nil {
span.RecordError(getErr)
span.SetStatus(codes.Error, getErr.Error())
return fmt.Errorf("an error occurred while getting bundle source code: %w", getErr)
}
decodedBundleBytes, decodeErr := base64.StdEncoding.DecodeString(string(bundleBytes))
if decodeErr != nil {
span.RecordError(decodeErr)
span.SetStatus(codes.Error, decodeErr.Error())
return fmt.Errorf("an error occurred while decoding bundle source code: %w", decodeErr)
}
_, writeErr := outFile.Write(decodedBundleBytes)
if writeErr != nil {
span.RecordError(writeErr)
span.SetStatus(codes.Error, writeErr.Error())
return fmt.Errorf("an error occurred while writing bundle source code: %w", writeErr)
}
return nil
}
func PullV1(ctx context.Context, repo oras.Target, target oras.Target, tag string) (v1.Descriptor, error) {
_, span := otel.Tracer("xo").Start(ctx, "BundlePullV1")
telemetry.SetSpanAttributes(span)
defer span.End()
return oras.Copy(ctx, repo, tag, target, tag, oras.DefaultCopyOptions)
}
func GetRepo(mdClient *client.Client, organizationSlug string, bundleName string) (oras.Target, error) {
reg := mdClient.Auth.BaseURL
repo, repoErr := remote.NewRepository(filepath.Join(reg, organizationSlug, bundleName))
if repoErr != nil {
return nil, repoErr
}
repo.Client = &auth.Client{
Client: retry.DefaultClient,
Cache: auth.NewCache(),
Credential: auth.StaticCredential(reg, auth.Credential{
Username: "myuser",
Password: "mypass",
}),
}
return repo, nil
}