exec: replace global state with interface

This is cleaner, and finally enables writing tests for the nix invoking functions.
This commit is contained in:
2025-07-17 16:53:00 +09:00
parent d4572eb50f
commit d3a8aed237
34 changed files with 324496 additions and 258903 deletions

View File

@@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"os"
"os/signal"
@@ -20,7 +21,9 @@ type commandHandlerError string
func (c commandHandlerError) Error() string { return string(c) }
func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
var ctx nixbuild.Context
nixCtx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
var (
@@ -31,10 +34,13 @@ func main() {
c := command.New(os.Stderr, log.Printf, "nixbuild", func(args []string) error {
log.SetFlags(0)
log.SetPrefix("nixbuild: ")
nixbuild.Stdout = os.Stdout
var stderr io.Writer
if flagVerbose {
nixbuild.Stderr = os.Stderr
stderr = os.Stderr
}
ctx = nixbuild.New(nixCtx, nil, os.Stdout, stderr)
return nil
}).
Flag(&flagNixOS, "nixos", command.BoolFlag(false), "Interpret input as NixOS flake installable").