forked from graniticio/granitic
/
files.go
108 lines (76 loc) · 2.34 KB
/
files.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2016 Granitic. All rights reserved.
// Use of this source code is governed by an Apache 2.0 license that can be found in the LICENSE file at the root of this project.
package config
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
// FindConfigFilesInDir finds all files with a .json extension in the supplied directory path, recursively checking
// sub-directories. Note that each directory's contents are examined and added to the list of files lexicographically,
// so any files in a sub-directory 'b' would appear in the resulting list of files before 'c.json'
func FindConfigFilesInDir(dirPath string) ([]string, error) {
contents, err := ioutil.ReadDir(dirPath)
files := make([]string, 0)
if err != nil {
return nil, err
}
for _, info := range contents {
fileName := info.Name()
if info.Mode().IsDir() {
dp := filepath.Join(dirPath, fileName)
fmt.Println(dp)
if sub, err := FindConfigFilesInDir(dp); err != nil {
return nil, err
} else {
files = append(files, sub...)
}
} else if filepath.Ext(fileName) == ".json" {
f := filepath.Join(dirPath, fileName)
files = append(files, f)
}
}
return files, nil
}
// FileListFromPath takes a string that could represent a path to a directory
// or a path to a file and returns a list of file paths. If the path is to directory,
// any files in that directory are included in the result. Any sub-directories are recursively entered.
func FileListFromPath(path string) ([]string, error) {
files := make([]string, 0)
file, err := os.Open(path)
if err != nil {
err := errors.New("Unable to open file/dir " + path)
return files, err
}
defer file.Close()
fileInfo, err := file.Stat()
if err != nil {
err := errors.New("Unable to obtain file info for file/dir " + path)
return files, err
}
if fileInfo.IsDir() {
contents, err := ioutil.ReadDir(path)
if err != nil {
err := errors.New("Unable to read contents of directory " + path)
return files, err
}
for _, info := range contents {
fileName := info.Name()
p := filepath.Join(path, fileName)
if info.IsDir() {
if sf, err := FileListFromPath(p); err != nil {
return nil, err
} else {
files = append(files, sf...)
}
} else {
files = append(files, p)
}
}
} else {
files = append(files, file.Name())
}
return files, nil
}