Skip to content

Commit

Permalink
feat: Add directories and maintain structure
Browse files Browse the repository at this point in the history
  • Loading branch information
NoUseFreak committed Dec 8, 2023
1 parent d972a49 commit 25b3690
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 3 deletions.
3 changes: 3 additions & 0 deletions internal/drivers/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"io/ioutil"
"os"
"path/filepath"
)

func init() {
Expand Down Expand Up @@ -65,6 +66,8 @@ func (d *LocalDriver) Copy(src, dst string) (int64, error) {
}
defer source.Close()

os.MkdirAll(filepath.Dir(dst), 0750)

destination, err := os.Create(dst)
if err != nil {
return 0, err
Expand Down
2 changes: 1 addition & 1 deletion internal/drivers/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (d *S3Driver) Copy(src, dst string) (int64, error) {
return 0, fmt.Errorf("failed to open file %q, %v", src, err)
}

logrus.Tracef("Uploading %s:%s", d.bucket, src)
logrus.Tracef("Uploading %s to %s:%s", src, d.bucket, dst)
_, err = uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(d.bucket),
Key: aws.String(dst),
Expand Down
2 changes: 1 addition & 1 deletion internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (u *Utils) CopyFiles(files []string, target string) error {
return err
}
for _, file := range files {
if _, err := u.Driver.Copy(file, path.Join(target, path.Base(file))); err != nil {
if _, err := u.Driver.Copy(file, path.Join(target, file)); err != nil {
return err
}
}
Expand Down
24 changes: 24 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"os"
"path"
"path/filepath"
"time"

vembed "github.com/NoUseFreak/go-vembed"
Expand Down Expand Up @@ -76,6 +77,7 @@ var rootCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, files []string) error {
date, _ := cmd.Flags().GetString("date")

files = resolveFiles(files)
storageDriver, err := drivers.GetDriver(driverName)
if err != nil {
return err
Expand Down Expand Up @@ -106,6 +108,28 @@ var rootCmd = &cobra.Command{
},
}

func resolveFiles(files []string) []string {
resolvedFiles := []string{}
for _, file := range files {
if info, err := os.Stat(file); err == nil {
if info.IsDir() {
filepath.Walk(file, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
resolvedFiles = append(resolvedFiles, path)
}
return nil
})
} else {
resolvedFiles = append(resolvedFiles, file)
}
} else {
logrus.Warn("File not found: ", file)
}
}

return resolvedFiles
}

func setUpLogs(out io.Writer, level string) error {
logrus.SetOutput(out)

Expand Down
2 changes: 1 addition & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ for y in {2016..2021}; do
for m in {1..12}; do
for d in {1..28}; do
./test-binary \
--driver=local tests/file*.txt \
--driver=local tests \
--date $(printf "%02d-%02d-%02d_%02d-%02d-%02d" ${y} ${m} ${d} $((1 + $RANDOM % 10)) $((1 + $RANDOM % 10)) $((1 + $RANDOM % 10))) \
"$@"
done
Expand Down
Empty file added tests/subdir/test3.txt
Empty file.

0 comments on commit 25b3690

Please sign in to comment.