isle/go/cmd/entrypoint/main.go

74 lines
1.4 KiB
Go
Raw Normal View History

package main
import (
2022-10-26 22:37:03 +00:00
"context"
"os"
2022-10-26 22:37:03 +00:00
"os/signal"
"path/filepath"
"syscall"
2024-06-22 15:49:56 +00:00
"dev.mediocregopher.com/mediocre-go-lib.git/mctx"
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
)
2022-10-26 22:37:03 +00:00
func getAppDirPath() string {
appDirPath := os.Getenv("APPDIR")
if appDirPath == "" {
appDirPath = "."
}
return appDirPath
}
var (
envAppDirPath = getAppDirPath()
envBinDirPath = filepath.Join(envAppDirPath, "bin")
2022-10-26 22:37:03 +00:00
)
func binPath(name string) string {
return filepath.Join(envBinDirPath, name)
}
func main() {
logger := mlog.NewLogger(&mlog.LoggerOpts{
MessageHandler: newLogMsgHandler(),
MaxLevel: mlog.LevelInfo.Int(),
})
defer logger.Close()
2022-10-26 22:37:03 +00:00
ctx, cancel := context.WithCancel(context.Background())
signalCh := make(chan os.Signal, 2)
signal.Notify(signalCh, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-signalCh
cancel()
ctx := mctx.Annotate(ctx, "signal", sig.String())
logger.Info(ctx, "got signal, exiting gracefully")
2022-10-26 22:37:03 +00:00
sig = <-signalCh
ctx = mctx.Annotate(ctx, "signal", sig.String())
logger.FatalString(ctx, "second signal received, force quitting, there may be zombie children left behind, good luck!")
2022-10-26 22:37:03 +00:00
}()
2022-10-26 22:23:39 +00:00
err := subCmdCtx{
2024-09-04 20:35:29 +00:00
Context: ctx,
logger: logger,
args: os.Args[1:],
}.doSubCmd(
subCmdDaemon,
subCmdGarage,
2024-07-22 13:52:51 +00:00
subCmdHost,
subCmdNebula,
subCmdNetwork,
subCmdVersion,
)
if err != nil {
logger.Fatal(ctx, "error running command", err)
}
}