summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go257
1 files changed, 257 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..c0a8a73
--- /dev/null
+++ b/main.go
@@ -0,0 +1,257 @@
+package main
+
+import (
+ "errors"
+ "fmt"
+ "os"
+ "path/filepath"
+ "unicode"
+
+ "go.wit.com/lib/gui/shell"
+ "go.wit.com/log"
+
+ "go.wit.com/gui"
+ "go.wit.com/lib/protobuf/forgepb"
+ "go.wit.com/lib/protobuf/gitpb"
+)
+
+// 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)
+
+ 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()
+
+ me.myGui = gui.New()
+ me.myGui.Default()
+
+ if argv.RepoMap != "" {
+ repomap(argv.RepoMap)
+ okExit("")
+ }
+
+ if argv.Update {
+ if argv.DryRun {
+ log.Info("--dry-run", []string{"apt", "update"})
+ } else {
+ result := shell.Run([]string{"apt", "update"})
+ if result.Error != nil {
+ log.Info("apt update error:", result.Error)
+ badExit(result.Error)
+ }
+ }
+
+ fmt.Println("Installed Packages:")
+ loop := me.forge.Machine.Wit.SortByName()
+ for loop.Scan() {
+ p := loop.Next()
+ // log.Info("apt install", name)
+ if me.forge.Machine.IsInstalled(p.Name) {
+ if argv.DryRun {
+ log.Info("--dry-run", []string{"apt", "install", p.Name})
+ } else {
+ shell.Run([]string{"apt", "install", p.Name})
+ }
+ }
+ }
+ okExit("installed")
+ }
+
+ if argv.ListPkgs {
+ log.DaemonMode(true)
+ defer log.DaemonMode(false)
+ fmt.Println("Installed Packages:")
+ loop := me.forge.Machine.Wit.SortByName()
+ for loop.Scan() {
+ p := loop.Next()
+ var end string
+ var version string
+ if me.forge.Config.IsPrivate(p.Name) {
+ end += "(private) "
+ }
+ if actualp := me.forge.Machine.FindVersion(p.Name, p.Version); actualp != nil {
+ // end += "(version match) "
+ } else {
+ end += "(version mismatch) " + actualp.Version + " " + version + " "
+ }
+ if actualp := me.forge.Machine.FindInstalledByName(p.Name); actualp != nil {
+ end += "(installed " + actualp.Version + ") "
+ 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("")
+ }
+
+ all := me.forge.Repos.SortByFullPath()
+ for all.Scan() {
+ check := all.Next()
+
+ repotype := check.GetRepoType()
+ if repotype == "binary" || repotype == "plugin" {
+ // we only want to process things that can be compiled with 'go build'
+ } else {
+ // log.Info("skipping repo", check.GetGoPath(), repotype)
+ continue
+ }
+
+ if me.forge.Config.IsReadOnly(check.GetGoPath()) {
+ // ignore read only stuff
+ continue
+ }
+
+ // var cmd []string
+ var start string
+ var end string
+
+ // add te repotype
+ end += check.GetRepoType()
+
+ manufactured := check.GetCurrentVersion()
+ ver := trimNonNumericFromStart(manufactured)
+ name := me.forge.Config.DebName(check.GetGoPath())
+ var realver string
+ if installedPackage := me.forge.Machine.FindInstalledByName(name); installedPackage != nil {
+ realver = installedPackage.Version
+ }
+ if actualp := me.forge.Machine.FindVersion(name, ver); actualp != nil {
+ end += " (version match) " + actualp.Version + " " + ver + " "
+ state[check] = "on mirrors"
+ } else {
+ if realver != "" {
+ end += fmt.Sprintf(" (version miss) %s vs %s ", realver, ver)
+ }
+ // end += "" + ver + " "
+ }
+ if me.forge.Machine.IsInstalled(name) {
+ if actualp := me.forge.Machine.FindInstalledByName(name); actualp != nil {
+ if ver != actualp.Version {
+ end += "(installed " + actualp.Version + ") "
+ } else {
+ end += "(installed ok) "
+ }
+ } else {
+ end += "(installed) "
+ }
+ }
+
+ debname := name + "_" + ver + "_amd64.deb"
+ debnames[check] = debname
+ outdir := getOutdir(check)
+ _, err := os.Stat(filepath.Join(outdir, debname))
+ if err == nil {
+ // log.Info("exists", filepath.Join(outdir, debname))
+ state[check] = "in incoming"
+ } else {
+ // log.Info(debname, "does not exist")
+ }
+
+ if state[check] == "" {
+ state[check] = "need to build"
+ }
+ start = fmt.Sprintf("%-15s %-20s %-50s", state[check], ver, debname)
+
+ if state[check] == "need to build" {
+ end += " (will build) "
+ }
+
+ log.Info(start, end)
+ if name == "" {
+ // err := fmt.Sprintf("name is blank error %+v", repo)
+ log.Warn("name is blank error", check.GetGoPath())
+ }
+
+ if argv.DryRun {
+ continue
+ }
+ if argv.TestBuild {
+ if argv.DryRun {
+ continue
+ }
+ if err := me.forge.Build(check, nil); err != nil {
+ log.Warn("BUILD FAILED", check.GetGoPath(), err)
+ failed[check] = fmt.Sprintf("%s %s %v", "go build", check.GetGoPath(), err)
+ }
+ continue
+ }
+
+ if argv.MakeInstall {
+ log.Info("STARTING 'make install' in", check.GetGoPath())
+ if argv.DryRun {
+ continue
+ }
+ if err := me.forge.Install(check, nil); err != nil {
+ log.Warn("INSTALL FAILED", check.GetGoPath(), err)
+ failed[check] = fmt.Sprintf("%s %s %v", "go install", check.GetGoPath(), err)
+ }
+ continue
+ }
+ }
+ if argv.DebBuild {
+ buildDeb()
+ }
+ if len(failed) != 0 {
+ log.Info("")
+ log.Info("something failed on:")
+ for repo, cmd := range failed {
+ log.Info("failed cmd :", cmd, repo.GetGoPath())
+ }
+ me.forge.CheckoutUser()
+ shell.Run([]string{"forge", "--find-private"})
+ badExit(errors.New("some repos failed"))
+ }
+ if argv.Test {
+ homeDir, _ := os.UserHomeDir()
+
+ testdir := filepath.Join(homeDir, "go/src/go.wit.com/apps/utils/wit-utils/go-clone-test/")
+ os.Chdir(testdir)
+ shell.RunRealtime([]string{"go", "install"})
+
+ workdir := filepath.Join(homeDir, "gowork")
+ if err := os.MkdirAll(workdir, os.ModePerm); err != nil {
+ badExit(err)
+ }
+ os.Chdir(workdir)
+ shell.RunRealtime([]string{"go-clone-test"})
+ }
+ okExit("everything compiled")
+}
+
+// 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)
+}