Skip to content

Commit 0027587

Browse files
committed
Move archive size/checksum code to archive package
1 parent 4bdcc04 commit 0027587

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

internal/command/sync/sync.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import (
4040
"github.com/arduino/libraries-repository-engine/internal/libraries/archive"
4141
"github.com/arduino/libraries-repository-engine/internal/libraries/db"
4242
"github.com/arduino/libraries-repository-engine/internal/libraries/gitutils"
43-
"github.com/arduino/libraries-repository-engine/internal/libraries/hash"
4443
"github.com/go-git/go-git/v5/plumbing"
4544
"github.com/spf13/cobra"
4645
)
@@ -277,7 +276,7 @@ func syncLibraryTaggedRelease(logger *log.Logger, repo *libraries.Repository, ta
277276
return fmt.Errorf("Error while zipping library: %s", err)
278277
}
279278

280-
size, checksum, err := getSizeAndCalculateChecksum(zipFilePath)
279+
size, checksum, err := archive.GetSizeAndCalculateChecksum(zipFilePath)
281280
if err != nil {
282281
return fmt.Errorf("Error while calculating checksums: %s", err)
283282
}
@@ -295,22 +294,6 @@ func syncLibraryTaggedRelease(logger *log.Logger, repo *libraries.Repository, ta
295294
return nil
296295
}
297296

298-
func getSizeAndCalculateChecksum(filePath string) (int64, string, error) {
299-
info, err := os.Stat(filePath)
300-
if err != nil {
301-
return -1, "", err
302-
}
303-
304-
size := info.Size()
305-
306-
checksum, err := hash.Checksum(filePath)
307-
if err != nil {
308-
return -1, "", err
309-
}
310-
311-
return size, checksum, nil
312-
}
313-
314297
func outputLogFile(logger *log.Logger, repoMetadata *libraries.Repo, buffer *bytes.Buffer) error {
315298
if config.LogsFolder == "" {
316299
return nil

internal/libraries/archive/archive.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"path/filepath"
3030
"regexp"
3131

32+
"github.com/arduino/libraries-repository-engine/internal/libraries/hash"
3233
"github.com/arduino/libraries-repository-engine/internal/libraries/metadata"
3334
"github.com/arduino/libraries-repository-engine/internal/libraries/zip"
3435
)
@@ -53,3 +54,20 @@ func ZipFolderName(library *metadata.LibraryMetadata) string {
5354
pattern := regexp.MustCompile("[^a-zA-Z0-9]")
5455
return pattern.ReplaceAllString(library.Name, "_") + "-" + library.Version
5556
}
57+
58+
// GetSizeAndCalculateChecksum returns the size and SHA-256 checksum for the given file.
59+
func GetSizeAndCalculateChecksum(filePath string) (int64, string, error) {
60+
info, err := os.Stat(filePath)
61+
if err != nil {
62+
return -1, "", err
63+
}
64+
65+
size := info.Size()
66+
67+
checksum, err := hash.Checksum(filePath)
68+
if err != nil {
69+
return -1, "", err
70+
}
71+
72+
return size, checksum, nil
73+
}

0 commit comments

Comments
 (0)