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

169 lines
4.4 KiB
Go
Raw Normal View History

2024-12-07 16:48:45 -06:00
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()
2024-12-15 08:47:03 -06:00
// don't run this anywhere important
2024-12-07 16:48:45 -06:00
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)
}
}
2024-12-15 08:47:03 -06:00
// make sure this is empty
me.forge = nil
2024-12-07 16:48:45 -06:00
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"})
2024-12-15 15:52:34 -06:00
runStrict(wd, []string{"/home/jcarr/go/bin/go-clone", "--recursive", gopath, "--work", "--ignore"})
2024-12-15 08:47:03 -06:00
// runStrict(wd, []string{"/home/jcarr/go/bin/go-clone", "--work"})
2024-12-07 16:48:45 -06:00
}
func buildBinary(testDir string, gopath string) {
os.Unsetenv("FORGE_CONFIG")
os.Unsetenv("FORGE_GOSRC")
me.forge = forgepb.Init()
2024-12-15 20:55:49 -06:00
if err := me.forge.ConfigSave(); err != nil {
2024-12-07 16:48:45 -06:00
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())
}
2024-12-15 15:52:34 -06:00
runStrict(wd, []string{"go-clone", "--install", gopath, "--ignore"})
2024-12-07 16:48:45 -06:00
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)
2024-12-15 08:47:03 -06:00
if result.Error != nil {
2024-12-07 16:48:45 -06:00
log.Info("cmd failed", wd, cmd, err)
2024-12-15 08:47:03 -06:00
/*
for i, line := range result.Stdout {
log.Info("STDOUT:", i, line)
}
for i, line := range result.Stderr {
log.Info("STDERR:", i, line)
}
*/
2024-12-07 16:48:45 -06:00
os.Exit(-1)
}
2024-12-15 08:47:03 -06:00
if result.Exit != 0 {
log.Info("cmd failed", wd, cmd)
2024-12-07 16:48:45 -06:00
os.Exit(-1)
}
for i, line := range result.Stdout {
log.Info(i, line)
}
}