Other: SetMainExecutable, ForceLauncher

This commit is contained in:
James Houlahan
2022-10-13 03:04:02 +02:00
parent a4852c1b36
commit cec44be7c3
4 changed files with 41 additions and 38 deletions

View File

@ -5,30 +5,22 @@ import (
"strconv"
"strings"
"github.com/bradenaw/juniper/xslices"
"github.com/sirupsen/logrus"
"golang.org/x/sys/execabs"
)
const (
BridgeCrashCount = "BRIDGE_CRASH_COUNT"
BridgeLauncher = "BRIDGE_LAUNCHER"
)
const BridgeCrashCount = "BRIDGE_CRASH_COUNT"
type Restarter struct {
restart bool
crash bool
exe string
exe string
flags []string
}
func New() *Restarter {
var exe string
if osExe, err := os.Executable(); err == nil {
exe = osExe
} else {
logrus.WithError(err).Error("Failed to get executable path, the app will not be able to restart")
}
func New(exe string) *Restarter {
return &Restarter{exe: exe}
}
@ -37,6 +29,14 @@ func (restarter *Restarter) Set(restart, crash bool) {
restarter.crash = crash
}
func (restarter *Restarter) Override(exe string) {
restarter.exe = exe
}
func (restarter *Restarter) AddFlags(flags ...string) {
restarter.flags = append(restarter.flags, flags...)
}
func (restarter *Restarter) Restart() {
if !restarter.restart {
return
@ -49,12 +49,12 @@ func (restarter *Restarter) Restart() {
env := getEnvMap()
if restarter.crash {
env[BridgeCrashCount] = increment(env[BridgeLauncher])
env[BridgeCrashCount] = increment(env[BridgeCrashCount])
} else {
delete(env, BridgeCrashCount)
}
cmd := execabs.Command(restarter.exe, os.Args[1:]...)
cmd := execabs.Command(restarter.exe, xslices.Join(os.Args[1:], restarter.flags)...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout