Skip to content
This repository has been archived by the owner on Jan 14, 2022. It is now read-only.

Commit

Permalink
Add support for private repos using env var or netrc
Browse files Browse the repository at this point in the history
  • Loading branch information
codyaray committed Jun 7, 2018
1 parent 0b41696 commit 126e301
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 11 deletions.
72 changes: 67 additions & 5 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

[[constraint]]
name = "github.com/goreleaser/goreleaser"
version = ">=0.66.0"
version = ">=0.77.1"
[prune]
go-tests = true
unused-packages = true
57 changes: 54 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path"
"strings"
Expand All @@ -17,7 +18,10 @@ import (

"github.com/apex/log"
"github.com/apex/log/handlers/cli"
"github.com/bgentry/go-netrc/netrc"
"github.com/client9/codegen/shell"
"github.com/mitchellh/go-homedir"
"golang.org/x/oauth2"
"gopkg.in/alecthomas/kingpin.v2"
)

Expand Down Expand Up @@ -112,7 +116,7 @@ func loadURLs(path, configPath string) (*config.Project, error) {
if file == "" {
continue
}
url := fmt.Sprintf("%s/%s", path, file)
url := fmt.Sprintf("%s/%s?ref=master", path, file)
log.Infof("reading %s", url)
project, err := loadURL(url)
if err != nil {
Expand All @@ -126,7 +130,16 @@ func loadURLs(path, configPath string) (*config.Project, error) {
}

func loadURL(file string) (*config.Project, error) {
resp, err := http.Get(file)
client, err := buildSecureHTTPClient(file)
if err != nil {
return nil, err
}
req, err := http.NewRequest("GET", file, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/vnd.github.raw")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
Expand All @@ -144,6 +157,44 @@ func loadURL(file string) (*config.Project, error) {
return &p, err
}

func buildSecureHTTPClient(file string) (*http.Client, error) {
// Read GITHUB_TOKEN env var
token := os.Getenv("GITHUB_TOKEN")
if token != "" {
return buildHTTPClient(token), nil
}

// Read ~/.netrc login info
fileURL, err := url.Parse(file)
if err != nil {
return nil, err
}
netrcFile, err := homedir.Expand("~/.netrc")
if err != nil {
return nil, err
}
m, err := netrc.FindMachine(netrcFile, fileURL.Host)
if err != nil {
return nil, err
}
if m.Password != "" {
token = m.Password
} else if m.Login != "" {
token = m.Login
}
if token != "" {
return buildHTTPClient(token), nil
}

return http.DefaultClient, nil
}

func buildHTTPClient(token string) *http.Client {
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
client := oauth2.NewClient(nil, ts)
return client
}

func loadFile(file string) (*config.Project, error) {
p, err := config.Load(file)
return &p, err
Expand All @@ -158,7 +209,7 @@ func Load(repo, configPath, file string) (project *config.Project, err error) {
repo = normalizeRepo(repo)
log.Infof("reading repo %q on github", repo)
project, err = loadURLs(
fmt.Sprintf("https://raw.githubusercontent.com/%s/master", repo),
fmt.Sprintf("https://api.github.com/repos/%s/contents", repo),
configPath,
)
} else {
Expand Down
4 changes: 2 additions & 2 deletions shellfn.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 126e301

Please sign in to comment.