Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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 {
Expand Down