Skip to content

Commit

Permalink
extract method to reduce cyclomatic complexity
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Sep 29, 2023
1 parent fe8c278 commit 599e4b2
Showing 1 changed file with 36 additions and 29 deletions.
65 changes: 36 additions & 29 deletions pkg/remote/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,50 +108,57 @@ func (g ociRemoteLoader) Load(ctx context.Context, path string) (string, error)
local := filepath.Join(g.cache, descriptor.Digest.Hex())
composeFile := filepath.Join(local, "compose.yaml")
if _, err = os.Stat(local); os.IsNotExist(err) {

err = os.MkdirAll(local, 0o700)
var manifest v1.Manifest
err = json.Unmarshal(content, &manifest)

Check warning on line 112 in pkg/remote/oci.go

View check run for this annotation

Codecov / codecov/patch

pkg/remote/oci.go#L111-L112

Added lines #L111 - L112 were not covered by tests
if err != nil {
return "", err
}

f, err := os.Create(composeFile)
if err != nil {
return "", err
s, err2 := g.pullComposeFiles(ctx, local, composeFile, manifest, ref, resolver)
if err2 != nil {
return s, err2

Check warning on line 119 in pkg/remote/oci.go

View check run for this annotation

Codecov / codecov/patch

pkg/remote/oci.go#L117-L119

Added lines #L117 - L119 were not covered by tests
}
defer f.Close() //nolint:errcheck
}
return composeFile, nil

Check warning on line 122 in pkg/remote/oci.go

View check run for this annotation

Codecov / codecov/patch

pkg/remote/oci.go#L122

Added line #L122 was not covered by tests
}

var manifest v1.Manifest
err = json.Unmarshal(content, &manifest)
func (g ociRemoteLoader) pullComposeFiles(ctx context.Context, local string, composeFile string, manifest v1.Manifest, ref reference.Named, resolver *imagetools.Resolver) (string, error) {
err := os.MkdirAll(local, 0o700)
if err != nil {
return "", err
}

Check warning on line 129 in pkg/remote/oci.go

View check run for this annotation

Codecov / codecov/patch

pkg/remote/oci.go#L125-L129

Added lines #L125 - L129 were not covered by tests

f, err := os.Create(composeFile)
if err != nil {
return "", err
}
defer f.Close() //nolint:errcheck

if manifest.ArtifactType != "application/vnd.docker.compose.project" {
return "", fmt.Errorf("%s is not a compose project OCI artifact, but %s", ref.String(), manifest.ArtifactType)
}

Check warning on line 139 in pkg/remote/oci.go

View check run for this annotation

Codecov / codecov/patch

pkg/remote/oci.go#L131-L139

Added lines #L131 - L139 were not covered by tests

for i, layer := range manifest.Layers {
digested, err := reference.WithDigest(ref, layer.Digest)

Check warning on line 142 in pkg/remote/oci.go

View check run for this annotation

Codecov / codecov/patch

pkg/remote/oci.go#L141-L142

Added lines #L141 - L142 were not covered by tests
if err != nil {
return "", err
}

if manifest.ArtifactType != "application/vnd.docker.compose.project" {
return "", fmt.Errorf("%s is not a compose project OCI artifact, but %s", ref.String(), manifest.ArtifactType)
content, _, err := resolver.Get(ctx, digested.String())
if err != nil {
return "", err

Check warning on line 148 in pkg/remote/oci.go

View check run for this annotation

Codecov / codecov/patch

pkg/remote/oci.go#L146-L148

Added lines #L146 - L148 were not covered by tests
}

for i, layer := range manifest.Layers {
digested, err := reference.WithDigest(ref, layer.Digest)
if err != nil {
return "", err
}
content, _, err := resolver.Get(ctx, digested.String())
if err != nil {
return "", err
}
if i > 0 {
_, err = f.Write([]byte("\n---\n"))
if err != nil {
return "", err
}
}
_, err = f.Write(content)
if i > 0 {
_, err = f.Write([]byte("\n---\n"))

Check warning on line 151 in pkg/remote/oci.go

View check run for this annotation

Codecov / codecov/patch

pkg/remote/oci.go#L150-L151

Added lines #L150 - L151 were not covered by tests
if err != nil {
return "", err
}
}
_, err = f.Write(content)
if err != nil {
return "", err
}

Check warning on line 159 in pkg/remote/oci.go

View check run for this annotation

Codecov / codecov/patch

pkg/remote/oci.go#L156-L159

Added lines #L156 - L159 were not covered by tests
}
return composeFile, nil
return "", nil

Check warning on line 161 in pkg/remote/oci.go

View check run for this annotation

Codecov / codecov/patch

pkg/remote/oci.go#L161

Added line #L161 was not covered by tests
}

var _ loader.ResourceLoader = ociRemoteLoader{}

0 comments on commit 599e4b2

Please sign in to comment.