wit-utils/go-clone-test/main.go

169 lines
4.4 KiB
Go

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)
os.Unsetenv("FORGE_CONFIG")
os.Unsetenv("FORGE_GOSRC")
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
os.Unsetenv("FORGE_CONFIG")
os.Unsetenv("FORGE_GOSRC")
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) {
os.Unsetenv("FORGE_CONFIG")
os.Unsetenv("FORGE_GOSRC")
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)
}
}