-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDirectory.go
More file actions
95 lines (81 loc) · 1.84 KB
/
Directory.go
File metadata and controls
95 lines (81 loc) · 1.84 KB
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
package bugs
import (
"os"
"regexp"
"strings"
"time"
)
func GetRootDir() Directory {
dir := os.Getenv("PMIT")
if dir != "" {
return Directory(dir)
}
wd, _ := os.Getwd()
if dirinfo, err := os.Stat(wd + "/issues"); err == nil && dirinfo.IsDir() {
return Directory(wd)
}
// There's no environment variable and no issues
// directory, so walk up the tree until we find one
pieces := strings.Split(wd, "/")
for i := len(pieces); i > 0; i -= 1 {
dir := strings.Join(pieces[0:i], "/")
if dirinfo, err := os.Stat(dir + "/issues"); err == nil && dirinfo.IsDir() {
return Directory(dir)
}
}
return ""
}
func GetIssuesDir() Directory {
root := GetRootDir()
if root == "" {
return root
}
return GetRootDir() + "/issues/"
}
type Directory string
func (d Directory) GetShortName() Directory {
pieces := strings.Split(string(d), "/")
return Directory(pieces[len(pieces)-1])
}
func (d Directory) ToTitle() string {
multidash := regexp.MustCompile("([_]*)-([-_]*)")
dashReplacement := strings.Replace(string(d), " ", "/", -1)
return multidash.ReplaceAllStringFunc(dashReplacement, func(match string) string {
if match == "-" {
return " "
}
if strings.Count(match, "_") == 0 {
return match[1:]
}
return strings.Replace(match, "_", " ", -1)
})
}
func (d Directory) LastModified() time.Time {
var t time.Time
stat, err := os.Stat(string(d))
if err != nil {
panic("Directory " + string(d) + " is not a directory.")
}
if stat.IsDir() == false {
return stat.ModTime()
}
dir, _ := os.Open(string(d))
files, _ := dir.Readdir(-1)
if len(files) == 0 {
t = stat.ModTime()
}
for _, file := range files {
if file.IsDir() {
mtime := (d + "/" + Directory(file.Name())).LastModified()
if mtime.After(t) {
t = mtime
}
} else {
mtime := file.ModTime()
if mtime.After(t) {
t = mtime
}
}
}
return t
}