Skip to content

Commit

Permalink
Merge pull request #33 from hmiyado/clone-private-repository
Browse files Browse the repository at this point in the history
Accept github access token to clone private repository
  • Loading branch information
hmiyado authored Sep 7, 2022
2 parents 5604109 + a89cc23 commit 2ed0fe0
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion internal/cli/command_releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/hmiyado/four-keys/internal/core"
"github.com/urfave/cli/v2"
Expand All @@ -20,6 +21,11 @@ func getCommandReleasesFlags() []cli.Flag {
Usage: "the remote repository url. repository will be cloned in memory",
DefaultText: "local repository of current directory",
},
&cli.StringFlag{
Name: "accessToken",
Usage: "GitHub access token to clone private repository",
DefaultText: "no access token",
},
&cli.TimestampFlag{
Name: "since",
Usage: "the start date to query releases (inclusive)",
Expand Down Expand Up @@ -110,16 +116,25 @@ func (c *CliContextWrapper) Repository() (*git.Repository, error) {
c.StartTimer("Open Repository")
defer c.StopTimer("Open Repository")
repositoryUrl := c.context.String("repository")
accessToken := c.context.String("accessToken")
var repository *git.Repository
var error error
var auth *http.BasicAuth
if accessToken != "" {
auth = &http.BasicAuth{
Username: "four-keys",
Password: accessToken,
}
}
if repositoryUrl == "" {
repository, error = git.PlainOpenWithOptions("./", &git.PlainOpenOptions{DetectDotGit: true, EnableDotGitCommonDir: false})
if error != nil {
return nil, errors.New("cannot open repository at current directory")
}
} else {
repository, error = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: repositoryUrl,
Auth: auth,
URL: repositoryUrl,
})
if error != nil {
return nil, fmt.Errorf("cannot clone repository: %v", repositoryUrl)
Expand Down

0 comments on commit 2ed0fe0

Please sign in to comment.