Drive Conn3x from Go. This package runs the conn3x command, reads its -json stream as it comes, and hands you each event, with the circuit, step and host it's about already worked out.
It's for the web UI, CI tools, bots and anything else that wants Conn3x's results without scraping the terminal.
c := &conn3xexec.Conn3x{Dir: "./circuits"}
err := c.Run(ctx, func(ev conn3xexec.Event) error {
if ev.Hook != nil && ev.Hook.Resource.Step != nil && ev.Type == "apply_complete" {
s := ev.Hook.Resource.Step
fmt.Printf("%s: %s.%s done in %.0fs\n", s.Host, s.Circuit, s.Step, ev.Hook.Elapsed)
}
return nil
}, "apply", "-auto-approve")Runadds-jsonafter the command, so passplan,apply,destroyorrefreshand their flags as you'd type them.RunIntowrites the stream to a file with-json-intoinstead and follows it as conn3x writes, for flags that need stdout free of-json, such asapply -report.- Cancel the context and Conn3x gets an interrupt, as Ctrl-C sends, so it stops cleanly and releases its state lock. It's killed only if it hasn't stopped after
Grace(30 seconds by default). On Windows it's killed straight away. - A non-zero exit comes back as an
*ExitErrorwith its code and the error diagnostics from the stream.plan -detailed-exitcodeexiting 2 for "there are changes" isExitError{Code: 2}. Readreads a stream you already have, such as a file from-json-into.
The events and the conn3x step object are described in JSON output. The step object needs Conn3x v0.21.0 or later. Older releases still stream every event, just without Step.
Experimental, like the stream's step object, until Conn3x v1. Expect the API to change before then.
MPL-2.0, like Conn3x.