Skip to content

Commit aedee8f

Browse files
alacukupoiana
authored andcommitted
update(internal/utils): return full path of the extracted files in ExtractTarGz func
Signed-off-by: Aldo Lacuku <[email protected]>
1 parent 19a4ab7 commit aedee8f

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

internal/artifact/install/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func (o *artifactInstallOptions) RunArtifactInstall(ctx context.Context, args []
127127
}
128128

129129
// Extract artifact and move it to its destination directory
130-
err = utils.ExtractTarGz(f, destDir)
130+
_, err = utils.ExtractTarGz(f, destDir)
131131
if err != nil {
132132
return fmt.Errorf("cannot extract %q to %q: %w", result.Filename, destDir, err)
133133
}

internal/utils/extract.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ import (
2525
)
2626

2727
// ExtractTarGz extracts a *.tar.gz compressed archive and moves its content to destDir.
28-
func ExtractTarGz(gzipStream io.Reader, destDir string) error {
28+
// Returns a slice containing the full path of the extracted files.
29+
func ExtractTarGz(gzipStream io.Reader, destDir string) ([]string, error) {
30+
var files []string
31+
2932
uncompressedStream, err := gzip.NewReader(gzipStream)
3033
if err != nil {
31-
return err
34+
return nil, err
3235
}
3336

3437
tarReader := tar.NewReader(uncompressedStream)
@@ -41,30 +44,32 @@ func ExtractTarGz(gzipStream io.Reader, destDir string) error {
4144
}
4245

4346
if err != nil {
44-
return err
47+
return nil, err
4548
}
4649

4750
switch header.Typeflag {
4851
case tar.TypeDir:
49-
return fmt.Errorf("unexepected dir inside the archive, expected to find only files without any tree structure")
52+
return nil, fmt.Errorf("unexepected dir inside the archive, expected to find only files without any tree structure")
5053
case tar.TypeReg:
51-
outFile, err := os.Create(filepath.Clean(filepath.Join(destDir, filepath.Clean(header.Name))))
54+
f := filepath.Join(destDir, filepath.Clean(header.Name))
55+
outFile, err := os.Create(filepath.Clean(f))
5256
if err != nil {
53-
return err
57+
return nil, err
5458
}
5559
if err = copyInChunks(outFile, tarReader); err != nil {
56-
return err
60+
return nil, err
5761
}
5862
if err = outFile.Close(); err != nil {
59-
return err
63+
return nil, err
6064
}
65+
files = append(files, f)
6166

6267
default:
63-
return fmt.Errorf("extractTarGz: uknown type: %b in %s", header.Typeflag, header.Name)
68+
return nil, fmt.Errorf("extractTarGz: uknown type: %b in %s", header.Typeflag, header.Name)
6469
}
6570
}
6671

67-
return nil
72+
return files, nil
6873
}
6974

7075
func copyInChunks(dst io.Writer, src io.Reader) error {

0 commit comments

Comments
 (0)