1
0
mirror of https://github.com/StackExchange/dnscontrol.git synced 2024-05-11 05:55:12 +00:00
This commit is contained in:
Tom Limoncelli
2020-08-26 13:45:02 -04:00
committed by GitHub
parent 57d9cfa356
commit d6dd13820f
5 changed files with 21 additions and 21 deletions

View File

@@ -120,8 +120,8 @@ func require(call otto.FunctionCall) otto.Value {
func listFiles(call otto.FunctionCall) otto.Value {
// Check amount of arguments provided
if ! (len(call.ArgumentList) >= 1 && len(call.ArgumentList) <= 3) {
throw(call.Otto, "glob requires at least one argument: folder (string). " +
if !(len(call.ArgumentList) >= 1 && len(call.ArgumentList) <= 3) {
throw(call.Otto, "glob requires at least one argument: folder (string). "+
"Optional: recursive (bool) [true], fileExtension (string) [.js]")
}
@@ -143,8 +143,8 @@ func listFiles(call otto.FunctionCall) otto.Value {
}
// Second: Recursive?
var recursive bool = true;
if call.Argument(1).IsDefined() && ! call.Argument(1).IsNull() {
var recursive bool = true
if call.Argument(1).IsDefined() && !call.Argument(1).IsNull() {
if call.Argument(1).IsBoolean() {
recursive, _ = call.Argument(1).ToBoolean() // If it should be recursive
} else {
@@ -153,11 +153,11 @@ func listFiles(call otto.FunctionCall) otto.Value {
}
// Third: File extension filter.
var fileExtension string = ".js";
if call.Argument(2).IsDefined() && ! call.Argument(2).IsNull() {
var fileExtension string = ".js"
if call.Argument(2).IsDefined() && !call.Argument(2).IsNull() {
if call.Argument(2).IsString() {
fileExtension = call.Argument(2).String() // Which file extension to filter for.
if ! strings.HasPrefix(fileExtension, ".") {
if !strings.HasPrefix(fileExtension, ".") {
// If it doesn't start with a dot, probably user forgot it and we do it instead.
fileExtension = "." + fileExtension
}
@@ -169,14 +169,14 @@ func listFiles(call otto.FunctionCall) otto.Value {
// Now we're doing the actual work: Listing files.
// Folders are ending with a slash. Can be identified later on from the user with JavaScript.
// Additionally, when more smart logic required, user can use regex in JS.
files := make([]string, 0) // init files list
files := make([]string, 0) // init files list
dirClean := filepath.Clean(dir) // let's clean it here once, instead of over-and-over again within loop
err := filepath.Walk(dir, func(path string, fi os.FileInfo, err error) error {
// quick fix to get it working on windows, as it returns paths with double-backslash, what usually
// require() doesn't seem to handle well. For the sake of compatibility (and because slash looks nicer),
// we simply replace "\\" to "/" using filepath.ToSlash()..
path = filepath.ToSlash(filepath.Clean(path)) // convert to slashes for directories
if ! recursive && fi.IsDir() {
if !recursive && fi.IsDir() {
// If recursive is disabled, it is a dir what we're processing, and the path is different
// than specified, we're apparently in a different folder. Therefore: Skip it.
// So: Why this way? Because Walk() is always recursive and otherwise would require a complete