Skip to content

Commit

Permalink
use functional options handle copy option (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
zcong1993 committed May 7, 2020
1 parent bf3a121 commit be85817
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
21 changes: 9 additions & 12 deletions utils/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
)

// Copy copies src to dest, doesn't matter if src is a directory or a file.
func Copy(src, dest string, opt ...Options) error {
func Copy(src, dest string, opt ...CopyOption) error {
info, err := os.Lstat(src)
if err != nil {
return err
Expand All @@ -26,7 +26,7 @@ func Copy(src, dest string, opt ...Options) error {
// copy dispatches copy-funcs according to the mode.
// Because this "copy" could be called recursively,
// "info" MUST be given here, NOT nil.
func copy(src, dest string, info os.FileInfo, opt Options) error {
func copy(src, dest string, info os.FileInfo, opt *Options) error {

if opt.Skip(src) {
return nil
Expand All @@ -45,7 +45,7 @@ func copy(src, dest string, info os.FileInfo, opt Options) error {
// fcopy is for just a file,
// with considering existence of parent directory
// and file permission.
func fcopy(src, dest string, info os.FileInfo, opt Options) (err error) {
func fcopy(src, dest string, info os.FileInfo, opt *Options) (err error) {

if err = os.MkdirAll(filepath.Dir(dest), os.ModePerm); err != nil {
return
Expand Down Expand Up @@ -81,7 +81,7 @@ func fcopy(src, dest string, info os.FileInfo, opt Options) (err error) {
// dcopy is for a directory,
// with scanning contents inside the directory
// and pass everything to "copy" recursively.
func dcopy(srcdir, destdir string, info os.FileInfo, opt Options) (err error) {
func dcopy(srcdir, destdir string, info os.FileInfo, opt *Options) (err error) {

originalMode := info.Mode()

Expand All @@ -108,7 +108,7 @@ func dcopy(srcdir, destdir string, info os.FileInfo, opt Options) (err error) {
return
}

func onsymlink(src, dest string, info os.FileInfo, opt Options) error {
func onsymlink(src, dest string, info os.FileInfo, opt *Options) error {

switch opt.OnSymlink(src) {
case Shallow:
Expand Down Expand Up @@ -160,16 +160,13 @@ func chmod(dir string, mode os.FileMode, reported *error) {

// assure Options struct, should be called only once.
// All optional values MUST NOT BE nil/zero after assured.
func assure(opts ...Options) Options {
func assure(opts ...CopyOption) *Options {
if len(opts) == 0 {
return getDefaultOptions()
}
defopt := getDefaultOptions()
if opts[0].OnSymlink == nil {
opts[0].OnSymlink = defopt.OnSymlink
for _, opt := range opts {
opt(defopt)
}
if opts[0].Skip == nil {
opts[0].Skip = defopt.Skip
}
return opts[0]
return defopt
}
7 changes: 5 additions & 2 deletions utils/copy_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ const (
Skip
)

// CopyOption define a function type that handle optional actions.
type CopyOption func(*Options)

// getDefaultOptions provides default options,
// which would be modified by usage-side.
func getDefaultOptions() Options {
return Options{
func getDefaultOptions() *Options {
return &Options{
OnSymlink: func(string) SymlinkAction {
return Shallow // Do shallow copy
},
Expand Down

0 comments on commit be85817

Please sign in to comment.