Add basic compilation functionality

This commit is contained in:
2025-04-22 17:04:47 +03:00
parent 7b0a8bf1d6
commit e8d5f0a565
3 changed files with 298 additions and 0 deletions
+64
View File
@@ -5,6 +5,8 @@ import (
"errors"
"io"
"os"
"path"
"strings"
)
type tarballFileReader struct {
@@ -41,3 +43,65 @@ func readTarballContent(tarballPath, fileToExtract string) (*tarballFileReader,
return nil, errors.New("could not file in tarball")
}
func extractTarballDirectory(tarballPath, directoryToExtract, workingDirectory string) (err error) {
file, err := os.Open(tarballPath)
if err != nil {
return err
}
defer file.Close()
tr := tar.NewReader(file)
for {
header, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
if strings.HasPrefix(header.Name, directoryToExtract+"/") {
// Skip directory to extract
if strings.TrimRight(header.Name, "/") == workingDirectory {
continue
}
// Trim directory name from header name
header.Name = strings.TrimPrefix(header.Name, directoryToExtract+"/")
outputPath := path.Join(workingDirectory, header.Name)
switch header.Typeflag {
case tar.TypeDir:
// Create directory
err := os.MkdirAll(outputPath, 0755)
if err != nil {
return err
}
case tar.TypeReg:
// Create file and set permissions
file, err = os.Create(outputPath)
if err != nil {
return err
}
err := file.Chmod(header.FileInfo().Mode())
if err != nil {
return err
}
// Copy data to file
_, err = io.Copy(file, tr)
if err != nil {
return err
}
// Close file
file.Close()
default:
continue
}
}
}
return nil
}