Skip to content

Commit

Permalink
Move away from using package level vars
Browse files Browse the repository at this point in the history
Only use them inside the `Execute` otherwise it is super annoying to test and understand what is happening
  • Loading branch information
0sewa0 committed Feb 12, 2025
1 parent 29c2bc4 commit 87a8b13
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
14 changes: 7 additions & 7 deletions pkg/move/atomic.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import (
"github.com/spf13/afero"
)

func atomic(copy copyFunc) copyFunc {
func atomic(work string, copy copyFunc) copyFunc {
return func(fs afero.Afero, from, to string) (err error) {
logrus.Infof("Setting up atomic operation from %s to %s", from, to)

err = fs.RemoveAll(workFolder)
err = fs.RemoveAll(work)
if err != nil {
logrus.Errorf("Failed initial cleanup of workdir: %v", err)

return err
}

err = fs.MkdirAll(workFolder, os.ModePerm)
err = fs.MkdirAll(work, os.ModePerm)
if err != nil {
logrus.Errorf("Failed to create the base workdir: %v", err)

Expand All @@ -27,27 +27,27 @@ func atomic(copy copyFunc) copyFunc {

defer func() {
if err != nil {
if cleanupErr := fs.RemoveAll(workFolder); cleanupErr != nil {
if cleanupErr := fs.RemoveAll(work); cleanupErr != nil {
logrus.Errorf("Failed cleanup of workdir after failure: %v", err)
}
}
}()

err = copy(fs, from, workFolder)
err = copy(fs, from, work)
if err != nil {
logrus.Errorf("Error copying folder: %v", err)

return err
}

err = fs.Rename(workFolder, to)
err = fs.Rename(work, to)
if err != nil {
logrus.Errorf("Error moving folder: %v", err)

return err
}

logrus.Infof("Successfully finalized atomic operation from %s to %s", workFolder, to)
logrus.Infof("Successfully finalized atomic operation from %s to %s", work, to)

return nil
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/move/atomic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func mockCopyFuncWithAtomicCheck(t *testing.T, isSuccessful bool) copyFunc {
func TestAtomic(t *testing.T) {
source := "/source"
target := "/target"
work := "/work"

t.Run("success -> target is present", func(t *testing.T) {
fs := afero.Afero{Fs: afero.NewMemMapFs()}
Expand All @@ -45,7 +46,7 @@ func TestAtomic(t *testing.T) {
err := fs.MkdirAll(source, 0755)
assert.NoError(t, err)

atomicCopy := atomic(mockCopyFuncWithAtomicCheck(t, true))
atomicCopy := atomic(work, mockCopyFuncWithAtomicCheck(t, true))

err = atomicCopy(fs, source, target)
assert.NoError(t, err)
Expand All @@ -66,9 +67,8 @@ func TestAtomic(t *testing.T) {
})
t.Run("fail -> target is not present", func(t *testing.T) {
fs := afero.Afero{Fs: afero.NewMemMapFs()}
workFolder = "/work"

atomicCopy := atomic(mockCopyFuncWithAtomicCheck(t, false))
atomicCopy := atomic(work, mockCopyFuncWithAtomicCheck(t, false))

err := atomicCopy(fs, source, target)
assert.Error(t, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/move/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Execute(fs afero.Afero) error {
}

if workFolder != "" {
copy = atomic(copy)
copy = atomic(workFolder, copy)
}

return copy(fs, sourceFolder, targetFolder)
Expand Down

0 comments on commit 87a8b13

Please sign in to comment.