diff options
| author | Ophestra <cat@gensokyo.uk> | 2026-06-16 23:26:55 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2026-06-16 23:27:50 +0900 |
| commit | f46a0370a723f3920fc3be5edc8ca630d9039dc5 (patch) | |
| tree | a9bf694eb563211f1ca76550279baa3d6dbadad5 /cmd/app/run.go | |
| parent | 5ee66a86eea424f05ee88baba3c5c81d820555c7 (diff) | |
cmd/app: initial container template command
This program is an experimental frontend for cmd/hakurei. This serves as a short-term replacement of the nixos module and a testing environment to implement API useful to planterette.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'cmd/app/run.go')
| -rw-r--r-- | cmd/app/run.go | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/cmd/app/run.go b/cmd/app/run.go new file mode 100644 index 00000000..16f8b885 --- /dev/null +++ b/cmd/app/run.go @@ -0,0 +1,51 @@ +package main + +import ( + "context" + "encoding/json" + "os" + "os/exec" + "syscall" + + "hakurei.app/hst" + "hakurei.app/message" +) + +// run starts a container via cmd/hakurei and returns after it terminates. +func run(ctx context.Context, msg message.Msg, config *hst.Config) error { + c, cancel := context.WithCancel(ctx) + defer cancel() + + cmd := exec.CommandContext(c, "hakurei") + cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr + cmd.Cancel = func() error { + return cmd.Process.Signal(syscall.SIGINT) + } + if msg.IsVerbose() { + cmd.Args = append(cmd.Args, "-v") + } + cmd.Args = append(cmd.Args, "run", "3") + + r, w, err := os.Pipe() + if err != nil { + return err + } + cmd.ExtraFiles = append(cmd.ExtraFiles, r) + + if err = cmd.Start(); err != nil { + _, _ = r.Close(), w.Close() + return err + } + + if err = r.Close(); err != nil { + _ = w.Close() + return err + } else if err = json.NewEncoder(w).Encode(&config); err != nil { + _ = w.Close() + return err + } else if err = w.Close(); err != nil { + return err + } + + return cmd.Wait() +} |
