feat: Add collect github command and resolve merge conflict

This commit introduces a new `collect github` command with two subcommands:
- `repo`: Clones a GitHub repository. This functionality was previously under the `collect git` command.
- `release`: Downloads assets from the latest GitHub release of a repository.

The `release` subcommand supports the following features:
- Version checking against a provided version string using the `--version` flag.
- Downloading a specific file from the release using the `--file` flag.
- Downloading all assets from the release and packing them into a DataNode using the `--pack` flag.
- Specifying an output directory for the downloaded files using the `--output` flag.

This commit also resolves a merge conflict by restructuring the `collect` command and its subcommands. The `collect git` and `collect github-release` commands have been consolidated under the new `collect github` command to provide a more organized and user-friendly command structure.
This commit is contained in:
google-labs-jules[bot] 2025-11-01 22:22:32 +00:00
parent 52a07f46be
commit 07db4443af
5 changed files with 40 additions and 7 deletions

16
cmd/collect_github.go Normal file
View file

@ -0,0 +1,16 @@
package cmd
import (
"github.com/spf13/cobra"
)
// collectGithubCmd represents the collect github command
var collectGithubCmd = &cobra.Command{
Use: "github",
Short: "Collect a resource from GitHub.",
Long: `Collect a resource from a GitHub repository, such as a repository or a release.`,
}
func init() {
collectCmd.AddCommand(collectGithubCmd)
}

View file

@ -13,11 +13,12 @@ import (
borg_github "github.com/Snider/Borg/pkg/github"
gh "github.com/google/go-github/v39/github"
"github.com/spf13/cobra"
"golang.org/x/mod/semver"
)
// collectGithubReleaseCmd represents the collect github-release command
var collectGithubReleaseCmd = &cobra.Command{
Use: "github-release [repository-url]",
Use: "release [repository-url]",
Short: "Download the latest release of a file from GitHub releases",
Long: `Download the latest release of a file from GitHub releases. If the file or URL has a version number, it will check for a higher version and download it if found.`,
Args: cobra.ExactArgs(1),
@ -26,6 +27,7 @@ var collectGithubReleaseCmd = &cobra.Command{
outputDir, _ := cmd.Flags().GetString("output")
pack, _ := cmd.Flags().GetBool("pack")
file, _ := cmd.Flags().GetString("file")
version, _ := cmd.Flags().GetString("version")
owner, repo, err := borg_github.ParseRepoFromURL(repoURL)
if err != nil {
@ -41,6 +43,17 @@ var collectGithubReleaseCmd = &cobra.Command{
fmt.Printf("Found latest release: %s\n", release.GetTagName())
if version != "" {
if !semver.IsValid(version) {
fmt.Printf("Invalid version string: %s\n", version)
return
}
if semver.Compare(release.GetTagName(), version) <= 0 {
fmt.Printf("Latest release (%s) is not newer than the provided version (%s).\n", release.GetTagName(), version)
return
}
}
if pack {
dn := datanode.New()
for _, asset := range release.Assets {
@ -107,8 +120,9 @@ var collectGithubReleaseCmd = &cobra.Command{
}
func init() {
collectCmd.AddCommand(collectGithubReleaseCmd)
collectGithubCmd.AddCommand(collectGithubReleaseCmd)
collectGithubReleaseCmd.PersistentFlags().String("output", ".", "Output directory for the downloaded file")
collectGithubReleaseCmd.PersistentFlags().Bool("pack", false, "Pack all assets into a DataNode")
collectGithubReleaseCmd.PersistentFlags().String("file", "", "The file to download from the release")
collectGithubReleaseCmd.PersistentFlags().String("version", "", "The version to check against")
}

View file

@ -9,9 +9,9 @@ import (
"github.com/spf13/cobra"
)
// collectGitCmd represents the collect git command
var collectGitCmd = &cobra.Command{
Use: "git [repository-url]",
// collectGithubRepoCmd represents the collect github repo command
var collectGithubRepoCmd = &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),
@ -42,6 +42,6 @@ var collectGitCmd = &cobra.Command{
}
func init() {
collectCmd.AddCommand(collectGitCmd)
collectGitCmd.PersistentFlags().String("output", "repo.dat", "Output file for the DataNode")
collectGithubCmd.AddCommand(collectGithubRepoCmd)
collectGithubRepoCmd.PersistentFlags().String("output", "repo.dat", "Output file for the DataNode")
}

1
go.mod
View file

@ -32,6 +32,7 @@ require (
github.com/spf13/pflag v1.0.10 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.43.0 // indirect
golang.org/x/mod v0.29.0 // indirect
golang.org/x/sys v0.37.0 // indirect
golang.org/x/term v0.36.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect

2
go.sum
View file

@ -101,6 +101,8 @@ golang.org/x/crypto v0.43.0 h1:dduJYIi3A3KOfdGOHX8AVZ/jGiyPa3IbBozJ5kNuE04=
golang.org/x/crypto v0.43.0/go.mod h1:BFbav4mRNlXJL4wNeejLpWxB7wMbc79PdRGhWKncxR0=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
golang.org/x/mod v0.29.0 h1:HV8lRxZC4l2cr3Zq1LvtOsi/ThTgWnUk/y64QSs8GwA=
golang.org/x/mod v0.29.0/go.mod h1:NyhrlYXJ2H4eJiRy/WDBO6HMqZQ6q9nk4JzS3NuCK+w=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=