This commit introduces a TDD testing framework for the `collect` commands. - A `TDD/` directory has been added to house the tests. - An environment variable `BORG_PLEXSUS=0` has been implemented to enable a mock mode, which prevents external network calls during testing. - The `collect` commands have been updated to use the command's output streams, allowing for output capturing in tests. - A `pkg/mocks` package has been added to provide mock implementations for testing. - The `.gitignore` file has been updated to exclude generated `.datanode` files.
61 lines
1.2 KiB
Go
61 lines
1.2 KiB
Go
package vcs
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/Snider/Borg/pkg/datanode"
|
|
|
|
"github.com/go-git/go-git/v5"
|
|
)
|
|
|
|
// CloneGitRepository clones a Git repository from a URL and packages it into a DataNode.
|
|
func CloneGitRepository(repoURL string, progress io.Writer) (*datanode.DataNode, error) {
|
|
if os.Getenv("BORG_PLEXSUS") == "0" {
|
|
dn := datanode.New()
|
|
dn.AddData("README.md", []byte("Mock README"))
|
|
return dn, nil
|
|
}
|
|
tempPath, err := os.MkdirTemp("", "borg-clone-*")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer os.RemoveAll(tempPath)
|
|
|
|
cloneOptions := &git.CloneOptions{
|
|
URL: repoURL,
|
|
}
|
|
if progress != nil {
|
|
cloneOptions.Progress = progress
|
|
}
|
|
|
|
_, err = git.PlainClone(tempPath, false, cloneOptions)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
dn := datanode.New()
|
|
err = filepath.Walk(tempPath, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !info.IsDir() {
|
|
content, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
relPath, err := filepath.Rel(tempPath, path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
dn.AddData(relPath, content)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return dn, nil
|
|
}
|