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
  | 
package main
import (
	"os"
	"path/filepath"
	"strings"
	"time"
	"go.wit.com/lib/gui/shell"
	"go.wit.com/lib/protobuf/forgepb"
	"go.wit.com/log"
)
// sent via -ldflags
var VERSION string
var BUILDTIME string
func main() {
	log.Info("go-clone version", VERSION, "built on", BUILDTIME)
	me = new(testMe)
	me.startPwd, _ = os.Getwd()
	// don't run this anywhere important
	me.forge = forgepb.Init()
	if me.forge.IsGoWork() {
		log.Info()
		log.Info("you can not run this here", me.forge.GetGoSrc())
		log.Info("you have a go.work file")
		log.Info("you must run this from a neutral location (not ~/go/src && not with a parent go.work file)")
		log.Info()
		os.Exit(-1)
	} else {
		if strings.HasPrefix(me.startPwd, me.forge.GetGoSrc()) {
			log.Info()
			log.Info("you can not run this here", me.forge.GetGoSrc())
			log.Info("you are already in ~/go/src")
			log.Info("you must run this from a neutral location (not ~/go/src && not with a parent go.work file)")
			log.Info()
			os.Exit(-1)
		}
	}
	// make sure this is empty
	me.forge = nil
	me.testDir1 = "goclonetest"
	fullRun(me.testDir1)
	me.testDir2 = "goclonetesttest"
	fullRun(me.testDir2)
	// try this a second time
	// os.Mkdir(me.testDir2, 0755)
}
func fullRun(testDir string) {
	os.Chdir(me.startPwd)
	if argv.Force {
		log.Info("removing", testDir)
		shell.Run([]string{"rm", "-rf", testDir})
	}
	if shell.IsDir(filepath.Join(me.startPwd, testDir)) {
		log.Info("you must remove", testDir)
		os.Exit(-1)
	}
	testDir = "goclonetest"
	os.Mkdir(testDir, 0755)
	os.Chdir(testDir)
	me.forge = forgepb.Init()
	wd := filepath.Join(me.startPwd, testDir)
	runStrict(wd, []string{"touch", "go.work"})
	os.Chdir(me.startPwd)
	prepBinary(testDir, "go.wit.com/apps/go-clone")
	prepBinary(testDir, "go.wit.com/apps/autogenpb")
	prepBinary(testDir, "go.wit.com/apps/forge")
	prepBinary(testDir, "go.wit.com/apps/utils/wit-utils")
	buildBinary(testDir, "go.wit.com/apps/go-clone")
	buildBinary(testDir, "go.wit.com/apps/autogenpb")
	buildBinary(testDir, "go.wit.com/apps/forge")
	// buildBinary(testDir, "go.wit.com/apps/utils/wit-utils")
	installBinary(testDir, "go.wit.com/apps/go-clone")
	installBinary(testDir, "go.wit.com/apps/autogenpb")
	installBinary(testDir, "go.wit.com/apps/forge")
	// installBinary(testDir, "go.wit.com/apps/utils/wit-utils")
}
func prepBinary(testDir string, gopath string) {
	wd := filepath.Join(me.startPwd, testDir)
	time.Sleep(time.Second)
	// this is probably why, although ENV is great, it might
	// be a terrible idea? notsure... how often does this happen?
	// this is a test app. does this really ever happen in the real world?
	// switching GOSRC paths in the middle of doing things? It probably
	// shouldn't be supported or work the way it does. which, in this
	// case, breaks this test app
	runStrict(wd, []string{"sync"})
	runStrict(wd, []string{"/home/jcarr/go/bin/go-clone", "--recursive", gopath, "--work", "--ignore"})
	// runStrict(wd, []string{"/home/jcarr/go/bin/go-clone", "--work"})
}
func buildBinary(testDir string, gopath string) {
	me.forge = forgepb.Init()
	if err := me.forge.ConfigSave(); err != nil {
		log.Info("crapnuts. forge.ConfigSave() failed", err)
	}
	/*
		repos := me.forge.Repos.SortByGoPath()
		for repos.Scan() {
			repo := repos.Next()
			log.Info("go.work repo (hopefully):", repo.GoPath, repo.FullPath)
		}
	*/
}
func installBinary(testDir string, gopath string) {
	wd := filepath.Join(me.startPwd, testDir, gopath)
	repos := me.forge.Repos.SortByGoPath()
	for repos.Scan() {
		repo := repos.Next()
		log.Info("go.work repo (hopefully):", repo.GoPath, repo.FullPath, repo.RepoType())
	}
	runStrict(wd, []string{"go-clone", "--install", gopath, "--ignore"})
	os.Exit(-1)
}
func runStrict(wd string, cmd []string) {
	var err error
	if err = os.Chdir(wd); err != nil {
		log.Info("cd", "wd", "failed", err)
		os.Exit(-1)
	}
	log.Info(wd, "running:", wd, cmd)
	// result := shell.Run(cmd)
	result := shell.RunRealtime(cmd)
	if result.Error != nil {
		log.Info("cmd failed", wd, cmd, err)
		/*
			for i, line := range result.Stdout {
				log.Info("STDOUT:", i, line)
			}
			for i, line := range result.Stderr {
				log.Info("STDERR:", i, line)
			}
		*/
		os.Exit(-1)
	}
	if result.Exit != 0 {
		log.Info("cmd failed", wd, cmd)
		os.Exit(-1)
	}
	for i, line := range result.Stdout {
		log.Info(i, line)
	}
}
  |