Skip to content
This repository was archived by the owner on Oct 23, 2023. It is now read-only.

Add library function for copying directory #59

Merged
merged 7 commits into from
Jun 20, 2022
Merged
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
44 changes: 44 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ import (
"io/ioutil"
"os"
"os/exec"
"path"
"regexp"
"strconv"
"strings"
"syscall"

"github.com/canonical/edgex-snap-hooks/v2/log"
"github.com/canonical/edgex-snap-hooks/v2/options"
Expand Down Expand Up @@ -82,6 +84,48 @@ func CopyFile(srcPath, destPath string) error {
return nil
}

// CopyDir copies a whole directory recursively
// snippet from https://blog.depa.do/post/copy-files-and-directories-in-go
func CopyDir(srcPath string, dstPath string) error {
var err error
var fds []os.FileInfo
var srcinfo os.FileInfo

srcinfo, err = os.Stat(srcPath)
if err != nil {
return err
}

oldMask := syscall.Umask(0)
defer syscall.Umask(oldMask)

err = os.MkdirAll(dstPath, srcinfo.Mode())
if err != nil {
return err
}

if fds, err = ioutil.ReadDir(srcPath); err != nil {
return err
}
for _, fd := range fds {
srcfp := path.Join(srcPath, fd.Name())
dstfp := path.Join(dstPath, fd.Name())

if fd.IsDir() {
err = CopyDir(srcfp, dstfp)
if err != nil {
return err
}
} else {
err = CopyFile(srcfp, dstfp)
if err != nil {
return err
}
}
}
return nil
}

// CopyFileReplace copies a file within the snap and replaces strings using
// the string/replace values in the rStrings parameter.
func CopyFileReplace(srcPath, destPath string, rStrings map[string]string) error {
Expand Down
25 changes: 25 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,28 @@ func getConfigValue(t *testing.T, key string) string {
require.NoError(t, err, "Error getting config value via snapctl.")
return strings.TrimSpace(string(out))
}

func TestCopyFile(t *testing.T) {
tmpdir := t.TempDir()
tmpfile, _ := os.CreateTemp(tmpdir, "tmpSrcFile")
srcPath := tmpfile.Name()

tmpdir = t.TempDir()
tmpfile, _ = os.CreateTemp(tmpdir, "tmpDstFile")
dstPath := tmpfile.Name()

require.NoError(t, CopyFile(srcPath, dstPath), "Error copying file.")
}

func TestCopyDir(t *testing.T) {
tmpdir := t.TempDir()
tmpSrcDir, _ := os.MkdirTemp(tmpdir, "tmpSrcDir")
os.CreateTemp(tmpSrcDir, "tmpSrcFile1")
os.CreateTemp(tmpSrcDir, "tmpSrcFile2")
os.CreateTemp(tmpSrcDir, "tmpSrcFile3")

tmpdir = t.TempDir()
tmpDstDir, _ := os.MkdirTemp(tmpdir, "tmpDstDir")

require.NoError(t, CopyDir(tmpSrcDir, tmpDstDir), "Error copying directory.")
}
Comment on lines +151 to +174
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests should have better coverage for permissions. Also, all errors during the mocking are ignored.