diff --git a/internal/drivers/local.go b/internal/drivers/local.go index 329df65..e28d1bf 100644 --- a/internal/drivers/local.go +++ b/internal/drivers/local.go @@ -5,6 +5,7 @@ import ( "io" "io/ioutil" "os" + "path/filepath" ) func init() { @@ -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 diff --git a/internal/drivers/s3.go b/internal/drivers/s3.go index 23d4689..de4e8a1 100644 --- a/internal/drivers/s3.go +++ b/internal/drivers/s3.go @@ -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), diff --git a/internal/utils/utils.go b/internal/utils/utils.go index f3ac7c8..48d53bb 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -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 } } diff --git a/main.go b/main.go index cb49c94..f80b3d7 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "io" "os" "path" + "path/filepath" "time" vembed "github.com/NoUseFreak/go-vembed" @@ -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 @@ -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) diff --git a/test.sh b/test.sh index a8b8e9a..5e0e2df 100755 --- a/test.sh +++ b/test.sh @@ -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 diff --git a/tests/subdir/test3.txt b/tests/subdir/test3.txt new file mode 100644 index 0000000..e69de29