isle/go/cmd/entrypoint/main.go

111 lines
2.2 KiB
Go
Raw Permalink Normal View History

package main
import (
2022-10-26 22:37:03 +00:00
"context"
2024-11-14 20:49:35 +00:00
"fmt"
"isle/toolkit"
"os"
2022-10-26 22:37:03 +00:00
"os/signal"
"path/filepath"
"sync"
2022-10-26 22:37:03 +00:00
"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"
"github.com/adrg/xdg"
)
const systemRootPath = ""
func getSystemRootPath() string {
if systemRootPath != "" {
return systemRootPath
}
if appDirPath := os.Getenv("APPDIR"); appDirPath != "" {
return appDirPath
2022-10-26 22:37:03 +00:00
}
return "/"
}
func getBinDirPath() string {
return filepath.Join(getSystemRootPath(), "usr/libexec/isle")
}
func getShareDirPath() string {
return filepath.Join(getSystemRootPath(), "usr/share/isle")
2022-10-26 22:37:03 +00:00
}
var (
envCacheDir = sync.OnceValue(func() toolkit.Dir {
cacheHome, err := toolkit.MkDir(xdg.CacheHome, true)
if err != nil {
panic(fmt.Errorf("creating cache directory %q: %w", xdg.CacheHome, err))
}
cacheDir, err := cacheHome.MkChildDir("isle", true)
if err != nil {
panic(fmt.Errorf("creating isle cache directory: %w", err))
}
return cacheDir
})
2022-10-26 22:37:03 +00:00
)
func binPath(name string) string {
return filepath.Join(getBinDirPath(), name)
}
2024-11-14 20:49:35 +00:00
var rootCmd = subCmd{
name: "isle",
descr: "All Isle sub-commands",
2024-11-14 20:49:35 +00:00
do: func(ctx subCmdCtx) error {
return ctx.doSubCmd(
subCmdDaemon,
subCmdGarage,
subCmdHost,
subCmdNetwork,
subCmdStorage,
subCmdVersion,
subCmdVPN,
2024-11-14 20:49:35 +00:00
)
},
}
func doRootCmd(
ctx context.Context,
logger *mlog.Logger,
opts *subCmdCtxOpts,
) error {
subCmdCtx := newSubCmdCtx(ctx, logger, rootCmd, opts)
2024-11-14 20:49:35 +00:00
return subCmdCtx.subCmd.do(subCmdCtx)
}
func main() {
logger := mlog.NewLogger(nil)
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
}()
if err := doRootCmd(ctx, logger, nil); err != nil {
2024-11-14 20:49:35 +00:00
fmt.Fprintln(os.Stderr, err)
}
}