Skip to content

Commit 99337ef

Browse files
rdiersdiscentem
authored andcommitted
integrate bindetector into upload via fileutils helpers
1 parent 6cd87da commit 99337ef

File tree

8 files changed

+81
-7
lines changed

8 files changed

+81
-7
lines changed

internal/cli/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ go_library(
1414
visibility = ["//:__subpackages__"],
1515
deps = [
1616
"//internal/config",
17+
"//internal/fileutils",
1718
"//internal/metadata",
1819
"//internal/program",
1920
"//internal/stores",

internal/cli/upload.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66

7+
"github.com/discentem/cavorite/internal/fileutils"
78
"github.com/discentem/cavorite/internal/program"
89
"github.com/google/logger"
910
"github.com/spf13/afero"
@@ -33,6 +34,7 @@ func uploadCmd() *cobra.Command {
3334
RunE: uploadFn,
3435
}
3536

37+
// uploadCmd.LocalFlags().Bool("store_type", "", "Storage backend to use")
3638
return uploadCmd
3739
}
3840

@@ -75,7 +77,10 @@ func uploadFn(cmd *cobra.Command, objects []string) error {
7577
}
7678

7779
logger.Infof("Uploading to: %s", s.GetOptions().BackendAddress)
78-
logger.Infof("Uploading file: %s", objects)
80+
objects, err = fileutils.GetBinariesWalkPath(fsys, objects)
81+
if err != nil {
82+
return fmt.Errorf("walking binaries path error: %w", err)
83+
}
7984
if err := s.Upload(cmd.Context(), objects...); err != nil {
8085
return err
8186
}

internal/fileutils/BUILD.bazel

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
load("@io_bazel_rules_go//go:def.bzl", "go_library")
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
22

33
go_library(
44
name = "fileutils",
55
srcs = ["fileutils.go"],
66
importpath = "github.com/discentem/cavorite/internal/fileutils",
77
visibility = ["//:__subpackages__"],
8-
deps = ["@com_github_spf13_afero//:afero"],
8+
deps = [
9+
"//internal/bindetector",
10+
"@com_github_google_logger//:logger",
11+
"@com_github_spf13_afero//:afero",
12+
],
13+
)
14+
15+
go_test(
16+
name = "fileutils_test",
17+
srcs = ["fileutils_test.go"],
18+
embed = [":fileutils"],
19+
deps = ["@com_github_stretchr_testify//assert"],
920
)

internal/fileutils/fileutils.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package fileutils
33
import (
44
"fmt"
55
"io"
6+
"io/fs"
67

8+
"github.com/discentem/cavorite/internal/bindetector"
9+
"github.com/google/logger"
710
"github.com/spf13/afero"
811
)
912

@@ -23,3 +26,37 @@ func BytesFromAferoFile(f afero.File) ([]byte, error) {
2326
}
2427
return b, nil
2528
}
29+
30+
func GetBinariesWalkPath(fsys afero.Fs, objects []string) ([]string, error) {
31+
afs := afero.Afero{Fs: fsys}
32+
33+
var paths []string
34+
logger.V(2).Infof("walking objects: %s", objects)
35+
for _, object := range objects {
36+
err := afs.Walk(object, func(p string, info fs.FileInfo, err error) error {
37+
logger.V(2).Infof("walkfunc() p: %s", p)
38+
isDir, err := afs.IsDir(p)
39+
if err != nil {
40+
logger.Errorf("walkfunc() fsys.IsDir error: %v", err)
41+
return err
42+
}
43+
logger.V(2).Infof("walkfunc() isDir: %t", isDir)
44+
45+
if isDir {
46+
return nil
47+
}
48+
49+
if bindetector.IsBinary(p) {
50+
logger.V(2).Infof("walkfunc() - bindetector().IsBinary appending path: %s", p)
51+
paths = append(paths, p)
52+
}
53+
54+
return nil
55+
})
56+
if err != nil {
57+
return paths, err
58+
}
59+
}
60+
61+
return paths, nil
62+
}

internal/fileutils/fileutils_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package fileutils
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestGetBinariesWalkPath(t *testing.T) {
10+
// expectedBinariesFromPath := []string{
11+
// "abc.pkg",
12+
// "zxf.pkg",
13+
// }
14+
assert.True(t, true)
15+
}

internal/stores/azure.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func (s *AzureBlobStore) Upload(ctx context.Context, objects ...string) error {
4949
return err
5050
}
5151
containerName := path.Base(s.Options.BackendAddress)
52+
logger.Infof(`Uploading "%s" to Azure`, o)
5253
_, err = s.containerClient.UploadStream(
5354
ctx,
5455
containerName,

internal/stores/gcs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ func (s *GCSStore) Upload(ctx context.Context, objects ...string) error {
107107
// return multiple errors
108108
return multErr
109109
}
110+
logger.Infof(`Uploading "%s" to GCS`, o)
110111
_, err = io.Copy(wc, f)
111112
if err != nil {
112113
multErr = multierror.Append(multErr, fmt.Errorf("io.Copy() error: %w", err))

internal/stores/s3.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,22 +160,25 @@ func (s *S3Store) Upload(ctx context.Context, objects ...string) error {
160160
logger.Errorf("error encountered parsing backend address: %v", err)
161161
return err
162162
}
163-
obj := s3.PutObjectInput{
163+
164+
putObjInput := s3.PutObjectInput{
164165
Bucket: aws.String(s3BucketName),
165166
Key: aws.String(o),
166167
Body: f,
167168
}
168-
out, err := s.s3Uploader.Upload(ctx, &obj)
169+
logger.Infof(`Uploading "%s" to S3`, o)
170+
uploadOutput, err := s.s3Uploader.Upload(ctx, &putObjInput)
169171
if err != nil {
170172
if err := cleanupFn(); err != nil {
171173
return fmt.Errorf("cleanup() failed after Upload failure: %w", err)
172174
}
173-
logger.Error(out)
175+
logger.Error(uploadOutput)
174176
return err
175177
}
176178
if err := f.Close(); err != nil {
177179
return err
178180
}
181+
logger.Infof(`Upload Complete for "%s" to S3`, o)
179182
}
180183
return nil
181184
}
@@ -240,7 +243,7 @@ func (s *S3Store) Retrieve(ctx context.Context, objects ...string) error {
240243

241244
func (s *S3Store) getBucketName() (string, error) {
242245
var bucketName string
243-
logger.Infof("s3store getBucketName: backend address: %s", s.Options.BackendAddress)
246+
logger.V(2).Infof("s3store getBucketName: backend address: %s", s.Options.BackendAddress)
244247
switch {
245248
case strings.HasPrefix(s.Options.BackendAddress, "s3://"):
246249
s3BucketUrl, err := url.Parse(s.Options.BackendAddress)

0 commit comments

Comments
 (0)