From 38bbae55531e2237163caaa44ad6f54ea4e90cbd Mon Sep 17 00:00:00 2001 From: jack Date: Wed, 12 Feb 2025 18:48:29 +0800 Subject: [PATCH] add token paramter to the pull method --- src/pull.go | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/pull.go b/src/pull.go index 84ae3812..a6d0696b 100644 --- a/src/pull.go +++ b/src/pull.go @@ -9,11 +9,13 @@ import ( "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/config" + "github.com/go-git/go-git/v5/plumbing/transport" + "github.com/go-git/go-git/v5/plumbing/transport/http" "github.com/spf13/cobra" ) type PullOnlyFlags struct { - SourceURL string + SourceURL, Token string } type PullFlags struct { @@ -28,6 +30,7 @@ func (f *PullFlags) Init(cmd *cobra.Command) { func (f *PullOnlyFlags) Init(cmd *cobra.Command) { cmd.Flags().StringVar(&f.SourceURL, "source-url", "https://github.com", "The domain to pull from") + cmd.Flags().StringVar(&f.Token, "source-token", "", "Token to pull from the source") } func (f *PullFlags) Validate() Validations { @@ -45,19 +48,19 @@ func Pull(ctx context.Context, flags *PullFlags) error { return err } - return PullManyWithGitImpl(ctx, flags.SourceURL, flags.CacheDir, repoNames, gitImplementation{}) + return PullManyWithGitImpl(ctx, flags.SourceURL, flags.Token, flags.CacheDir, repoNames, gitImplementation{}) } -func PullManyWithGitImpl(ctx context.Context, sourceURL, cacheDir string, repoNames []string, gitimpl GitImplementation) error { +func PullManyWithGitImpl(ctx context.Context, sourceURL, token, cacheDir string, repoNames []string, gitimpl GitImplementation) error { for _, repoName := range repoNames { - if err := PullWithGitImpl(ctx, sourceURL, cacheDir, repoName, gitimpl); err != nil { + if err := PullWithGitImpl(ctx, sourceURL, token, cacheDir, repoName, gitimpl); err != nil { return err } } return nil } -func PullWithGitImpl(ctx context.Context, sourceURL, cacheDir string, repoName string, gitimpl GitImplementation) error { +func PullWithGitImpl(ctx context.Context, sourceURL, token, cacheDir string, repoName string, gitimpl GitImplementation) error { originRepoName, destRepoName, err := extractSourceDest(repoName) if err != nil { return err @@ -68,14 +71,24 @@ func PullWithGitImpl(ctx context.Context, sourceURL, cacheDir string, repoName s return err } + var auth transport.AuthMethod = nil + if token != "" { + auth = &http.BasicAuth{ + Username: "token", + Password: token, + } + } + dst := path.Join(cacheDir, destRepoName) if !gitimpl.RepositoryExists(dst) { fmt.Fprintf(os.Stdout, "pulling %s to %s ...\n", originRepoName, dst) - _, err := gitimpl.CloneRepository(dst, &git.CloneOptions{ + option := &git.CloneOptions{ SingleBranch: false, URL: fmt.Sprintf("%s/%s", sourceURL, originRepoName), - }) + Auth: auth, + } + _, err := gitimpl.CloneRepository(dst, option) if err != nil { if strings.Contains(err.Error(), "authentication required") { return fmt.Errorf("could not pull %s, the repository may require authentication or does not exist", originRepoName) @@ -94,6 +107,7 @@ func PullWithGitImpl(ctx context.Context, sourceURL, cacheDir string, repoName s RefSpecs: []config.RefSpec{ config.RefSpec("+refs/heads/*:refs/heads/*"), }, + Auth: auth, Tags: git.AllTags, }) if err != nil && err != git.NoErrAlreadyUpToDate {