-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfiles.go
97 lines (87 loc) · 2.01 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
package main
import "os"
import "fmt"
import "path"
import "bufio"
import "strings"
import "io/ioutil"
import "path/filepath"
import "github.com/bnclabs/golog"
func readlines(filepath string) ([]string, error) {
fd, err := os.Open(filepath)
if err != nil {
log.Errorf("%v\n", err)
return nil, err
}
defer fd.Close()
scanner := bufio.NewScanner(fd)
scanner.Split(bufio.ScanLines)
lines := []string{}
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, nil
}
func coveringjournals(cwd string) (files []string) {
files = []string{}
log.Debugf("gathering journals from %q\n", cwd)
dirs := parentpaths(cwd, []string{})[1:]
for _, dir := range dirs {
entries, err := ioutil.ReadDir(dir)
if err != nil {
fmt.Printf("%v\n", err)
continue
}
for _, entry := range entries {
if entry.IsDir() {
continue
}
filename := entry.Name()
ok := filename == "ledgerrc"
ok = ok || filename == ".ledgerrc"
ok = ok || path.Ext(filename) == ".ldg"
if ok {
includefile := path.Join(dir, filename)
log.Debugf("auto including %q\n", includefile)
files = append(files, includefile)
}
}
}
return files
}
func listjournals(cwd string) ([]string, error) {
files := []string{}
items, err := ioutil.ReadDir(cwd)
if err != nil {
log.Errorf("%v\n", err)
return nil, err
}
for _, item := range items {
if path.Ext(item.Name()) == ".ldg" {
files = append(files, filepath.Join(cwd, item.Name()))
}
}
return files, nil
}
func findjournals(cwd string) ([]string, error) {
files := []string{}
filepath.Walk(
cwd,
func(pathdir string, info os.FileInfo, err error) error {
if path.Ext(info.Name()) == ".ldg" {
files = append(files, filepath.Join(pathdir, info.Name()))
}
return nil
},
)
return files, nil
}
func parentpaths(dirpath string, acc []string) (dirs []string) {
dir, _ := path.Split(dirpath)
if dir != "" {
acc = append(acc, dirpath)
dir = strings.TrimRight(dir, string(os.PathSeparator))
return parentpaths(dir, acc)
}
return acc
}