Skip to content

Commit

Permalink
modify: refactoring dirwalk implementaion
Browse files Browse the repository at this point in the history
  • Loading branch information
d-tsuji committed Apr 25, 2020
1 parent 4253057 commit 2049dd3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@
# Executable Binary
qiisync
qiisync.exe

# Go test coverage
c.out
coverage.html
26 changes: 12 additions & 14 deletions broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,10 @@ func (b *Broker) fetchRemoteItemsPerPage(page int) ([]*Article, bool, error) {
// FetchLocalArticles searches base_dir of local filesystem and extracts articles.
func (b *Broker) FetchLocalArticles() (articles map[string]*Article, err error) {
articles = make(map[string]*Article)
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("recoverd when dirwalk(%s): %v", b.baseDir(), r)
}
}()
fnameList := dirwalk(b.baseDir())
fnameList, err := dirwalk(b.baseDir())
if err != nil {
return nil, fmt.Errorf("dirwalk %s: %w", fnameList, err)
}
for i := range fnameList {
a, err := ArticleFromFile(fnameList[i])
if err != nil {
Expand All @@ -144,26 +142,26 @@ func (b *Broker) FetchLocalArticles() (articles map[string]*Article, err error)
return articles, nil
}

func dirwalk(dir string) []string {
err := os.MkdirAll(dir, 0755)
if err != nil {
panic(err)
}
func dirwalk(dir string) ([]string, error) {
files, err := ioutil.ReadDir(dir)
if err != nil {
panic(err)
return nil, fmt.Errorf("read dir: %w", err)
}

var paths []string
for _, file := range files {
if file.IsDir() {
paths = append(paths, dirwalk(filepath.Join(dir, file.Name()))...)
p, err := dirwalk(filepath.Join(dir, file.Name()))
if err != nil {
return nil, fmt.Errorf("dirwalk %s: %w", filepath.Join(dir, file.Name()), err)
}
paths = append(paths, p...)
continue
}
paths = append(paths, filepath.Join(dir, file.Name()))
}

return paths
return paths, nil
}

// NewRequest is a testable NewRequest that wraps http.NewRequest.
Expand Down
5 changes: 4 additions & 1 deletion broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,10 @@ func Test_dirwalk(t *testing.T) {
f, _ = os.Create(filepath.Join(tempDir, "dir_c", "file_c"))
f.Close()

got := dirwalk(baseDir)
got, err := dirwalk(baseDir)
if err != nil {
t.Errorf("dirwalk: %v", err)
}
want := []string{
filepath.Join(tempDir, "dir_b", "file_b"),
filepath.Join(tempDir, "dir_c", "file_c"),
Expand Down

0 comments on commit 2049dd3

Please sign in to comment.