go-mod-clean/run.go

112 lines
2.7 KiB
Go

package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/go-cmd/cmd"
"go.wit.com/lib/gui/shell"
"go.wit.com/log"
)
func purgeGoCaches() {
homedir, err := os.UserHomeDir()
if err != nil {
badExit(nil, err)
}
pkgdir := filepath.Join(homedir, "go/pkg")
var cmd []string
cmd = []string{"chmod", "700", "-R", pkgdir}
runStrict("", cmd)
cmd = []string{"rm", "-rf", pkgdir}
runStrict("", cmd)
builddir := filepath.Join(homedir, ".cache/go-build")
cmd = []string{"rm", "-rf", builddir}
runStrict("", cmd)
// this can't have really happened
// echo this still failed in:
// echo "Linux hpdev2.grid.wit.com 6.9.8-rt-amd64 #1 SMP PREEMPT_RT Debian 6.9.8-1 (2024-07-07) x86_64 GNU/Linux"
// echo and I had to drop the caches after building go install binaries quickly and running them
// echo "as an os.Exec() between binaries"
// echo sysctl -w vm.drop_caches=3
}
func runStrict(wd string, cmd []string) {
var err error
if wd != "" {
if err = os.Chdir(wd); err != nil {
log.Info("cd", "wd", "failed", err)
badExit(nil, err)
}
}
log.Info(wd, "running:", wd, cmd)
// result := shell.Run(cmd)
result := shell.RunRealtime(cmd)
if result.Error != nil {
log.Info("cmd failed", wd, cmd, err)
for i, line := range result.Stdout {
log.Info("STDOUT:", i, line)
}
for i, line := range result.Stderr {
log.Info("STDERR:", i, line)
}
badExit(nil, err)
}
if result.Exit != 0 {
log.Info("cmd failed", wd, cmd, err)
for i, line := range result.Stdout {
log.Info("STDOUT:", i, line)
}
for i, line := range result.Stderr {
log.Info("STDERR:", i, line)
}
badExit(nil, errors.New(fmt.Sprintf("cmd failed with %d", result.Exit)))
}
for i, line := range result.Stdout {
log.Info(i, line)
}
}
func runVerbose(wd string, argv []string) (cmd.Status, error) {
var err error
log.DaemonMode(true)
defer log.DaemonMode(false)
if wd != "" {
if err = os.Chdir(wd); err != nil {
var s cmd.Status
s.Stdout = []string{"notreal stdout from runVerbose()"}
return s, fmt.Errorf("cd %s failed %v", wd, err)
}
}
log.Info(wd, "running:", wd, argv)
// result := shell.Run(cmd)
result := shell.Run(argv)
if result.Error != nil {
log.Info("cmd failed", wd, argv, err)
for _, line := range result.Stdout {
log.Info(line)
}
for i, line := range result.Stderr {
log.Info("STDERR:", i, line)
}
return result, result.Error
}
if result.Exit != 0 {
log.Info("cmd failed", wd, argv, err)
for _, line := range result.Stdout {
log.Info(line)
}
for i, line := range result.Stderr {
log.Info("STDERR:", i, line)
}
return result, fmt.Errorf("cmd failed with %d", result.Exit)
}
for _, line := range result.Stdout {
log.Info(line)
}
return result, nil
}