This commit introduces the ability to generate a manifest file that lists the contents of a collection, including metadata such as file size, SHA256 hash, and file type statistics. The manifest can be generated in three ways: - During collection, by using the `--manifest` flag with the `collect all` command. - From an existing archive, by using the new `borg manifest` command. - As a public, unencrypted header in a `.stim` file, by using the `--public-manifest` flag with the `compile` command. This feature is useful for inspecting the contents of large or encrypted archives without having to extract them first. Co-authored-by: Snider <631881+Snider@users.noreply.github.com>
107 lines
2.7 KiB
Go
107 lines
2.7 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/Snider/Borg/pkg/manifest"
|
|
"github.com/Snider/Borg/pkg/tim"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var borgfile string
|
|
var output string
|
|
var encryptPassword string
|
|
var publicManifest bool
|
|
|
|
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 := tim.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])
|
|
}
|
|
}
|
|
|
|
// If encryption is requested, output as .stim
|
|
if encryptPassword != "" {
|
|
var manifestData []byte
|
|
if publicManifest {
|
|
m, err := manifest.Generate(m.RootFS, borgfile, "stim", true)
|
|
if err != nil {
|
|
return fmt.Errorf("error generating manifest: %w", err)
|
|
}
|
|
manifestData, err = m.ToJSON()
|
|
if err != nil {
|
|
return fmt.Errorf("error marshalling manifest: %w", err)
|
|
}
|
|
}
|
|
|
|
stimData, err := m.ToSigil(encryptPassword, manifestData)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
outputPath := output
|
|
if !strings.HasSuffix(outputPath, ".stim") {
|
|
outputPath = strings.TrimSuffix(outputPath, ".tim") + ".stim"
|
|
}
|
|
fmt.Fprintf(cmd.OutOrStdout(), "Compiled encrypted TIM to %s\n", outputPath)
|
|
return os.WriteFile(outputPath, stimData, 0644)
|
|
}
|
|
|
|
// Original unencrypted output
|
|
tarball, err := m.ToTar()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fmt.Fprintf(cmd.OutOrStdout(), "Compiled TIM to %s\n", output)
|
|
return os.WriteFile(output, tarball, 0644)
|
|
},
|
|
}
|
|
compileCmd.Flags().StringVarP(&borgfile, "file", "f", "Borgfile", "Path to the Borgfile.")
|
|
compileCmd.Flags().StringVarP(&output, "output", "o", "a.tim", "Path to the output tim file.")
|
|
compileCmd.Flags().StringVarP(&encryptPassword, "encrypt", "e", "", "Encrypt with ChaCha20-Poly1305 using this password (outputs .stim)")
|
|
compileCmd.Flags().BoolVar(&publicManifest, "public-manifest", false, "Embed a public manifest in the .stim header")
|
|
return compileCmd
|
|
}
|
|
|
|
func GetCompileCmd() *cobra.Command {
|
|
return compileCmd
|
|
}
|
|
|
|
func init() {
|
|
RootCmd.AddCommand(GetCompileCmd())
|
|
}
|