Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Code Refactor & Cleanup #154

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

VILJkid
Copy link

@VILJkid VILJkid commented Jun 21, 2024

Issue #155

Changes introduced

  • _ the unused variables in functions
  • PerservePermission to PreservePermission typo fix
  • Usage of early returns
  • Removal of else conditions
  • Making the code look nice 😉

@VILJkid VILJkid changed the title (fix) : code refactor and cleanup [Fix] Code Refactor & Cleanup Jun 21, 2024
if os.IsNotExist(err) {
return nil
if !os.IsNotExist(err) {
return
Copy link
Owner

Choose a reason for hiding this comment

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

[Just IMO for readability] 🤔
In this case, if os.IsNotExist(err) is the exceptional case.
Code-readers expect if err != nil, then return err as usual. But the interesting point is that we do not return the error if it's ErrNotExist.
To represent this flow of thinking, I do prefer using if os.IsNotExist(err); then return nil.

In addition, !os.IsNotExist(err) is a double-negative way.

@@ -461,7 +461,8 @@ func TestOptions_RenameDestination(t *testing.T) {
// Your own logic here
if strings.HasSuffix(s, ".template") {
return strings.TrimSuffix(d, ".template"), nil
} else if strings.HasSuffix(s, ".tpl") {
}
if strings.HasSuffix(s, ".tpl") {
Copy link
Owner

Choose a reason for hiding this comment

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

I love this.

return opt.FS.Open(src)
}
return os.Open(src)
}
Copy link
Owner

Choose a reason for hiding this comment

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

I love this.

return nil
skip, err := onDirExists(opt, srcdir, destdir)
if err != nil || skip {
return
}
Copy link
Owner

Choose a reason for hiding this comment

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

[Just IMO for readability] 🤔
Returning error on error and skip this dcopy if on skip == true are different things. Thus I prefer separating those 2 in two returns.
What do you think?

} else {
readcloser, err = os.Open(src)
}
readcloser, err := fopen(src, opt)
Copy link
Owner

Choose a reason for hiding this comment

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

In the following function dread, you give only opt.FS. What's the difference between this and that?

return
}
if err = chooseAndPerformCopyMethod(shouldCopyConcurrent, srcdir, destdir, contents, opt); err != nil {
return
Copy link
Owner

Choose a reason for hiding this comment

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

I don't see any benefit of separating this block to a function.

@@ -262,7 +274,10 @@ func dcopyConcurrent(srcdir, destdir string, contents []os.FileInfo, opt Options

func onDirExists(opt Options, srcdir, destdir string) (bool, error) {
_, err := os.Stat(destdir)
if err == nil && opt.OnDirExists != nil && destdir != opt.intent.dest {
if err != nil && !os.IsNotExist(err) {
return true, err // Unwelcome error type...!
Copy link
Owner

Choose a reason for hiding this comment

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

I love this comment.

@@ -262,7 +274,10 @@ func dcopyConcurrent(srcdir, destdir string, contents []os.FileInfo, opt Options

func onDirExists(opt Options, srcdir, destdir string) (bool, error) {
_, err := os.Stat(destdir)
if err == nil && opt.OnDirExists != nil && destdir != opt.intent.dest {
if err != nil && !os.IsNotExist(err) {
Copy link
Owner

Choose a reason for hiding this comment

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

I love this condition separation and moving exception case first.

if err != nil && !os.IsNotExist(err) {
return true, err // Unwelcome error type...!
}
if opt.OnDirExists != nil && destdir != opt.intent.dest {
switch opt.OnDirExists(srcdir, destdir) {
Copy link
Owner

Choose a reason for hiding this comment

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

IMO, we can separate another exception case destdir == opt.intent.dest on the top of this function for early-return.

@@ -130,7 +130,7 @@ func getDefaultOptions(src, dest string) Options {
OnError: nil, // Default is "accept error"
Skip: nil, // Do not skip anything
AddPermission: 0, // Add nothing
PermissionControl: PerservePermission, // Just preserve permission
PermissionControl: PreservePermission, // Just preserve permission
Copy link
Owner

Choose a reason for hiding this comment

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

In order to keep backward compatibility, we can still keep PerservePermission with // Deprecated: marker.

Copy link
Owner

Choose a reason for hiding this comment

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

Accordingly, let's painfully respect PerservePermission but prioritize PreservePermission in other code blocks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants