Skip to content

Commit

Permalink
lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
idsulik committed Oct 6, 2024
1 parent 18ca016 commit cc14e73
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
25 changes: 13 additions & 12 deletions pkg/compose/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (
"bytes"
"encoding/json"
"fmt"
"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 All @@ -38,16 +38,16 @@ func ServiceHash(o types.ServiceConfig) (string, error) {
}
o.DependsOn = nil

bytes, err := json.Marshal(o)
data, err := json.Marshal(o)
if err != nil {
return "", err
}
return digest.SHA256.FromBytes(bytes).Encoded(), nil
return digest.SHA256.FromBytes(data).Encoded(), nil
}

// ServiceConfigsHash computes the configuration hash for service configs.
func ServiceConfigsHash(project *types.Project, serviceConfig types.ServiceConfig) (string, error) {
bytes := make([]byte, 0)
data := make([]byte, 0)
for _, config := range serviceConfig.Configs {
file := project.Configs[config.Source]
b, err := createTarForConfig(project, types.FileReferenceConfig(config), types.FileObjectConfig(file))
Expand All @@ -56,15 +56,15 @@ func ServiceConfigsHash(project *types.Project, serviceConfig types.ServiceConfi
return "", err
}

bytes = append(bytes, b.Bytes()...)
data = append(data, b.Bytes()...)
}

return digest.SHA256.FromBytes(bytes).Encoded(), nil
return digest.SHA256.FromBytes(data).Encoded(), nil
}

// ServiceSecretsHash computes the configuration hash for service secrets.
func ServiceSecretsHash(project *types.Project, serviceConfig types.ServiceConfig) (string, error) {
bytes := make([]byte, 0)
data := make([]byte, 0)
for _, secret := range serviceConfig.Secrets {
file := project.Secrets[secret.Source]
b, err := createTarForConfig(project, types.FileReferenceConfig(secret), types.FileObjectConfig(file))
Expand All @@ -73,10 +73,10 @@ func ServiceSecretsHash(project *types.Project, serviceConfig types.ServiceConfi
return "", err
}

bytes = append(bytes, b.Bytes()...)
data = append(data, b.Bytes()...)
}

return digest.SHA256.FromBytes(bytes).Encoded(), nil
return digest.SHA256.FromBytes(data).Encoded(), nil
}

func createTarForConfig(
Expand All @@ -91,9 +91,10 @@ func createTarForConfig(
serviceConfig.Target = "/" + serviceConfig.Source
}

if file.Content != "" {
switch {
case file.Content != "":
return bytes.NewBuffer([]byte(file.Content)), nil
} else if file.Environment != "" {
case file.Environment != "":
env, ok := project.Environment[file.Environment]
if !ok {
return nil, fmt.Errorf(
Expand All @@ -103,7 +104,7 @@ func createTarForConfig(
)
}
return bytes.NewBuffer([]byte(env)), nil
} else if file.File != "" {
case file.File != "":
return utils.CreateTarByPath(file.File, modTime)
}

Expand Down
8 changes: 6 additions & 2 deletions pkg/utils/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ func CreateTar(content []byte, config types.FileReferenceConfig, modTime time.Ti
func CreateTarByPath(path string, modTime time.Time) (*bytes.Buffer, error) {
b := new(bytes.Buffer)
tw := tar.NewWriter(b)
defer tw.Close()
defer func() {
_ = 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 {
Expand Down Expand Up @@ -110,7 +112,9 @@ func CreateTarByPath(path string, modTime time.Time) (*bytes.Buffer, error) {
if err != nil {
return err
}
defer f.Close()
defer func() {
_ = f.Close()
}()

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

0 comments on commit cc14e73

Please # to comment.