Skip to content

Commit

Permalink
Don't re-Bag directories that are already Bags
Browse files Browse the repository at this point in the history
Added logic in create Bag activity to not try to create Bags from
directories that are already Bags.
  • Loading branch information
mcantelon committed Jan 9, 2025
1 parent cca7443 commit da823c9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions bagcreate/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"io/fs"
"os"
"path/filepath"

gobagit "github.com/nyudlts/go-bagit"
cp "github.com/otiai10/copy"
Expand Down Expand Up @@ -52,6 +54,11 @@ func (a *Activity) Execute(ctx context.Context, params *Params) (*Result, error)
logger := temporal.GetLogger(ctx)
logger.V(1).Info("Executing bag-create activity", "SourcePath", params.SourcePath)

// Check if directory is already a Bag
if _, err := os.Stat(filepath.Join(params.SourcePath, "bagit.txt")); err == nil {
return &Result{BagPath: params.SourcePath}, nil
}

dest, err := a.create(params.SourcePath, params.BagPath)
if err != nil {
return nil, fmt.Errorf("bagcreate: %v", err)
Expand Down
21 changes: 21 additions & 0 deletions bagcreate/activity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ func sourcePath(t *testing.T) string {
return td.Path()
}

func existingBagPath(t *testing.T) string {
t.Helper()

td := tfs.NewDir(t, "sdps_bagit_existing_bag_create_test",
tfs.WithFile("bagit.txt", "Fake bagit file\n"),
tfs.WithDir("data"),
)

return td.Path()
}

func TestActivity(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -107,6 +118,16 @@ func TestActivity(t *testing.T) {
},
wantErr: "activity error (type: bag-create, scheduledEventID: 0, startedEventID: 0, identity: ): bagcreate: copy source dir to bag path: open",
},
{
name: "Errors if source dir is already a bag",
params: bagcreate.Params{
SourcePath: existingBagPath(t),
},
want: tfs.Expected(t,
tfs.WithFile("bagit.txt", "Fake bagit file\n"),
tfs.WithDir("data"),
),
},
} {
tt := tt
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit da823c9

Please sign in to comment.