summaryrefslogtreecommitdiff
path: root/main.go
blob: 96b3295980bcd68c62e6671eeef4a0fee9b7db31 (plain)
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
163
164
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

import (
	"debug/buildinfo"
	"fmt"
	"os"
	"path/filepath"
	"unicode"

	"go.wit.com/lib/gui/prep"
	"go.wit.com/lib/protobuf/forgepb"
	"go.wit.com/lib/protobuf/gitpb"
	"go.wit.com/lib/protobuf/zoopb"
	"go.wit.com/log"
)

// sent via -ldflags
var VERSION string
var BUILDTIME string

// used for shell auto completion
var ARGNAME string = "wit-test" // todo: get this from $0 ?

var failed map[*gitpb.Repo]string
var state map[*gitpb.Repo]string
var debnames map[*gitpb.Repo]string

func main() {
	me = new(autoType)
	me.auto = prep.Bash3(&argv) // add support for bash autocomplete with go-arg

	dumpDebug()

	failed = make(map[*gitpb.Repo]string)
	state = make(map[*gitpb.Repo]string)
	debnames = make(map[*gitpb.Repo]string)

	me.forge = forgepb.Init()

	me.machine, _ = zoopb.InitMachine()

	if argv.Clone != nil {
		if argv.RepoMap != "" {
			repomap(argv.RepoMap)
			okExit("")
		}
	}

	if argv.WITCOM {
		doWITCOM()
		okExit("")
	}

	if argv.Upgrade != nil {
		doAptUpgrade()
	}

	if argv.ListPkgs != nil {
		doAptList()
		log.DaemonMode(true)
		defer log.DaemonMode(false)
		fmt.Println("Installed Packages:")
		loop := me.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.machine.FindByVersion(p.Name, p.Version); actualp != nil {
				// end += "(version match) "
			} else {
				end += "(version mismatch) " + actualp.Version + " " + version + " "
			}
			if actualp := me.machine.FindInstalledByName(p.Name); actualp != nil {
				if p.Version != actualp.Version {
					end += "(installed " + actualp.Version + " vs " + p.Version + ") "
				} else {
					end += "(installed ok) "
				}
				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("")
	}

	doListRepos()

	if argv.DebBuild != nil {
		buildDeb()
		okExit("")
	}

	if argv.MakeInstall != nil {
		if err := doInstall(); err != nil {
			log.Info("doInstall() failed", err)
			badExit(err)
		}
		okExit("EVERYTHING BUILT!")
	}

	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.Config.ReposDir)
	os.Exit(-1)
}

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)
	}
}