1 package artifactregistry
2
3 import (
4 "context"
5 "fmt"
6
7 gcpgar "cloud.google.com/go/artifactregistry/apiv1"
8 "cloud.google.com/go/artifactregistry/apiv1/artifactregistrypb"
9 )
10
11 type ArtifactRegistry struct {
12 garClient *gcpgar.Client
13 }
14
15 func NewArtifactRegistry(ctx context.Context) (*ArtifactRegistry, error) {
16 client, err := gcpgar.NewClient(ctx)
17 if err != nil {
18 return nil, err
19 }
20
21 ar := &ArtifactRegistry{
22 garClient: client,
23 }
24
25 return ar, nil
26 }
27
28 func (ar *ArtifactRegistry) ListTags(location, project, repository, pkg string) (*gcpgar.TagIterator, error) {
29 parentString := fmt.Sprintf(
30 "projects/%s/locations/%s/repositories/%s/packages/%s",
31 project,
32 location,
33 repository,
34 pkg,
35 )
36 getTagsReq := &artifactregistrypb.ListTagsRequest{
37 Parent: parentString,
38 PageSize: 1000,
39 }
40
41 ctx := context.Background()
42 return ar.garClient.ListTags(ctx, getTagsReq), nil
43 }
44
45 func (ar *ArtifactRegistry) GetTag(location, project, repository, pkg, tag string) (*artifactregistrypb.Tag, error) {
46 parentString := fmt.Sprintf(
47 "projects/%s/locations/%s/repositories/%s/packages/%s/tags/%s",
48 project,
49 location,
50 repository,
51 pkg,
52 tag,
53 )
54 getTagReq := &artifactregistrypb.GetTagRequest{
55 Name: parentString,
56 }
57
58 ctx := context.Background()
59 return ar.garClient.GetTag(ctx, getTagReq)
60 }
61
62 func (ar *ArtifactRegistry) GetVersion(location, project, repository, pkg, version string) (*artifactregistrypb.Version, error) {
63 parentString := fmt.Sprintf(
64 "projects/%s/locations/%s/repositories/%s/packages/%s/versions/%s",
65 project,
66 location,
67 repository,
68 pkg,
69 version,
70 )
71 getVersionReq := &artifactregistrypb.GetVersionRequest{
72 Name: parentString,
73 View: artifactregistrypb.VersionView_FULL,
74 }
75
76 ctx := context.Background()
77 return ar.garClient.GetVersion(ctx, getVersionReq)
78 }
79
80 func (ar *ArtifactRegistry) Close() error {
81 return ar.garClient.Close()
82 }
83
View as plain text