better error handling

This commit is contained in:
Jeff Carr 2024-03-01 07:44:02 -06:00
parent d02e23a68a
commit 8d25d3996e
4 changed files with 72 additions and 66 deletions

View File

@ -67,13 +67,13 @@ build-all:
go-deb --no-gui --repo golang.org/x/tools/cmd/goimports
build-releases:
go-deb --release --no-gui --repo go.wit.com/apps/autotypist
go-deb --release --no-gui --repo go.wit.com/apps/control-panel-dns
go-deb --release --no-gui --repo go.wit.com/apps/control-panel-digitalocean
go-deb --release --no-gui --repo go.wit.com/apps/control-panel-cloudflare
go-deb --release --no-gui --repo go.wit.com/apps/control-panel-vpn
go-deb --release --no-gui --repo go.wit.com/apps/go-gui-toolkits
go-deb --release --no-gui --repo go.wit.com/apps/guireleaser
go-deb --release --no-gui --repo go.wit.com/apps/go-deb
go-deb --release --no-gui --repo go.wit.com/apps/go.wit.com
go-deb --release --no-gui --repo go.wit.com/apps/helloworld
-go-deb --release --no-gui --repo go.wit.com/apps/autotypist
-go-deb --release --no-gui --repo go.wit.com/apps/control-panel-dns
-go-deb --release --no-gui --repo go.wit.com/apps/control-panel-digitalocean
-go-deb --release --no-gui --repo go.wit.com/apps/control-panel-cloudflare
-go-deb --release --no-gui --repo go.wit.com/apps/control-panel-vpn
-go-deb --release --no-gui --repo go.wit.com/apps/go-gui-toolkits
-go-deb --release --no-gui --repo go.wit.com/apps/guireleaser
-go-deb --release --no-gui --repo go.wit.com/apps/go.wit.com
-go-deb --release --no-gui --repo go.wit.com/apps/helloworld
-go-deb --release --no-gui --repo go.wit.com/apps/go-deb

View File

@ -3,6 +3,7 @@
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
@ -14,7 +15,7 @@ import (
"go.wit.com/log"
)
func (c *controlBox) buildPackage() bool {
func (c *controlBox) buildPackage() error {
// TODO: if dirty, set GO111MODULE
// also, if last tag != version
/*
@ -26,35 +27,14 @@ func (c *controlBox) buildPackage() bool {
*/
// ldflags := "main.GOTAG=" + repo.LastTag()
if args.Release {
os.Unsetenv("GO111MODULE")
path := c.pathL.String() + "@latest"
cmd := []string{"go", "install", "-v", "-x", path}
if shell.Run(cmd) {
log.Warn("build worked")
} else {
log.Warn("build failed")
return false
}
} else {
os.Setenv("GO111MODULE", "off")
if shell.Run([]string{"go", "build", "-v", "-x"}) {
log.Warn("build worked")
} else {
log.Warn("build failed")
return false
}
}
filename := c.Package.String()
if filename == "" {
log.Warn("build failed")
return false
return errors.New("filename is blank")
}
if !shell.Exists(filename) {
log.Warn("build failed")
return false
homeDir, err := os.UserHomeDir()
if err != nil {
return err
}
arch := c.Architecture.String()
@ -62,34 +42,70 @@ func (c *controlBox) buildPackage() bool {
if version == "" {
version = "0.0.0"
}
debname := filename + "_" + version + "_" + arch + ".deb"
var fullfilename string
if args.Release {
fullfilename = filepath.Join(homeDir, "go/bin", filename)
} else {
fullfilename = filename
}
shell.Run([]string{"rm", "-f", fullfilename})
if shell.Exists(fullfilename) {
// something wrong
return errors.New("binary existed before build")
}
if args.Release {
os.Unsetenv("GO111MODULE")
path := c.pathL.String() + "@latest"
cmd := []string{"go", "install", "-v", "-x", path}
if shell.Run(cmd) {
log.Warn("go install worked")
} else {
return errors.New("go install")
}
} else {
// set the GO111 build var to true. pass the versions to the compiler manually
os.Setenv("GO111MODULE", "off")
vldflag := "-X main.VERSION=" + version
gldflag := "-X main.GUIVERSION=" + version // todo: git this from the filesystem
if shell.Run([]string{"go", "build", "-v", "-x", "-ldflags", vldflag, gldflag}) {
log.Warn("go build worked")
} else {
return errors.New("go build")
}
}
if !shell.Exists(fullfilename) {
log.Warn("build failed. filename does not exist", fullfilename)
return errors.New("missing" + fullfilename)
}
if shell.Exists("files") {
if !shell.Run([]string{"rm", "-rf", "files"}) {
log.Warn("rm failed")
return false
return errors.New("rm files/")
}
}
if shell.Exists("files") {
log.Warn("rm failed")
return false
return errors.New("rm files/")
}
if !shell.Mkdir("files/DEBIAN") {
log.Warn("mkdir failed")
return false
return errors.New("mkdir files/DEBIAN")
}
if !shell.Mkdir("files/usr/bin") {
log.Warn("mkdir failed")
return false
return errors.New("mkdir files/usr/bin")
}
if !shell.Run([]string{"cp", filename, "files/usr/bin"}) {
if !shell.Run([]string{"cp", fullfilename, "files/usr/bin"}) {
log.Warn("cp failed")
return false
return errors.New("cp " + fullfilename)
}
if !shell.Run([]string{"strip", "files/usr/bin/" + filename}) {
log.Warn("strip failed")
return false
return errors.New("strip " + filename)
}
// put the README in there (if missing, generate it?)
@ -105,24 +121,15 @@ func (c *controlBox) buildPackage() bool {
if readme != "" {
path := filepath.Join("files/usr/lib/" + filename)
if !shell.Mkdir(path) {
log.Warn("mkdir failed")
return false
return errors.New("no files/usr/lib")
}
if !shell.Run([]string{"cp", readme, path}) {
log.Warn("cp failed")
return false
return errors.New("cp readme")
}
}
if !c.writeFiles() {
log.Warn("write control file failed")
return false
}
homeDir, err := os.UserHomeDir()
if err != nil {
log.Warn("os.UserHomeDir() failed")
return false
return errors.New("write control file")
}
// experiment for the toolkit package
@ -139,14 +146,13 @@ func (c *controlBox) buildPackage() bool {
shell.Run([]string{"dpkg-deb", "--build", "files", fulldebname})
if shell.Exists(fulldebname) {
log.Warn("build worked")
} else {
log.Warn("build failed")
return false
return errors.New("dpkg-deb --build worked")
}
shell.Run([]string{"dpkg-deb", "-I", fulldebname})
shell.Run([]string{"dpkg-deb", "-c", fulldebname})
return true
return nil
}
func (c *controlBox) writeFiles() bool {

View File

@ -6,9 +6,9 @@ import (
"path/filepath"
"go.wit.com/gui"
"go.wit.com/lib/gui/shell"
"go.wit.com/lib/debugger"
"go.wit.com/lib/gadgets"
"go.wit.com/lib/gui/shell"
"go.wit.com/log"
)
@ -49,10 +49,10 @@ func main() {
if args.NoGui {
shell.TestTerminalColor()
if cBox.buildPackage() {
if err := cBox.buildPackage(); err == nil {
log.Info("build worked")
} else {
log.Warn("build failed")
log.Warn("build failed:", err)
os.Exit(-1)
}
os.Exit(0)

View File

@ -36,10 +36,10 @@ func makebasicWindow() *gadgets.BasicWindow {
group1.NewButton("Make .deb", func() {
basicWindow.Disable()
if cBox.buildPackage() {
if err := cBox.buildPackage(); err == nil {
log.Info("build worked")
} else {
log.Warn("build failed")
log.Warn("build failed", err)
}
basicWindow.Enable()
})