aboutsummaryrefslogtreecommitdiff
path: root/copy.go
diff options
context:
space:
mode:
authorFlu0r1ne <flur01ne@flu0r1ne.net>2021-09-19 00:27:55 -0500
committerFlu0r1ne <flur01ne@flu0r1ne.net>2021-09-19 00:27:55 -0500
commit1d479733bcbeb630557cd07f721d4510e989934c (patch)
tree42422bd0c2ecc51869eceb8bbead8f5c3e58855f /copy.go
parent86d8789f065636779d3cecded363743b2a39bb47 (diff)
downloadplanr-master.tar.xz
planr-master.zip
Add list command, copy for bash command, and ability to run non-directory commands anywhereHEADv0.1.6master
Diffstat (limited to 'copy.go')
-rw-r--r--copy.go96
1 files changed, 96 insertions, 0 deletions
diff --git a/copy.go b/copy.go
new file mode 100644
index 0000000..72d9635
--- /dev/null
+++ b/copy.go
@@ -0,0 +1,96 @@
+package planr
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+ "io"
+)
+
+
+func Exists(filePath string) bool {
+ if _, err := os.Stat(filePath); os.IsNotExist(err) {
+ return false
+ }
+
+ return true
+}
+
+func CreateIfNotExists(dir string, perm os.FileMode) error {
+ if Exists(dir) {
+ return nil
+ }
+
+ if err := os.MkdirAll(dir, perm); err != nil {
+ return fmt.Errorf("failed to create directory: '%s', error: '%s'", dir, err.Error())
+ }
+
+ return nil
+}
+
+func CopySymlink(src, dst string, perm os.FileMode) error {
+ link, err := os.Readlink(src)
+ if err != nil {
+ return err
+ }
+
+ if err := os.Chmod(src, perm); err != nil {
+ return err
+ }
+
+ return os.Symlink(link, dst)
+}
+
+func Copy(srcFile, dstFile string, perm os.FileMode) error {
+ src, err := os.Open(srcFile)
+ if err != nil {
+ return err
+ }
+
+ defer src.Close()
+
+ dst, err := os.OpenFile(dstFile, os.O_CREATE | os.O_WRONLY | os.O_TRUNC, perm)
+ if err != nil {
+ return err
+ }
+
+ defer dst.Close()
+
+ _, err = io.Copy(dst, src)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
+
+
+func RecursiveCopyDir(source, destination string) error {
+ err := filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
+ relPath, err := filepath.Rel(source, path)
+
+ if err != nil {
+ return err
+ }
+
+ dst := filepath.Join(destination, relPath)
+ src := filepath.Join(source, relPath)
+
+ mode := info.Mode()
+
+ if relPath == "" {
+ return nil
+ }
+
+ switch info.Mode() & os.ModeType {
+ case os.ModeDir:
+ return CreateIfNotExists(dst, mode)
+ case os.ModeSymlink:
+ return CopySymlink(src, dst, mode)
+ default:
+ return Copy(src, dst, mode)
+ }
+ });
+
+ return err
+}