-
Notifications
You must be signed in to change notification settings - Fork 61
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
Add RmdirContents API in the Fileystem v2alpha1 API Group #186
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
|
@@ -18,6 +19,7 @@ type API interface { | |
PathValid(path string) (bool, error) | ||
Mkdir(path string) error | ||
Rmdir(path string, force bool) error | ||
RmdirContents(path string) error | ||
CreateSymlink(oldname string, newname string) error | ||
IsSymlink(path string) (bool, error) | ||
} | ||
|
@@ -78,6 +80,29 @@ func (filesystemAPI) Rmdir(path string, force bool) error { | |
return os.Remove(path) | ||
} | ||
|
||
// RmdirContents removes the contents of a directory with `os.RemoveAll` | ||
func (filesystemAPI) RmdirContents(path string) error { | ||
dir, err := os.Open(path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://stackoverflow.com/a/52685448 a reference of implementation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I took the same implementation as LVP for linux |
||
if err != nil { | ||
return err | ||
} | ||
defer dir.Close() | ||
|
||
files, err := dir.Readdirnames(-1) | ||
if err != nil { | ||
return err | ||
} | ||
for _, file := range files { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you didn't implement force option here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks, fixed |
||
candidatePath := filepath.Join(path, file) | ||
err = os.RemoveAll(candidatePath) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// CreateSymlink creates newname as a symbolic link to oldname. | ||
func (filesystemAPI) CreateSymlink(oldname, newname string) error { | ||
return os.Symlink(oldname, newname) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe add a few more words to mention contents are all the files and sub dirs under the dir. The directory itself will not be deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added: