Skip to content

Commit 98d3be7

Browse files
committed
single vs recursive for dirspath and filespath. readme update
1 parent d03ceb4 commit 98d3be7

File tree

3 files changed

+62
-15
lines changed

3 files changed

+62
-15
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# essentials - aah framework
22

3-
[![Build Status](https://travis-ci.org/go-aah/essentials.svg?branch=master)](https://travis-ci.org/go-aah/essentials) [![codecov](https://codecov.io/gh/go-aah/essentials/branch/master/graph/badge.svg)](https://codecov.io/gh/go-aah/essentials/branch/master) [![Go Report Card](https://goreportcard.com/badge/aahframework.org/essentials)](https://goreportcard.com/report/aahframework.org/essentials) [![GoDoc](https://godoc.org/aahframework.org/essentials?status.svg)](https://godoc.org/aahframework.org/essentials) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
3+
[![Build Status](https://travis-ci.org/go-aah/essentials.svg?branch=master)](https://travis-ci.org/go-aah/essentials) [![codecov](https://codecov.io/gh/go-aah/essentials/branch/master/graph/badge.svg)](https://codecov.io/gh/go-aah/essentials/branch/master) [![Go Report Card](https://goreportcard.com/badge/aahframework.org/essentials)](https://goreportcard.com/report/aahframework.org/essentials)
4+
[![Version](https://img.shields.io/badge/version-0.1-blue.svg)](https://github.com/go-aah/essentials/releases/latest) [![GoDoc](https://godoc.org/aahframework.org/essentials?status.svg)](https://godoc.org/aahframework.org/essentials) [![License](https://img.shields.io/github/license/go-aah/essentials.svg)](LICENSE)
45

56
***v0.1 [released](https://github.com/go-aah/essentials/releases/latest) and tagged on Jan 22, 2017***
67

@@ -11,6 +12,7 @@
1112
* os
1213
* reflect
1314
* string
15+
* archive (zip)
1416

1517
*`essentials` developed for aah framework. However, it's an independent library, can be used separately with any `Go` language project. Feel free to use it.*
1618

filepath.go

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"bytes"
99
"fmt"
1010
"io"
11+
"io/ioutil"
1112
"os"
1213
"path/filepath"
1314
"strings"
@@ -267,24 +268,56 @@ func DeleteFiles(files ...string) (errs []error) {
267268
}
268269

269270
// DirsPath method returns all directories absolute path from given base path recursively.
270-
func DirsPath(basePath string) (pdirs []string, err error) {
271-
err = Walk(basePath, func(srcPath string, info os.FileInfo, err error) error {
272-
if info.IsDir() {
273-
pdirs = append(pdirs, srcPath)
271+
func DirsPath(basePath string, recursive bool) (pdirs []string, err error) {
272+
if recursive {
273+
err = Walk(basePath, func(srcPath string, info os.FileInfo, err error) error {
274+
if info.IsDir() {
275+
pdirs = append(pdirs, srcPath)
276+
}
277+
return nil
278+
})
279+
return
280+
}
281+
282+
var list []os.FileInfo
283+
list, err = ioutil.ReadDir(basePath)
284+
if err != nil {
285+
return
286+
}
287+
288+
for _, v := range list {
289+
if v.IsDir() {
290+
pdirs = append(pdirs, filepath.Join(basePath, v.Name()))
274291
}
275-
return nil
276-
})
292+
}
293+
277294
return
278295
}
279296

280297
// FilesPath method returns all files absolute path from given base path recursively.
281-
func FilesPath(basePath string) (files []string, err error) {
282-
err = Walk(basePath, func(srcPath string, info os.FileInfo, err error) error {
283-
if !info.IsDir() {
284-
files = append(files, srcPath)
298+
func FilesPath(basePath string, recursive bool) (files []string, err error) {
299+
if recursive {
300+
err = Walk(basePath, func(srcPath string, info os.FileInfo, err error) error {
301+
if !info.IsDir() {
302+
files = append(files, srcPath)
303+
}
304+
return nil
305+
})
306+
return
307+
}
308+
309+
var list []os.FileInfo
310+
list, err = ioutil.ReadDir(basePath)
311+
if err != nil {
312+
return
313+
}
314+
315+
for _, v := range list {
316+
if !v.IsDir() {
317+
files = append(files, filepath.Join(basePath, v.Name()))
285318
}
286-
return nil
287-
})
319+
}
320+
288321
return
289322
}
290323

@@ -294,5 +327,11 @@ func StripExt(name string) string {
294327
if IsStrEmpty(name) {
295328
return name
296329
}
297-
return name[:strings.IndexByte(name, '.')]
330+
331+
idx := strings.IndexByte(name, '.')
332+
if idx > 0 {
333+
return name[:idx]
334+
}
335+
336+
return name
298337
}

filepath_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,18 @@ func TestDirPaths(t *testing.T) {
235235
_ = MkDirAll(path21, 0755)
236236
_ = MkDirAll(path22, 0755)
237237

238-
dirs, err := DirsPath(join(testdataPath, "dirpaths"))
238+
dirs, err := DirsPath(join(testdataPath, "dirpaths"), true)
239239
assert.FailOnError(t, err, "unable to get directory list")
240240
assert.True(t, IsSliceContainsString(dirs, path1))
241241
assert.True(t, IsSliceContainsString(dirs, path11))
242242
assert.True(t, IsSliceContainsString(dirs, path12))
243243
assert.True(t, IsSliceContainsString(dirs, path21))
244244
assert.True(t, IsSliceContainsString(dirs, path22))
245245
assert.False(t, IsSliceContainsString(dirs, join(path22, "not-exists")))
246+
247+
dirs, err = DirsPath(join(testdataPath, "dirpaths", "level1"), false)
248+
assert.FailOnError(t, err, "unable to get directory list")
249+
assert.True(t, IsSliceContainsString(dirs, path11))
250+
assert.True(t, IsSliceContainsString(dirs, path12))
251+
assert.False(t, IsSliceContainsString(dirs, join(path22, "not-exists")))
246252
}

0 commit comments

Comments
 (0)