Borg/cmd/compile.go
google-labs-jules[bot] 8ba0deab91 feat: Add _Good, _Bad, and _Ugly tests
Refactored the existing tests to use the `_Good`, `_Bad`, and `_Ugly`
testing convention. This provides a more structured approach to testing
and ensures that a wider range of scenarios are covered, including
valid inputs, invalid inputs, and edge cases.

In addition to refactoring the tests, this change also includes several
bug fixes that were uncovered by the new tests. These fixes improve the
robustness and reliability of the codebase.

The following packages and commands were affected:
- `pkg/datanode`
- `pkg/compress`
- `pkg/github`
- `pkg/matrix`
- `pkg/pwa`
- `pkg/vcs`
- `pkg/website`
- `cmd/all`
- `cmd/collect`
- `cmd/collect_github_repo`
- `cmd/collect_website`
- `cmd/compile`
- `cmd/root`
- `cmd/run`
- `cmd/serve`
2025-11-14 10:36:35 +00:00

74 lines
1.5 KiB
Go

package cmd
import (
"fmt"
"os"
"strings"
"github.com/Snider/Borg/pkg/matrix"
"github.com/spf13/cobra"
)
var borgfile string
var output string
var compileCmd = NewCompileCmd()
func NewCompileCmd() *cobra.Command {
compileCmd := &cobra.Command{
Use: "compile",
Short: "Compile a Borgfile into a Terminal Isolation Matrix.",
RunE: func(cmd *cobra.Command, args []string) error {
content, err := os.ReadFile(borgfile)
if err != nil {
return err
}
m, err := matrix.New()
if err != nil {
return err
}
lines := strings.Split(string(content), "\n")
for _, line := range lines {
parts := strings.Fields(line)
if len(parts) == 0 {
continue
}
switch parts[0] {
case "ADD":
if len(parts) != 3 {
return fmt.Errorf("invalid ADD instruction: %s", line)
}
src := parts[1]
dest := parts[2]
data, err := os.ReadFile(src)
if err != nil {
return err
}
m.RootFS.AddData(strings.TrimPrefix(dest, "/"), data)
default:
return fmt.Errorf("unknown instruction: %s", parts[0])
}
}
tarball, err := m.ToTar()
if err != nil {
return err
}
return os.WriteFile(output, tarball, 0644)
},
}
compileCmd.Flags().StringVarP(&borgfile, "file", "f", "Borgfile", "Path to the Borgfile.")
compileCmd.Flags().StringVarP(&output, "output", "o", "a.matrix", "Path to the output matrix file.")
return compileCmd
}
func GetCompileCmd() *cobra.Command {
return compileCmd
}
func init() {
RootCmd.AddCommand(GetCompileCmd())
}