From 599e4b242a851d61bfe5cb8100329e93ff06417d Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Fri, 29 Sep 2023 10:18:11 +0200 Subject: [PATCH] extract method to reduce cyclomatic complexity Signed-off-by: Nicolas De Loof --- pkg/remote/oci.go | 65 ++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/pkg/remote/oci.go b/pkg/remote/oci.go index 4bb1b713ce0..2a7e622e256 100644 --- a/pkg/remote/oci.go +++ b/pkg/remote/oci.go @@ -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) 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 } - defer f.Close() //nolint:errcheck + } + return composeFile, nil +} - 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 + } + + 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) + } + + for i, layer := range manifest.Layers { + digested, err := reference.WithDigest(ref, layer.Digest) 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 } - - 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")) if err != nil { return "", err } } + _, err = f.Write(content) + if err != nil { + return "", err + } } - return composeFile, nil + return "", nil } var _ loader.ResourceLoader = ociRemoteLoader{}