diff options
| author | Ophestra <cat@gensokyo.uk> | 2025-09-29 02:33:10 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2025-09-29 06:11:47 +0900 |
| commit | 46cd3a28c83e93b5d66c52d06a8366c11910d5c0 (patch) | |
| tree | 6e25c5078b5cd9de78b7a6c9b253f1132358812b /cmd/hpkg | |
| parent | ad1bc6794f0053c3e63eab408a0d530d53e8b51c (diff) | |
container: remove global msg
This frees all container instances of side effects.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'cmd/hpkg')
| -rw-r--r-- | cmd/hpkg/main.go | 37 | ||||
| -rw-r--r-- | cmd/hpkg/paths.go | 5 | ||||
| -rw-r--r-- | cmd/hpkg/proc.go | 13 | ||||
| -rw-r--r-- | cmd/hpkg/with.go | 17 |
4 files changed, 37 insertions, 35 deletions
diff --git a/cmd/hpkg/main.go b/cmd/hpkg/main.go index 2ae646c3..03247aac 100644 --- a/cmd/hpkg/main.go +++ b/cmd/hpkg/main.go @@ -13,22 +13,21 @@ import ( "hakurei.app/command" "hakurei.app/container" "hakurei.app/hst" - "hakurei.app/internal" - "hakurei.app/internal/hlog" ) var ( errSuccess = errors.New("success") ) -func init() { - hlog.Prepare("hpkg") +func main() { + log.SetPrefix("hpkg: ") + log.SetFlags(0) + msg := container.NewMsg(log.Default()) + if err := os.Setenv("SHELL", pathShell.String()); err != nil { log.Fatalf("cannot set $SHELL: %v", err) } -} -func main() { if os.Geteuid() == 0 { log.Fatal("this program must not run as root") } @@ -41,7 +40,7 @@ func main() { flagVerbose bool flagDropShell bool ) - c := command.New(os.Stderr, log.Printf, "hpkg", func([]string) error { internal.InstallOutput(flagVerbose); return nil }). + c := command.New(os.Stderr, log.Printf, "hpkg", func([]string) error { msg.SwapVerbose(flagVerbose); return nil }). Flag(&flagVerbose, "v", command.BoolFlag(false), "Print debug messages to the console"). Flag(&flagDropShell, "s", command.BoolFlag(false), "Drop to a shell in place of next hakurei action") @@ -90,12 +89,12 @@ func main() { } cleanup := func() { // should be faster than a native implementation - mustRun(chmod, "-R", "+w", workDir.String()) - mustRun(rm, "-rf", workDir.String()) + mustRun(msg, chmod, "-R", "+w", workDir.String()) + mustRun(msg, rm, "-rf", workDir.String()) } beforeRunFail.Store(&cleanup) - mustRun(tar, "-C", workDir.String(), "-xf", pkgPath) + mustRun(msg, tar, "-C", workDir.String(), "-xf", pkgPath) /* Parse bundle and app metadata, do pre-install checks. @@ -148,10 +147,10 @@ func main() { } // sec: should compare version string - hlog.Verbosef("installing application %q version %q over local %q", + msg.Verbosef("installing application %q version %q over local %q", bundle.ID, bundle.Version, a.Version) } else { - hlog.Verbosef("application %q clean installation", bundle.ID) + msg.Verbosef("application %q clean installation", bundle.ID) // sec: should install credentials } @@ -159,7 +158,7 @@ func main() { Setup steps for files owned by the target user. */ - withCacheDir(ctx, "install", []string{ + withCacheDir(ctx, msg, "install", []string{ // export inner bundle path in the environment "export BUNDLE=" + hst.Tmp + "/bundle", // replace inner /etc @@ -181,7 +180,7 @@ func main() { }, workDir, bundle, pathSet, flagDropShell, cleanup) if bundle.GPU { - withCacheDir(ctx, "mesa-wrappers", []string{ + withCacheDir(ctx, msg, "mesa-wrappers", []string{ // link nixGL mesa wrappers "mkdir -p nix/.nixGL", "ln -s " + bundle.Mesa + "/bin/nixGLIntel nix/.nixGL/nixGL", @@ -193,7 +192,7 @@ func main() { Activate home-manager generation. */ - withNixDaemon(ctx, "activate", []string{ + withNixDaemon(ctx, msg, "activate", []string{ // clean up broken links "mkdir -p .local/state/{nix,home-manager}", "chmod -R +w .local/state/{nix,home-manager}", @@ -261,7 +260,7 @@ func main() { */ if a.GPU && flagAutoDrivers { - withNixDaemon(ctx, "nix-gl", []string{ + withNixDaemon(ctx, msg, "nix-gl", []string{ "mkdir -p /nix/.nixGL/auto", "rm -rf /nix/.nixGL/auto", "export NIXPKGS_ALLOW_UNFREE=1", @@ -316,7 +315,7 @@ func main() { Spawn app. */ - mustRunApp(ctx, config, func() {}) + mustRunApp(ctx, msg, config, func() {}) return errSuccess }). Flag(&flagDropShellNixGL, "s", command.BoolFlag(false), "Drop to a shell on nixGL build"). @@ -324,9 +323,9 @@ func main() { } c.MustParse(os.Args[1:], func(err error) { - hlog.Verbosef("command returned %v", err) + msg.Verbosef("command returned %v", err) if errors.Is(err, errSuccess) { - hlog.BeforeExit() + msg.BeforeExit() os.Exit(0) } }) diff --git a/cmd/hpkg/paths.go b/cmd/hpkg/paths.go index 0964a550..ee4b7e71 100644 --- a/cmd/hpkg/paths.go +++ b/cmd/hpkg/paths.go @@ -9,7 +9,6 @@ import ( "hakurei.app/container" "hakurei.app/hst" - "hakurei.app/internal/hlog" ) const bash = "bash" @@ -51,8 +50,8 @@ func lookPath(file string) string { var beforeRunFail = new(atomic.Pointer[func()]) -func mustRun(name string, arg ...string) { - hlog.Verbosef("spawning process: %q %q", name, arg) +func mustRun(msg container.Msg, name string, arg ...string) { + msg.Verbosef("spawning process: %q %q", name, arg) cmd := exec.Command(name, arg...) cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr if err := cmd.Run(); err != nil { diff --git a/cmd/hpkg/proc.go b/cmd/hpkg/proc.go index d374e13d..1bbf5b49 100644 --- a/cmd/hpkg/proc.go +++ b/cmd/hpkg/proc.go @@ -9,14 +9,14 @@ import ( "os" "os/exec" + "hakurei.app/container" "hakurei.app/hst" "hakurei.app/internal" - "hakurei.app/internal/hlog" ) var hakureiPath = internal.MustHakureiPath() -func mustRunApp(ctx context.Context, config *hst.Config, beforeFail func()) { +func mustRunApp(ctx context.Context, msg container.Msg, config *hst.Config, beforeFail func()) { var ( cmd *exec.Cmd st io.WriteCloser @@ -26,10 +26,10 @@ func mustRunApp(ctx context.Context, config *hst.Config, beforeFail func()) { beforeFail() log.Fatalf("cannot pipe: %v", err) } else { - if hlog.Load() { - cmd = exec.CommandContext(ctx, hakureiPath, "-v", "app", "3") + if msg.IsVerbose() { + cmd = exec.CommandContext(ctx, hakureiPath.String(), "-v", "app", "3") } else { - cmd = exec.CommandContext(ctx, hakureiPath, "app", "3") + cmd = exec.CommandContext(ctx, hakureiPath.String(), "app", "3") } cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr cmd.ExtraFiles = []*os.File{r} @@ -51,7 +51,8 @@ func mustRunApp(ctx context.Context, config *hst.Config, beforeFail func()) { var exitError *exec.ExitError if errors.As(err, &exitError) { beforeFail() - internal.Exit(exitError.ExitCode()) + msg.BeforeExit() + os.Exit(exitError.ExitCode()) } else { beforeFail() log.Fatalf("cannot wait: %v", err) diff --git a/cmd/hpkg/with.go b/cmd/hpkg/with.go index 1895a06f..578d5251 100644 --- a/cmd/hpkg/with.go +++ b/cmd/hpkg/with.go @@ -2,20 +2,21 @@ package main import ( "context" + "os" "strings" "hakurei.app/container" "hakurei.app/container/seccomp" "hakurei.app/hst" - "hakurei.app/internal" ) func withNixDaemon( ctx context.Context, + msg container.Msg, action string, command []string, net bool, updateConfig func(config *hst.Config) *hst.Config, app *appInfo, pathSet *appPathSet, dropShell bool, beforeFail func(), ) { - mustRunAppDropShell(ctx, updateConfig(&hst.Config{ + mustRunAppDropShell(ctx, msg, updateConfig(&hst.Config{ ID: app.ID, Path: pathShell, @@ -61,9 +62,10 @@ func withNixDaemon( func withCacheDir( ctx context.Context, + msg container.Msg, action string, command []string, workDir *container.Absolute, app *appInfo, pathSet *appPathSet, dropShell bool, beforeFail func()) { - mustRunAppDropShell(ctx, &hst.Config{ + mustRunAppDropShell(ctx, msg, &hst.Config{ ID: app.ID, Path: pathShell, @@ -97,12 +99,13 @@ func withCacheDir( }, dropShell, beforeFail) } -func mustRunAppDropShell(ctx context.Context, config *hst.Config, dropShell bool, beforeFail func()) { +func mustRunAppDropShell(ctx context.Context, msg container.Msg, config *hst.Config, dropShell bool, beforeFail func()) { if dropShell { config.Args = []string{bash, "-l"} - mustRunApp(ctx, config, beforeFail) + mustRunApp(ctx, msg, config, beforeFail) beforeFail() - internal.Exit(0) + msg.BeforeExit() + os.Exit(0) } - mustRunApp(ctx, config, beforeFail) + mustRunApp(ctx, msg, config, beforeFail) } |
