wit-test/main.go

191 lines
3.9 KiB
Go
Raw Normal View History

2025-02-01 11:38:22 -06:00
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0
2024-12-24 04:47:58 -06:00
package main
import (
2025-02-20 09:39:31 -06:00
"debug/buildinfo"
2024-12-24 04:47:58 -06:00
"fmt"
"os"
"path/filepath"
"unicode"
2025-01-07 17:17:01 -06:00
"go.wit.com/dev/alexflint/arg"
2025-02-20 09:39:31 -06:00
"go.wit.com/lib/gui/shell"
2024-12-24 04:47:58 -06:00
"go.wit.com/lib/protobuf/forgepb"
"go.wit.com/lib/protobuf/gitpb"
2025-02-22 07:17:13 -06:00
"go.wit.com/lib/protobuf/zoopb"
2025-02-20 09:39:31 -06:00
"go.wit.com/log"
2024-12-24 04:47:58 -06:00
)
// sent via -ldflags
var VERSION string
var BUILDTIME string
var failed map[*gitpb.Repo]string
var state map[*gitpb.Repo]string
var debnames map[*gitpb.Repo]string
func main() {
me = new(autoType)
2025-01-07 17:17:01 -06:00
me.argpp = arg.MustParse(&argv)
if argv.Bash {
argv.doBash()
os.Exit(0)
}
if len(argv.BashAuto) != 0 {
argv.doBashAuto()
os.Exit(0)
}
2025-02-20 09:39:31 -06:00
dumpDebug()
2024-12-24 04:47:58 -06:00
failed = make(map[*gitpb.Repo]string)
state = make(map[*gitpb.Repo]string)
debnames = make(map[*gitpb.Repo]string)
// load the ~/.config/forge/ config
me.forge = forgepb.Init()
2025-02-22 07:17:13 -06:00
me.machine = new(zoopb.Machine)
if err := me.machine.ConfigLoad(); err != nil {
log.Info("zoopb.ConfigLoad() failed", err)
}
me.machine.InitWit()
2025-01-07 17:17:01 -06:00
if argv.Clone != nil {
if argv.RepoMap != "" {
repomap(argv.RepoMap)
okExit("")
}
2024-12-24 04:47:58 -06:00
}
2025-02-01 11:22:46 -06:00
if argv.WITCOM {
doWITCOM()
okExit("")
}
2025-01-07 17:17:01 -06:00
if argv.Upgrade != nil {
2025-01-20 01:40:14 -06:00
doAptUpgrade()
2024-12-24 04:47:58 -06:00
}
2025-01-07 17:17:01 -06:00
if argv.ListPkgs != nil {
2025-01-20 01:40:14 -06:00
doAptList()
2024-12-24 04:47:58 -06:00
log.DaemonMode(true)
defer log.DaemonMode(false)
fmt.Println("Installed Packages:")
2025-02-22 07:17:13 -06:00
loop := me.machine.Wit.SortByName()
2024-12-24 04:47:58 -06:00
for loop.Scan() {
p := loop.Next()
var end string
var version string
if me.forge.Config.IsPrivate(p.Name) {
end += "(private) "
}
2025-02-22 07:17:13 -06:00
if actualp := me.machine.FindVersion(p.Name, p.Version); actualp != nil {
2024-12-24 04:47:58 -06:00
// end += "(version match) "
} else {
end += "(version mismatch) " + actualp.Version + " " + version + " "
}
2025-02-22 07:17:13 -06:00
if actualp := me.machine.FindInstalledByName(p.Name); actualp != nil {
2024-12-24 04:58:44 -06:00
if p.Version != actualp.Version {
end += "(installed " + actualp.Version + " vs " + p.Version + ") "
} else {
end += "(installed ok) "
}
2024-12-24 04:47:58 -06:00
version = actualp.Version
}
if me.forge.Config.IsReadOnly(p.Name) {
// end += " (readonly) "
} else {
end += "(writable) "
}
log.Printf("%-30s %-8s %s\n", p.Name, p.Version, end) // p.PkgName)
}
okExit("")
}
2025-02-14 17:27:00 -06:00
doListRepos()
2025-02-14 17:27:00 -06:00
if argv.DebBuild != nil {
buildDeb()
okExit("")
}
2025-02-14 17:27:00 -06:00
if argv.MakeInstall != nil {
if err := doInstall(); err != nil {
log.Info("doInstall() failed", err)
badExit(err)
}
okExit("EVERYTHING BUILT!")
2025-02-14 17:27:00 -06:00
}
if argv.Test {
homeDir, _ := os.UserHomeDir()
2024-12-24 04:47:58 -06:00
testdir := filepath.Join(homeDir, "go/src/go.wit.com/apps/utils/wit-utils/go-clone-test/")
os.Chdir(testdir)
shell.RunRealtime([]string{"go", "install"})
2024-12-24 04:47:58 -06:00
workdir := filepath.Join(homeDir, "gowork")
if err := os.MkdirAll(workdir, os.ModePerm); err != nil {
badExit(err)
2024-12-24 04:47:58 -06:00
}
os.Chdir(workdir)
shell.RunRealtime([]string{"go-clone-test"})
}
2024-12-24 04:47:58 -06:00
okExit("everything compiled")
2024-12-24 04:47:58 -06:00
}
// this is dumb. sync this with go-deb
func trimNonNumericFromStart(s string) string {
for i, r := range s {
if unicode.IsDigit(r) {
return s[i:]
}
}
return ""
}
func okExit(thing string) {
if thing != "" {
log.Info(thing, "ok")
}
// log.Info("Finished go-clean on", check.GetGoPath(), "ok")
os.Exit(0)
}
func badExit(err error) {
log.Info("go-clean failed: ", err, me.forge.GetGoSrc())
os.Exit(-1)
}
2025-02-20 09:39:31 -06:00
func dumpDebug() {
// Get absolute path of the currently running binary
exePath, err := os.Executable()
if err != nil {
fmt.Println("Error getting executable path:", err)
return
}
// Resolve symlinks if necessary
exePath, err = filepath.EvalSymlinks(exePath)
if err != nil {
fmt.Println("Error resolving symlink:", err)
return
}
// Read build info
bi, err := buildinfo.ReadFile(exePath)
if err != nil {
fmt.Println("Error reading build info:", err)
return
}
fmt.Println("Go Version:", bi.GoVersion)
for _, dep := range bi.Deps {
fmt.Printf("Dependency: %s %s\n", dep.Path, dep.Version)
}
}