This commit introduces a significant refactoring of the `cmd` package to improve testability and increases test coverage across the application. Key changes include: - Refactored Cobra commands to use `RunE` for better error handling and testing. - Extracted business logic from command handlers into separate, testable functions. - Added comprehensive unit tests for the `cmd`, `compress`, `github`, `logger`, and `pwa` packages. - Added tests for missing command-line arguments, as requested. - Implemented the `borg all` command to clone all public repositories for a GitHub user or organization. - Restored and improved the `collect pwa` functionality. - Removed duplicate code and fixed various bugs.
95 lines
2.6 KiB
Go
95 lines
2.6 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/Snider/Borg/pkg/compress"
|
|
"github.com/Snider/Borg/pkg/matrix"
|
|
"github.com/Snider/Borg/pkg/ui"
|
|
"github.com/Snider/Borg/pkg/vcs"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var (
|
|
// GitCloner is the git cloner used by the command. It can be replaced for testing.
|
|
GitCloner = vcs.NewGitCloner()
|
|
)
|
|
|
|
// NewCollectGithubRepoCmd creates a new cobra command for collecting a single git repository.
|
|
func NewCollectGithubRepoCmd() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "repo [repository-url]",
|
|
Short: "Collect a single Git repository",
|
|
Long: `Collect a single Git repository and store it in a DataNode.`,
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
repoURL := args[0]
|
|
outputFile, _ := cmd.Flags().GetString("output")
|
|
format, _ := cmd.Flags().GetString("format")
|
|
compression, _ := cmd.Flags().GetString("compression")
|
|
|
|
prompter := ui.NewNonInteractivePrompter(ui.GetVCSQuote)
|
|
prompter.Start()
|
|
defer prompter.Stop()
|
|
|
|
var progressWriter io.Writer
|
|
if prompter.IsInteractive() {
|
|
bar := ui.NewProgressBar(-1, "Cloning repository")
|
|
progressWriter = ui.NewProgressWriter(bar)
|
|
}
|
|
|
|
dn, err := GitCloner.CloneGitRepository(repoURL, progressWriter)
|
|
if err != nil {
|
|
return fmt.Errorf("Error cloning repository: %w", err)
|
|
}
|
|
|
|
var data []byte
|
|
if format == "matrix" {
|
|
matrix, err := matrix.FromDataNode(dn)
|
|
if err != nil {
|
|
return fmt.Errorf("Error creating matrix: %w", err)
|
|
}
|
|
data, err = matrix.ToTar()
|
|
if err != nil {
|
|
return fmt.Errorf("Error serializing matrix: %w", err)
|
|
}
|
|
} else {
|
|
data, err = dn.ToTar()
|
|
if err != nil {
|
|
return fmt.Errorf("Error serializing DataNode: %w", err)
|
|
}
|
|
}
|
|
|
|
compressedData, err := compress.Compress(data, compression)
|
|
if err != nil {
|
|
return fmt.Errorf("Error compressing data: %w", err)
|
|
}
|
|
|
|
if outputFile == "" {
|
|
outputFile = "repo." + format
|
|
if compression != "none" {
|
|
outputFile += "." + compression
|
|
}
|
|
}
|
|
|
|
err = os.WriteFile(outputFile, compressedData, 0644)
|
|
if err != nil {
|
|
return fmt.Errorf("Error writing DataNode to file: %w", err)
|
|
}
|
|
|
|
fmt.Fprintln(cmd.OutOrStdout(), "Repository saved to", outputFile)
|
|
return nil
|
|
},
|
|
}
|
|
cmd.PersistentFlags().String("output", "", "Output file for the DataNode")
|
|
cmd.PersistentFlags().String("format", "datanode", "Output format (datanode or matrix)")
|
|
cmd.PersistentFlags().String("compression", "none", "Compression format (none, gz, or xz)")
|
|
return cmd
|
|
}
|
|
|
|
func init() {
|
|
collectGithubCmd.AddCommand(NewCollectGithubRepoCmd())
|
|
}
|