Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
Signed-off-by: ashpect <[email protected]>
  • Loading branch information
ashpect committed Mar 3, 2024
1 parent ba64ecc commit d0d653f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 30 deletions.
7 changes: 6 additions & 1 deletion pkg/imgpkg/cmd/copy_repo_src.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package cmd

import (
"fmt"
"io/ioutil"
"os"

regname "github.com/google/go-containerregistry/pkg/name"
Expand Down Expand Up @@ -76,7 +77,11 @@ func (c CopyRepoSrc) CopyToRepo(repo string) (*ctlimgset.ProcessedImages, error)
}

if c.OciFlags.IsOci() {
tempDir, err = image.ExtractOciTarGz(c.OciFlags.OcitoReg)
tempDir, err := ioutil.TempDir("", "imgpkg-oci-extract-")
if err != nil {
return nil, err
}
err = image.ExtractOciTarGz(c.OciFlags.OcitoReg, tempDir)
if err != nil {
return nil, err
}
Expand Down
40 changes: 13 additions & 27 deletions pkg/imgpkg/image/tar_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -243,69 +242,56 @@ func CreateOciTarFileAndDeleteFolder(source, target string) error {
return nil
}

func ExtractOciTarGz(tarGzFilePath string) (string, error) {
// Create a temporary directory
tempDir, err := ioutil.TempDir("", "imgpkg-oci-extract-")
if err != nil {
return "", err
func ExtractOciTarGz(inputDir, extractDir string) error {

if !strings.HasSuffix(inputDir, ".tar.gz") {
return fmt.Errorf("inputDir '%s' is not a tar.gz file", inputDir)
}
//defer os.RemoveAll(tempDir) // Clean up the temporary directory when done

// Open the tar.gz file
tarGzFile, err := os.Open(tarGzFilePath)
tarGzFile, err := os.Open(inputDir)
if err != nil {
return "", err
return err
}
defer tarGzFile.Close()

// Create a gzip reader
gzipReader, err := gzip.NewReader(tarGzFile)
if err != nil {
return "", err
return err
}
defer gzipReader.Close()

// Create a tar reader
tarReader := tar.NewReader(gzipReader)

// Extract files to the temporary directory
for {
header, err := tarReader.Next()

if err == io.EOF {
break // End of archive
}

if err != nil {
return "", err
return err
}
targetPath := filepath.Join(extractDir, header.Name)

// Construct the full path to the file in the temporary directory
targetPath := filepath.Join(tempDir, header.Name)

// Check if the file is a directory
if header.FileInfo().IsDir() {
// Create the directory
err := os.MkdirAll(targetPath, os.ModePerm)
if err != nil {
return "", err
return err
}
continue
}

// Create the file
file, err := os.Create(targetPath)
if err != nil {
return "", err
return err
}
defer file.Close()

// Copy the file contents
_, err = io.Copy(file, tarReader)
if err != nil {
return "", err
return err
}
}

return tempDir, nil
return nil
}
3 changes: 1 addition & 2 deletions pkg/imgpkg/imagetar/tar_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ func (r TarReader) ReadOci(reponame string) ([]imagedesc.ImageOrIndex, error) {
}

if !stat.IsDir() {
//give error "not a directory"
return nil, err
return nil, fmt.Errorf("path %s is not a directory", r.path)
}

//TODO : FromPath checks for index.json but does not check for oci-layout, so add a check for oci-layout here.
Expand Down

0 comments on commit d0d653f

Please sign in to comment.