Skip to content

Commit 16b8745

Browse files
loresusopoiana
authored andcommitted
chore: address minor comments
Signed-off-by: Lorenzo Susini <[email protected]>
1 parent a0cfff1 commit 16b8745

File tree

4 files changed

+29
-26
lines changed

4 files changed

+29
-26
lines changed

internal/artifact/install/deps.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,19 @@ var (
3333
)
3434

3535
type depInfo struct {
36+
// ref is the remote reference to this artifact
3637
ref string
38+
// res contains the config layer for this artifact
3739
res *oci.RegistryResult
40+
// ver represents the semver version of this artifact
3841
ver *semver.Version
39-
ok bool
42+
// ok is used to mark this dependency as fully processed, with its own
43+
// dependencies and alternatives
44+
ok bool
4045
}
4146

4247
func copyDepsMap(in depsMapType) (out depsMapType) {
43-
out = make(depsMapType)
48+
out = make(depsMapType, len(in))
4449
for k, v := range in {
4550
out[k] = v
4651
}
@@ -91,13 +96,15 @@ func ResolveDeps(resolver artifactConfigResolver, inRefs ...string) (outRefs []s
9196

9297
for {
9398
allOk := true
99+
// Since we are updating depMap in this for loop, let's copy the map for iterating it
100+
// while we continue inserting new values in the real depMap map.
94101
for name, info := range copyDepsMap(depMap) {
95102
if info.ok {
96103
continue
97104
}
98105
for _, required := range info.res.Config.Dependencies {
99106
// Does already exist in the map?
100-
if existing, exists := depMap[required.Name]; exists {
107+
if existing, ok := depMap[required.Name]; ok {
101108
requiredVer, err := semver.Parse(required.Version)
102109
if err != nil {
103110
return nil, fmt.Errorf(`invalid artifact config: version %q is not semver compatible`, required.Version)
@@ -120,8 +127,8 @@ func ResolveDeps(resolver artifactConfigResolver, inRefs ...string) (outRefs []s
120127
// Are alternatives already in the map?
121128
var foundAlternative = false
122129
for _, alternative := range required.Alternatives {
123-
existing, exists := depMap[alternative.Name]
124-
if !exists {
130+
existing, ok := depMap[alternative.Name]
131+
if !ok {
125132
continue
126133
}
127134

internal/artifact/install/install.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ package install
1616

1717
import (
1818
"context"
19-
"encoding/json"
2019
"fmt"
21-
"io"
2220
"os"
2321
"path/filepath"
2422
"runtime"
@@ -107,23 +105,13 @@ func (o *artifactInstallOptions) RunArtifactInstall(ctx context.Context, args []
107105
return nil, err
108106
}
109107

110-
configReader, err := puller.PullConfigLayer(ctx, ref)
108+
artifactConfig, err := puller.PullConfigLayer(ctx, ref)
111109
if err != nil {
112110
return nil, err
113111
}
114112

115-
configBytes, err := io.ReadAll(configReader)
116-
if err != nil {
117-
return nil, err
118-
}
119-
120-
var artifactConfig oci.ArtifactConfig
121-
if err = json.Unmarshal(configBytes, &artifactConfig); err != nil {
122-
return nil, err
123-
}
124-
125113
return &oci.RegistryResult{
126-
Config: artifactConfig,
114+
Config: *artifactConfig,
127115
}, nil
128116
})
129117

pkg/oci/puller/puller.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func manifestFromDesc(ctx context.Context, target oras.Target, desc *v1.Descript
157157
}
158158

159159
// PullConfigLayer fetches only the config layer from a given ref.
160-
func (p *Puller) PullConfigLayer(ctx context.Context, ref string) (io.Reader, error) {
160+
func (p *Puller) PullConfigLayer(ctx context.Context, ref string) (*oci.ArtifactConfig, error) {
161161
repo, err := repository.NewRepository(ref, repository.WithClient(p.Client))
162162
if err != nil {
163163
return nil, err
@@ -172,10 +172,8 @@ func (p *Puller) PullConfigLayer(ctx context.Context, ref string) (io.Reader, er
172172
// Resolve to actual manifest if an index is found.
173173
if desc.MediaType == v1.MediaTypeImageIndex {
174174
var index v1.Index
175-
_, indexReader, err := repo.FetchReference(ctx, ref)
176-
if err != nil {
177-
return nil, fmt.Errorf("unable to fetch index for ref %q", ref)
178-
}
175+
indexReader := manifestReader
176+
defer indexReader.Close()
179177

180178
indexBytes, err := io.ReadAll(indexReader)
181179
if err != nil {
@@ -228,5 +226,15 @@ func (p *Puller) PullConfigLayer(ctx context.Context, ref string) (io.Reader, er
228226
return nil, fmt.Errorf("unable to fetch descriptor with digest: %s", desc.Digest.String())
229227
}
230228

231-
return rc, nil
229+
configBytes, err := io.ReadAll(rc)
230+
if err != nil {
231+
return nil, err
232+
}
233+
234+
var artifactConfig oci.ArtifactConfig
235+
if err = json.Unmarshal(configBytes, &artifactConfig); err != nil {
236+
return nil, err
237+
}
238+
239+
return &artifactConfig, nil
232240
}

pkg/oci/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func (a *ArtifactDependency) SetAlternative(name, version string) {
152152
// SetDependency stores an artifact dependency in the config.
153153
//
154154
// Return the insertion position.
155-
func (rc *ArtifactConfig) SetDependency(name, version string, alternatives []dependency) int {
155+
func (rc *ArtifactConfig) SetDependency(name, version string, alternatives []Dependency) int {
156156
for i, d := range rc.Dependencies {
157157
if d.Name == name {
158158
rc.Dependencies[i].Version = version

0 commit comments

Comments
 (0)