Skip to content

integrate bindetector into upload via fileutils helpers #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/cli/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ go_library(
visibility = ["//:__subpackages__"],
deps = [
"//internal/config",
"//internal/fileutils",
"//internal/metadata",
"//internal/program",
"//internal/stores",
Expand Down
7 changes: 6 additions & 1 deletion internal/cli/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"

"github.com/discentem/cavorite/internal/fileutils"
"github.com/discentem/cavorite/internal/program"
"github.com/google/logger"
"github.com/spf13/afero"
Expand Down Expand Up @@ -33,6 +34,7 @@ func uploadCmd() *cobra.Command {
RunE: uploadFn,
}

// uploadCmd.LocalFlags().Bool("store_type", "", "Storage backend to use")
return uploadCmd
}

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

logger.Infof("Uploading to: %s", s.GetOptions().BackendAddress)
logger.Infof("Uploading file: %s", objects)
objects, err = fileutils.GetBinariesWalkPath(fsys, objects)
if err != nil {
return fmt.Errorf("walking binaries path error: %w", err)
}
if err := s.Upload(cmd.Context(), objects...); err != nil {
return err
}
Expand Down
15 changes: 13 additions & 2 deletions internal/fileutils/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "fileutils",
srcs = ["fileutils.go"],
importpath = "github.com/discentem/cavorite/internal/fileutils",
visibility = ["//:__subpackages__"],
deps = ["@com_github_spf13_afero//:afero"],
deps = [
"//internal/bindetector",
"@com_github_google_logger//:logger",
"@com_github_spf13_afero//:afero",
],
)

go_test(
name = "fileutils_test",
srcs = ["fileutils_test.go"],
embed = [":fileutils"],
deps = ["@com_github_stretchr_testify//assert"],
)
37 changes: 37 additions & 0 deletions internal/fileutils/fileutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package fileutils
import (
"fmt"
"io"
"io/fs"

"github.com/discentem/cavorite/internal/bindetector"
"github.com/google/logger"
"github.com/spf13/afero"
)

Expand All @@ -23,3 +26,37 @@ func BytesFromAferoFile(f afero.File) ([]byte, error) {
}
return b, nil
}

func GetBinariesWalkPath(fsys afero.Fs, objects []string) ([]string, error) {
afs := afero.Afero{Fs: fsys}

var paths []string
logger.V(2).Infof("walking objects: %s", objects)
for _, object := range objects {
walkErr := afs.Walk(object, func(p string, info fs.FileInfo, err error) error {
logger.V(2).Infof("walkfunc() p: %s", p)
isDir, err := afs.IsDir(p)
if err != nil {
logger.Errorf("walkfunc() fsys.IsDir error: %v", err)
return err
}
logger.V(2).Infof("walkfunc() isDir: %t", isDir)

if isDir {
return nil
}

if bindetector.IsBinary(p) {
logger.V(2).Infof("walkfunc() - bindetector().IsBinary appending path: %s", p)
paths = append(paths, p)
}

return nil
})
if walkErr != nil {
return []string{}, walkErr
}
}

return paths, nil
}
15 changes: 15 additions & 0 deletions internal/fileutils/fileutils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package fileutils

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetBinariesWalkPath(t *testing.T) {
// expectedBinariesFromPath := []string{
// "abc.pkg",
// "zxf.pkg",
// }
assert.True(t, true)
}
1 change: 1 addition & 0 deletions internal/stores/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (s *AzureBlobStore) Upload(ctx context.Context, objects ...string) error {
return err
}
containerName := path.Base(s.Options.BackendAddress)
logger.Infof(`Uploading "%s" to Azure`, o)
_, err = s.containerClient.UploadStream(
ctx,
containerName,
Expand Down
1 change: 1 addition & 0 deletions internal/stores/gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func (s *GCSStore) Upload(ctx context.Context, objects ...string) error {
// return multiple errors
return multErr
}
logger.Infof(`Uploading "%s" to GCS`, o)
_, err = io.Copy(wc, f)
if err != nil {
multErr = multierror.Append(multErr, fmt.Errorf("io.Copy() error: %w", err))
Expand Down
11 changes: 7 additions & 4 deletions internal/stores/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,22 +160,25 @@ func (s *S3Store) Upload(ctx context.Context, objects ...string) error {
logger.Errorf("error encountered parsing backend address: %v", err)
return err
}
obj := s3.PutObjectInput{

putObjInput := s3.PutObjectInput{
Bucket: aws.String(s3BucketName),
Key: aws.String(o),
Body: f,
}
out, err := s.s3Uploader.Upload(ctx, &obj)
logger.Infof(`Uploading "%s" to S3`, o)
uploadOutput, err := s.s3Uploader.Upload(ctx, &putObjInput)
if err != nil {
if err := cleanupFn(); err != nil {
return fmt.Errorf("cleanup() failed after Upload failure: %w", err)
}
logger.Error(out)
logger.Error(uploadOutput)
return err
}
if err := f.Close(); err != nil {
return err
}
logger.Infof(`Upload Complete for "%s" to S3`, o)
}
return nil
}
Expand Down Expand Up @@ -240,7 +243,7 @@ func (s *S3Store) Retrieve(ctx context.Context, objects ...string) error {

func (s *S3Store) getBucketName() (string, error) {
var bucketName string
logger.Infof("s3store getBucketName: backend address: %s", s.Options.BackendAddress)
logger.V(2).Infof("s3store getBucketName: backend address: %s", s.Options.BackendAddress)
switch {
case strings.HasPrefix(s.Options.BackendAddress, "s3://"):
s3BucketUrl, err := url.Parse(s.Options.BackendAddress)
Expand Down