This commit introduces two key improvements to the application: 1. **Authenticated GitHub API Access:** The GitHub client now uses a personal access token (PAT) from the `GITHUB_TOKEN` environment variable if it is available. This increases the rate limit for GitHub API requests, making the tool more robust for users who need to collect a large number of repositories. 2. **Structured Logging:** The application now uses the standard library's `slog` package for structured logging. A `--verbose` flag has been added to the root command to control the log level, allowing for more detailed output when needed. This makes the application's output more consistent and easier to parse.
61 lines
1.6 KiB
Go
61 lines
1.6 KiB
Go
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/Snider/Borg/pkg/github"
|
|
"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) {
|
|
log := cmd.Context().Value("logger").(*slog.Logger)
|
|
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)
|
|
|
|
dn, err := vcs.CloneGitRepository(repoURL)
|
|
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")
|
|
}
|