-
Notifications
You must be signed in to change notification settings - Fork 0
/
atp.go
495 lines (410 loc) · 11.3 KB
/
atp.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// Package atp manages test environments for git-in-sync packages.
package atp
import (
"bufio"
"bytes"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"strings"
"sync"
"github.com/jychri/brf"
"github.com/jychri/tilde"
)
// private
const base = "~/tmpgis"
// model is an example repository
type model struct {
name string // gis-Ahead
remote string // jychri/gis-Ahead
dir string // /Users/jychri/tmpgis/atp/staging
}
// set m.dir to tdir + m.name or mdir + m.name.
// m.dir is used with cmd.Dir.
func (m *model) set(dir string) {
m.dir = path.Join(dir, m.name) // set m.dir
}
// make directory fresh
func (m *model) mkdirf() {
os.RemoveAll(m.dir) // verify rm -rf m.dir
os.MkdirAll(m.dir, 0766) // mkdir m.dir
}
// git init
func (m *model) init() {
cmd := exec.Command("git", "init") // git init
cmd.Dir = m.dir // set dir
cmd.Run() // run
}
// hub delete -y m.remote followed by hub create
func (m *model) hub() {
cmd := exec.Command("hub", "delete", "-y", m.remote) // verify hub delete
cmd.Dir = m.dir //
cmd.Run() //
cmd = exec.Command("hub", "create") // hub create
cmd.Dir = m.dir //
cmd.Run() //
}
// create file with lorem ipsum
func (m *model) create(name string) {
lorem := "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
data := []byte(lorem)
name = strings.Join([]string{name, ".md"}, "")
file := path.Join(m.dir, name)
if err := ioutil.WriteFile(file, data, 0777); err != nil {
log.Fatal(err)
}
}
// git add *
func (m *model) add() {
cmd := exec.Command("git", "add", "*") // git add
cmd.Dir = m.dir // set dir
cmd.Run() // run
}
// git commit -m $message
func (m *model) commit(message string) {
cmd := exec.Command("git", "commit", "-m", message) // git commit
cmd.Dir = m.dir // set dir
cmd.Run() // run
}
// git push -u origin master
func (m *model) push() {
cmd := exec.Command("git", "push", "-u", "origin", "master") // push -u origin master
cmd.Dir = m.dir // set dir
cmd.Run() // run
}
// set m.dir directly
func (m *model) direct(dir string) {
m.dir = dir
}
// hub clone $m.remote
func (m *model) clone() {
cmd := exec.Command("hub", "clone", m.remote) // hub clone
cmd.Dir = m.dir // set dir
cmd.Run() // run
}
// set model behind
func (m *model) behind() {
if !strings.Contains(m.name, "Behind") {
return
}
m.create("BEHIND")
m.add()
m.commit("'BEHIND' commit")
m.push()
}
// set model ahead
func (m *model) ahead() {
if !strings.Contains(m.name, "Ahead") {
return
}
m.create("AHEAD")
m.add()
m.commit("'AHEAD' commit")
}
// make model untracked
func (m *model) untracked() {
if !strings.Contains(m.name, "Untracked") {
return
}
m.create("UNTRACKED")
}
// overwrite README.md with fox text
func (m *model) overwrite() {
fox := "The sly brown fox jumped over the lazy dog."
data := []byte(fox)
file := path.Join(m.dir, "README.md")
if err := ioutil.WriteFile(file, data, 0777); err != nil {
log.Fatal(err)
}
}
// make model dirty
func (m *model) dirty() {
if !strings.Contains(m.name, "Dirty") {
return
}
m.overwrite()
}
// hub delete -y $m.remote
func (m *model) remove() {
cmd := exec.Command("hub", "delete", "-y", m.remote) // hub delete
cmd.Run() // run
}
type models []*model
// create model repos, configured to their names
func (ms models) startup(mdir string, tdir string) {
var wg sync.WaitGroup
for i := range ms {
wg.Add(1)
go func(m *model) {
defer wg.Done()
m.set(mdir) // set models dir
m.mkdirf() // make directory fresh
m.init() // git init
m.hub() // verify fresh repo on GitHub
m.create("README") // create readme
m.add() // add *
m.commit("Initial commit") // commit -m "Initial commit"
m.push() // push -u origin master
m.direct(tdir) // set tmpgis dir
m.clone() // clone to tmpgis dir
m.set(mdir) // set models dir
m.behind() // set *Behind* models behind origin master
m.set(tdir) // switch to tmpgis directory
m.ahead() // set *Ahead* models behind origin master
m.untracked() // make *Untracked* models untracked
m.dirty() // make *Dirty* models dirty
}(ms[i])
}
wg.Wait()
}
// remove all remotes
func (ms models) cleanup() {
var wg sync.WaitGroup
for i := range ms {
wg.Add(1)
go func(m *model) {
defer wg.Done()
m.remove() // remove GitHub remote
}(ms[i])
}
wg.Wait()
}
// create models from set options
func modeler(user string) (ms models) {
var tmps = []string{
"gis-Ahead",
"gis-Behind",
"gis-Dirty",
"gis-DirtyUntracked",
"gis-DirtyAhead",
"gis-DirtyBehind",
"gis-Untracked",
"gis-UntrackedAhead",
"gis-UntrackedBehind",
"gis-Complete",
}
for _, name := range tmps {
remote := path.Join(user, name)
m := new(model)
m.name = name
m.remote = remote
ms = append(ms, m)
}
return ms
}
func user() string {
path := tilde.Abs("~/.config/hub")
file, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
var user string // user set in ~/.config/hub
var token bool // token present in ~/.config/hub
for scanner.Scan() {
l := scanner.Text()
if match, err := brf.After(l, "- user:"); match != "" && err == nil {
user = match
}
if match, err := brf.After(l, "oauth_token:"); match != "" && err == nil {
token = true
}
}
if user == "" || token == false {
log.Fatalf("Error in ~/.config.hub")
}
return user
}
func paths(pkg string) (string, string) {
if pkg == "" {
log.Fatalf("pkg is empty")
}
base := tilde.Abs(base)
dir := path.Join(base, pkg)
os.RemoveAll(dir)
if err := os.MkdirAll(dir, 0777); err != nil {
log.Fatalf("Unable to create %v", dir)
}
return base, dir
}
// create subdirectories
func subdirs(dir string) (mdir string, tdir string) {
mdir = path.Join(dir, "models")
tdir = path.Join(dir, "tmpgis")
if err := os.MkdirAll(mdir, 0777); err != nil {
log.Fatalf("Unable to create %v", mdir)
}
if err := os.MkdirAll(tdir, 0777); err != nil {
log.Fatalf("Unable to create %v", tdir)
}
return mdir, tdir
}
// gisrc writes a gisrc to file, with data from jmap matching key k.
func gisrc(dir string, k string) string {
var json []byte
var ok bool
if json, ok = Jmap[k]; ok != true {
log.Fatalf("%v not found in jmap", k)
}
gisrc := path.Join(dir, "gisrc.json")
json = bytes.Replace(json, []byte("SETPATH"), []byte(dir), -1)
if err := ioutil.WriteFile(gisrc, json, 0777); err != nil {
log.Fatalf("Unable to write to %v (%v)", gisrc, err.Error())
}
return gisrc
}
// Public
// Jmap maps a string to JSON data as a byte slice.
var Jmap = map[string][]byte{
"recipes": []byte(`
{
"bundles": [{
"path": "SETPATH",
"zones": [{
"user": "hendricius",
"remote": "github",
"workspace": "recipes",
"repositories": [
"pizza-dough"
]
},
{
"user": "rochacbruno",
"remote": "github",
"workspace": "recipes",
"repositories": [
"vegan_recipes"
]
},
{
"user": "niw",
"remote": "github",
"workspace": "recipes",
"repositories": [
"ramen"
]
}
]
}]
}
`),
"tmpgis": []byte(`
{
"bundles": [{
"path": "SETPATH",
"zones": [{
"user": "jychri",
"remote": "github",
"workspace": "tmpgis",
"repositories": [
"gis-Ahead",
"gis-Behind",
"gis-Dirty",
"gis-DirtyUntracked",
"gis-DirtyAhead",
"gis-DirtyBehind",
"gis-Untracked",
"gis-UntrackedAhead",
"gis-UntrackedBehind",
"gis-Complete"
]
}
]
}]
}
`),
}
// Rmap maps a string to Results.
var Rmap = map[string]Results{
"recipes": {
{"hendricius", "github", "recipes", []string{"pizza-dough"}},
{"rochacbruno", "github", "recipes", []string{"vegan_recipes"}},
{"niw", "github", "recipes", []string{"ramen"}},
},
"tmpgis": {
{"jychri", "github", "tmpgis", []string{
"gis-Ahead",
"gis-Behind",
"gis-Dirty",
"gis-DirtyUntracked",
"gis-DirtyAhead",
"gis-DirtyBehind",
"gis-Untracked",
"gis-UntrackedAhead",
"gis-UntrackedBehind",
"gis-Complete",
}}},
}
// Result is the expected value for a zone.
type Result struct {
User, Remote, Workspace string
Repos []string
}
// Results collects Result structs.
type Results []Result
// Resulter returns Results from map Rmap.
func Resulter(key string) Results {
if _, ok := Rmap[key]; ok != true {
log.Fatalf("%v not found in rmap", key)
}
return Rmap[key]
}
// Setup creates a test environment at ~/tmpgis/$scope/, returning
// the absolute path of ~/tmpgis/$scope/gisrc.json and a cleanup function.
func Setup(scope string, key string) (string, func()) {
base, dir := paths(scope) // base and directory paths
gisrc := gisrc(dir, key) // write temporary gisrc
return gisrc, func() {
os.RemoveAll(base) // rm -rf base
}
}
// Hub creates a test environment at ~/tmpgis/$scope, returning
// the path of ~/tmpgis/$scope/gisrc.json and a cleanup function.
// Unlike Setup, Hub creates matching remotes on GitHub and sets repos
// ahead, behind, dirty, untracked or complete depending according to their names.
func Hub(scope string, key string) (string, func()) {
user := user() // read user ~/.config/hub
base, dir := paths(scope) // base and directory paths (return base for cleanup...)
gisrc := gisrc(dir, key) // create gisrc, return it's path
mdir, tdir := subdirs(dir) // create subdirectories models and tmp
ms := modeler(user) // create basic models, collect as models
ms.startup(mdir, tdir) // async startup
return gisrc, func() {
os.RemoveAll(base) // rm -rf base
ms.cleanup() // async remove all remotes
}
}
// Direct verifies ~/.gisrc.json, but does not validate its content.
// If no ~/.gisrc.json, Direct creates one and returns its absolute
// path with a cleanup function. If present, Direct returns its
// absolute path with a nil cleanup function.
func Direct(scope string, key string) (string, func()) {
var json []byte
var ok bool
if scope == "" {
log.Fatalf("pkg is empty")
}
if json, ok = Jmap[key]; ok != true {
log.Fatalf("%v not found in jmap", key)
}
tg := tilde.Abs("~/.gisrc.json") // test gisrc
tb := tilde.Abs("~/tmpgis") // test base
td := path.Join(tb, scope) // test directory
if _, err := os.Stat(tg); err == nil {
return "", func() {} // .gisrc.json exists, return
}
json = bytes.Replace(json, []byte("SETPATH"), []byte(td), -1)
if err := ioutil.WriteFile(tg, json, 0777); err != nil {
log.Fatalf("Unable to write to %v (%v)", tg, err.Error())
}
if err := os.MkdirAll(td, 0777); err != nil {
log.Fatalf("Unable to create %v", td)
}
return tg, func() {
os.Remove(tg)
os.RemoveAll(tb)
}
}