-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7fa4b9d
commit c830405
Showing
8 changed files
with
355 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package utils | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func IsIn(dir, path string) bool { | ||
rel, err := filepath.Rel(dir, path) | ||
if err != nil { | ||
return false | ||
} | ||
return filepath.IsLocal(rel) | ||
} | ||
|
||
func CopyRecursive(from, to string) error { | ||
return filepath.Walk(from, func(path string, info os.FileInfo, err error) error { //nolint:wrapcheck | ||
if err != nil { | ||
return err | ||
} | ||
if IsIn(to, path) { | ||
return nil | ||
} | ||
relPath, err := filepath.Rel(from, path) | ||
if err != nil { | ||
return err //nolint:wrapcheck | ||
} | ||
newPath := filepath.Join(to, relPath) | ||
if info.IsDir() { | ||
err := os.Mkdir(newPath, 0o755) | ||
if err != nil && !os.IsExist(err) { | ||
return err //nolint:wrapcheck | ||
} | ||
return nil | ||
} | ||
f, err := os.Open(path) | ||
if err != nil { | ||
return err //nolint:wrapcheck | ||
} | ||
defer f.Close() | ||
f2, err := os.Create(newPath) | ||
if err != nil { | ||
return err //nolint:wrapcheck | ||
} | ||
defer f2.Close() | ||
_, err = io.Copy(f2, f) | ||
return err //nolint:wrapcheck | ||
}) | ||
} | ||
|
||
func MoveRecursive(from, to string) (bool, error) { | ||
err := CopyRecursive(from, to) | ||
if err != nil { | ||
return false, errors.Wrapf(err, "failed to copy %s to %s", from, to) | ||
} | ||
err = filepath.Walk(from, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
if !os.IsNotExist(err) { | ||
return err | ||
} | ||
return nil | ||
} | ||
if IsIn(path, to) { | ||
// Skip parent directories of destination | ||
return nil | ||
} | ||
if IsIn(to, path) { | ||
// Skip contents of destination | ||
return nil | ||
} | ||
err = os.RemoveAll(path) | ||
if err != nil { | ||
if !os.IsNotExist(err) { | ||
return err //nolint:wrapcheck | ||
} | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return true, errors.Wrapf(err, "failed to remove %s", from) | ||
} | ||
return true, nil | ||
} |
Oops, something went wrong.