-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
229 lines (197 loc) · 5.93 KB
/
main.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/shu-go/clise"
"github.com/shu-go/gli/v2"
"github.com/shu-go/shortcut"
)
type globalCmd struct {
Verbose bool `help:"標準エラーに追加情報を出力します"`
Target string `help:"対象のルートディレクトリーを指定します"`
Link string `help:"ショートカットを作成するディレクトリーを指定します"`
Format string `default:":tdate:_:pabb:_:tname:" help:"作成されるショートカットのファイル名規則を指定します"`
Ignores string `default:"!#@" help:"走査対象外のディレクトリー名のプレフィックスを指定します"`
}
func (c globalCmd) Before() error {
if len(c.Target) == 0 {
return fmt.Errorf("対象のディレクトリが指定されていません --target")
}
if len(c.Link) == 0 {
return fmt.Errorf("ショートカット作成先のディレクトリが指定されていません --link")
}
if len(c.Format) == 0 {
return fmt.Errorf("ショートカットのファイル名規則が空白になっています --format")
}
return nil
}
func (c globalCmd) Run() error {
targetDir := strings.Replace(c.Target, `\`, `/`, -1)
linkDir := strings.Replace(c.Link, `\`, `/`, -1)
// 既存のショートカットを全て削除。後で作り直すので。
for _, lnk := range listLinkFiles(linkDir, c.Ignores) {
if err := os.Remove(lnk); err != nil {
println(err.Error())
}
}
// ショートカット作成対象を格納しているディレクトリ
for _, p := range listProjectDirs(targetDir, c.Ignores) {
pabb, pname := projectAbbAndName(p)
println(pabb, pname)
for _, t := range listTaskDirs(p, c.Ignores) {
tname, tdate := taskNameAndDate(t)
lnkName := linkName(c.Format, pabb, pname, tname, tdate)
println(t, "=>", lnkName)
s := shortcut.New(t)
if err := s.Save(linkDir + "/" + lnkName + ".lnk"); err != nil {
println(err.Error())
}
}
}
return nil
}
func main() {
app := gli.NewWith(&globalCmd{})
app.Name = "taskol"
app.Version = "0.2.0"
app.Copyright = "(C) 2018 Shuhei Kubota"
app.Desc = "仕掛中の作業フォルダへのショートカットを作るツール"
app.Usage = `taskol --target=/path/to/work/root --link=/path/to/link/root`
err := app.Run(os.Args)
if err != nil {
os.Exit(-1)
}
}
func linkName(linkFormat, pabb, pname, tname string, tdate *time.Time) string {
result := linkFormat
result = strings.Replace(result, ":pabb:", pabb, -1)
result = strings.Replace(result, ":pname:", pname, -1)
result = strings.Replace(result, ":tname:", tname, -1)
if tdate == nil {
result = strings.Replace(result, ":tdate:", "", -1)
result = strings.Replace(result, ":tdate-:", "", -1)
result = strings.Replace(result, ":tdate年月日:", "", -1)
} else {
result = strings.Replace(result, ":tdate:", fmt.Sprintf("%04d%02d%02d", tdate.Year(), tdate.Month(), tdate.Day()), -1)
result = strings.Replace(result, ":tdate-:", fmt.Sprintf("%04d-%02d-%02d", tdate.Year(), tdate.Month(), tdate.Day()), -1)
result = strings.Replace(result, ":tdate年月日:", fmt.Sprintf("%04d年%02d月%02d日", tdate.Year(), tdate.Month(), tdate.Day()), -1)
}
return result
}
func taskNameAndDate(dir string) (name string, date *time.Time) {
compoSepPtn := regexp.MustCompile(`_|\(|\)`)
datePtn := regexp.MustCompile(`(\d{2,4})-?(\d{2})-?(\d{2})`)
base := filepath.Base(dir)
var withoutPrefix string
if strings.HasPrefix(base, "t_") {
withoutPrefix = base[2:len(base)]
} else {
withoutPrefix = base
}
compos := compoSepPtn.Split(withoutPrefix, -1)
for _, c := range compos {
if len(c) == 0 {
continue
}
if subs := datePtn.FindStringSubmatch(c); len(subs) >= 4 {
y, _ := strconv.Atoi(subs[1])
if y < 100 {
y += 2000
}
m, _ := strconv.Atoi(subs[2])
d, _ := strconv.Atoi(subs[3])
dt := time.Date(y, time.Month(m), d, 0, 0, 0, 0, time.UTC)
date = &dt
} else {
if len(name) != 0 {
name += "_"
}
name += c
}
}
return name, date
}
func projectAbbAndName(dir string) (abb, name string) {
compoSepPtn := regexp.MustCompile(`_|\(|\)`)
abbPtn := regexp.MustCompile(`[[:alnum:]]`)
base := filepath.Base(dir)
compos := compoSepPtn.Split(base, -1)
for _, c := range compos {
if len(c) == 0 {
continue
}
if abbPtn.MatchString(c) {
abb = c
if len(name) == 0 {
name = c
}
} else {
if len(abb) == 0 {
abb = c
}
name = c
}
}
return abb, name
}
func isDir(p string) bool {
info, err := os.Lstat(p)
if err != nil {
return false
}
return info.IsDir()
}
func shouldBeIgnored(p, ignores string) bool {
base := filepath.Base(p)
// compare first runes
for _, b := range base {
for _, c := range ignores {
if b == c {
return true
}
}
break
}
return false
}
func listLinkFiles(baseDir, ignores string) []string {
files, err := filepath.Glob(baseDir + "/*.lnk") // Glob needs separators be /
if err != nil {
return nil
}
clise.Filter(
&files,
func(i int) bool { return !isDir(files[i]) },
func(i int) bool { return !shouldBeIgnored(files[i], ignores) },
)
return files
}
func listProjectDirs(baseDir, ignores string) []string {
dirs, err := filepath.Glob(baseDir + "/*") // Glob needs separators be /
if err != nil {
return nil
}
clise.Filter(
&dirs,
func(i int) bool { return isDir(dirs[i]) },
func(i int) bool { return !shouldBeIgnored(dirs[i], ignores) },
)
return dirs
}
func listTaskDirs(prjDir, ignores string) []string {
dirs, err := filepath.Glob(prjDir + "/t_*") // Glob needs separators be /
if err != nil {
return nil
}
clise.Filter(
&dirs,
func(i int) bool { return isDir(dirs[i]) },
func(i int) bool { return !shouldBeIgnored(dirs[i], ignores) },
)
return dirs
}