This commit introduces a TDD testing framework for the `collect` commands and resolves a build failure. - A `TDD/` directory has been added to house tests for the `collect` commands. - 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. - The "flag redefined" build error has been fixed by refactoring the root command initialization in `cmd/root.go` to prevent duplicate flag definitions.
69 lines
1.8 KiB
Go
69 lines
1.8 KiB
Go
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/Snider/Borg/pkg/github"
|
|
"github.com/Snider/Borg/pkg/ui"
|
|
"github.com/Snider/Borg/pkg/vcs"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// allCmd represents the all command
|
|
var allCmd = &cobra.Command{
|
|
Use: "all [user/org]",
|
|
Short: "Collect all public repositories from a user or organization",
|
|
Long: `Collect all public repositories from a user or organization and store them in a DataNode.`,
|
|
Args: cobra.ExactArgs(1),
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
logVal := cmd.Context().Value("logger")
|
|
log, ok := logVal.(*slog.Logger)
|
|
if !ok || log == nil {
|
|
fmt.Fprintln(os.Stderr, "Error: logger not properly initialised")
|
|
return
|
|
}
|
|
repos, err := github.GetPublicRepos(context.Background(), args[0])
|
|
if err != nil {
|
|
log.Error("failed to get public repos", "err", err)
|
|
return
|
|
}
|
|
|
|
outputDir, _ := cmd.Flags().GetString("output")
|
|
|
|
for _, repoURL := range repos {
|
|
log.Info("cloning repository", "url", repoURL)
|
|
bar := ui.NewProgressBar(-1, "Cloning repository")
|
|
|
|
dn, err := vcs.CloneGitRepository(repoURL, bar)
|
|
bar.Finish()
|
|
if err != nil {
|
|
log.Error("failed to clone repository", "url", repoURL, "err", err)
|
|
continue
|
|
}
|
|
|
|
data, err := dn.ToTar()
|
|
if err != nil {
|
|
log.Error("failed to serialize datanode", "url", repoURL, "err", err)
|
|
continue
|
|
}
|
|
|
|
repoName := strings.Split(repoURL, "/")[len(strings.Split(repoURL, "/"))-1]
|
|
outputFile := fmt.Sprintf("%s/%s.dat", outputDir, repoName)
|
|
err = os.WriteFile(outputFile, data, 0644)
|
|
if err != nil {
|
|
log.Error("failed to write datanode to file", "url", repoURL, "err", err)
|
|
continue
|
|
}
|
|
}
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
RootCmd.AddCommand(allCmd)
|
|
allCmd.PersistentFlags().String("output", ".", "Output directory for the DataNodes")
|
|
}
|