Skip to content

Commit

Permalink
updated hash.go and tar.go
Browse files Browse the repository at this point in the history
Signed-off-by: Suleiman Dibirov <idsulik@gmail.com>
  • Loading branch information
idsulik committed Oct 6, 2024
1 parent c4d8087 commit 18ca016
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 65 deletions.
74 changes: 9 additions & 65 deletions pkg/compose/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/docker/compose/v2/pkg/utils"
"time"

"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/compose/v2/pkg/utils"
"github.com/opencontainers/go-digest"
)

Expand Down Expand Up @@ -88,10 +86,13 @@ func createTarForConfig(
) (*bytes.Buffer, error) {
// fixed time to ensure the tarball is deterministic
modTime := time.Unix(0, 0)
content := make([]byte, 0)

if serviceConfig.Target == "" {
serviceConfig.Target = "/" + serviceConfig.Source
}

if file.Content != "" {
content = []byte(file.Content)
return bytes.NewBuffer([]byte(file.Content)), nil
} else if file.Environment != "" {
env, ok := project.Environment[file.Environment]
if !ok {
Expand All @@ -101,67 +102,10 @@ func createTarForConfig(
file.Name,
)
}
content = []byte(env)
return bytes.NewBuffer([]byte(env)), nil
} else if file.File != "" {
var err error
content, err = readPathContent(file.File)

if err != nil {
return nil, err
}
}

if len(content) == 0 {
return nil, fmt.Errorf("config %q is empty", file.Name)
}

if serviceConfig.Target == "" {
serviceConfig.Target = "/" + serviceConfig.Source
}

b, err := utils.CreateTar(content, serviceConfig, modTime)
if err != nil {
return nil, err
}

return &b, nil
}

func readPathContent(path string) ([]byte, error) {
content := make([]byte, 0)

// Check if the path is a directory
info, err := os.Stat(path)
if err != nil {
return nil, fmt.Errorf("error accessing path %q: %v", path, err)
}

if info.IsDir() {
// If it's a directory, read all files and concatenate their contents
err = filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
fileContent, err := os.ReadFile(path)
if err != nil {
return err
}
content = append(content, fileContent...)
}
return nil
})
if err != nil {
return nil, fmt.Errorf("error reading directory %q: %v", path, err)
}
} else {
// If it's a file, read its content
fileContent, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("error reading file %q: %v", path, err)
}
content = fileContent
return utils.CreateTarByPath(file.File, modTime)
}

return content, nil
return nil, fmt.Errorf("config %q is empty", file.Name)
}
58 changes: 58 additions & 0 deletions pkg/utils/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ package utils
import (
"archive/tar"
"bytes"
"io"
"os"
"path/filepath"
"strconv"
"time"

Expand Down Expand Up @@ -68,3 +71,58 @@ func CreateTar(content []byte, config types.FileReferenceConfig, modTime time.Ti
err = tarWriter.Close()
return b, err
}

func CreateTarByPath(path string, modTime time.Time) (*bytes.Buffer, error) {
b := new(bytes.Buffer)
tw := tar.NewWriter(b)
defer tw.Close()

// Walk the directory or file tree at the given path
err := filepath.Walk(path, func(file string, fi os.FileInfo, err error) error {
if err != nil {
return err
}

// Create tar header
header, err := tar.FileInfoHeader(fi, fi.Name())
if err != nil {
return err
}

// Preserve folder structure by using relative paths
header.Name, err = filepath.Rel(filepath.Dir(path), file)
if err != nil {
return err
}

// Set custom modification time
header.ModTime = modTime

// Write header to the tarball
if err := tw.WriteHeader(header); err != nil {
return err
}

// If it's a directory, we don't need to write file content
if fi.Mode().IsRegular() {
// Open the file and write its contents
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()

if _, err := io.Copy(tw, f); err != nil {
return err
}
}

return nil
})

if err != nil {
return nil, err
}

return b, nil
}

0 comments on commit 18ca016

Please sign in to comment.