Borg/pkg/matrix/run.go
google-labs-jules[bot] 80aacc85a4 feat: Implement Go examples and refactor matrix execution
- Implements all placeholder Go examples in the `examples` directory.
- Corrects the `run_matrix_programmatically` example to use the `borg` package.
- Refactors the code to centralize the matrix execution logic in the `matrix` package.
- Updates the documentation to include a new "Programmatic Usage" section that describes all of the Go examples.
- Updates the "Terminal Isolation Matrix" section to remove manual 'runc' instructions, emphasizing that 'borg run' handles this process to maintain security and isolation.
- Adds missing examples for 'collect github repos', 'collect github release', and 'compile' commands to the documentation.
2025-11-14 11:12:15 +00:00

62 lines
1.1 KiB
Go

package matrix
import (
"archive/tar"
"io"
"os"
"os/exec"
"path/filepath"
)
// Run executes a Terminal Isolation Matrix from a given path.
func Run(matrixPath string) error {
// Create a temporary directory to unpack the matrix file.
tempDir, err := os.MkdirTemp("", "borg-run-*")
if err != nil {
return err
}
defer os.RemoveAll(tempDir)
// Unpack the matrix file.
file, err := os.Open(matrixPath)
if err != nil {
return err
}
defer file.Close()
tr := tar.NewReader(file)
for {
header, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
path := filepath.Join(tempDir, header.Name)
if header.Typeflag == tar.TypeDir {
if err := os.MkdirAll(path, 0755); err != nil {
return err
}
continue
}
outFile, err := os.Create(path)
if err != nil {
return err
}
defer outFile.Close()
if _, err := io.Copy(outFile, tr); err != nil {
return err
}
}
// Run the matrix.
runc := exec.Command("runc", "run", "borg-container")
runc.Dir = tempDir
runc.Stdout = os.Stdout
runc.Stderr = os.Stderr
runc.Stdin = os.Stdin
return runc.Run()
}