This commit introduces two new commands to the `borg` CLI: - `borg compile`: Compiles a `Borgfile` into a "Terminal Isolation Matrix" (`.matrix` file). The `Borgfile` format is a simple text file with `ADD <src> <dest>` instructions. - `borg run`: Executes a `.matrix` file using `runc`. The command unpacks the matrix into a temporary directory and then executes `runc run`. Tests have been added for both commands, mocking the `runc` execution to avoid environment dependencies.
73 lines
1.3 KiB
Go
73 lines
1.3 KiB
Go
package cmd
|
|
|
|
import (
|
|
"archive/tar"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var runCmd = &cobra.Command{
|
|
Use: "run [matrix file]",
|
|
Short: "Run a Terminal Isolation Matrix.",
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
matrixFile := args[0]
|
|
|
|
// 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(matrixFile)
|
|
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 := execCommand("runc", "run", "borg-container")
|
|
runc.Dir = tempDir
|
|
runc.Stdout = os.Stdout
|
|
runc.Stderr = os.Stderr
|
|
runc.Stdin = os.Stdin
|
|
return runc.Run()
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
RootCmd.AddCommand(runCmd)
|
|
}
|