Skip to content

Commit

Permalink
make tags lookup off by default
Browse files Browse the repository at this point in the history
  • Loading branch information
dmgk committed Apr 7, 2020
1 parent 8eb4824 commit 13f2130
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 60 deletions.
6 changes: 0 additions & 6 deletions apis/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ import (
"strings"
)

const (
OfflineKey = "M2T_OFFLINE"
GithubCredentialsKey = "M2T_GITHUB"
// GitlabsCredentialsKey = "M2T_GITLAB"
)

func get(url string, credsKey string) ([]byte, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
Expand Down
10 changes: 7 additions & 3 deletions apis/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"net/url"
"strings"

"github.com/dmgk/modules2tuple/flags"
)

type GithubCommit struct {
Expand All @@ -21,13 +23,15 @@ var githubRateLimitError = fmt.Sprintf(`Github API rate limit exceeded. Please e
to let modules2tuple call Github API using basic authentication.
To create a new token, navigate to https://github.com/settings/tokens/new
(leave all checkboxes unchecked, modules2tuple doesn't need any access to your account)
- set %s=1 or pass "-offline" flag to module2tuple to disable network access`, OfflineKey, GithubCredentialsKey)
- set %s=0 and/or remove "-ghtags" flag to turn off Github tags lookup
- set %s=1 or pass "-offline" flag to module2tuple to disable network access`,
flags.GithubCredentialsKey, flags.LookupGithubTagsKey, flags.OfflineKey)

func GetGithubCommit(account, project, tag string) (string, error) {
projectID := fmt.Sprintf("%s/%s", url.PathEscape(account), url.PathEscape(project))
url := fmt.Sprintf("https://api.github.com/repos/%s/commits/%s", projectID, tag)

resp, err := get(url, GithubCredentialsKey)
resp, err := get(url, flags.GithubCredentialsKey)
if err != nil {
if strings.Contains(err.Error(), "API rate limit exceeded") {
return "", errors.New(githubRateLimitError)
Expand All @@ -47,7 +51,7 @@ func LookupGithubTag(account, project, tag string) (string, error) {
projectID := fmt.Sprintf("%s/%s", url.PathEscape(account), url.PathEscape(project))
url := fmt.Sprintf("https://api.github.com/repos/%s/git/refs/tags", projectID)

resp, err := get(url, GithubCredentialsKey)
resp, err := get(url, flags.GithubCredentialsKey)
if err != nil {
if strings.Contains(err.Error(), "API rate limit exceeded") {
return "", errors.New(githubRateLimitError)
Expand Down
60 changes: 60 additions & 0 deletions flags/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package flags

import (
"flag"
"fmt"
"html/template"
"os"
"path"
)

const (
GithubCredentialsKey = "M2T_GITHUB"
// GitlabsCredentialsKey = "M2T_GITLAB"
LookupGithubTagsKey = "M2T_GHTAGS"
OfflineKey = "M2T_OFFLINE"
PrefixKey = "M2T_PREFIX"
)

var (
LookupGithubTags = os.Getenv(LookupGithubTagsKey) == "1"
Offline = os.Getenv(OfflineKey) == "1"
PackagePrefix = os.Getenv(PrefixKey)
ShowVersion = false
)

var helpTemplate = template.Must(template.New("help").Parse(`
Vendor package dependencies and then run {{.Name}} on vendor/modules.txt:
$ go mod vendor
$ {{.Name}} vendor/modules.txt
By default, generated GH_TUPLE entries will place packages under "vendor".
This can be changed by passing different prefix using -prefix option (e.g.
-prefix src).
When generating GL_TUPLE entries, modules2tuple will attempt to use Gitlab
API to resolve short commit IDs and tags to the full 40-character IDs as
required by bsd.sites.mk. If network access is not available or not wanted,
this commit ID translation can be disabled with -offline flag.
`))

func init() {
basename := path.Base(os.Args[0])
if PackagePrefix == "" {
PackagePrefix = "vendor"
}

flag.BoolVar(&LookupGithubTags, "ghtags", LookupGithubTags, fmt.Sprintf("lookup tags with Github API (env %s)", LookupGithubTagsKey))
flag.BoolVar(&Offline, "offline", Offline, fmt.Sprintf("disable all network access (env %s)", OfflineKey))
flag.StringVar(&PackagePrefix, "prefix", PackagePrefix, fmt.Sprintf("package prefix (env %s)", PrefixKey))
flag.BoolVar(&ShowVersion, "v", false, "show version")

flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] modules.txt\n", basename)
flag.PrintDefaults()
helpTemplate.Execute(os.Stderr, map[string]string{
"Name": basename,
})
}
}
50 changes: 5 additions & 45 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import (
"flag"
"fmt"
"os"
"path"
"text/template"

"github.com/dmgk/modules2tuple/apis"
"github.com/dmgk/modules2tuple/flags"
"github.com/dmgk/modules2tuple/tuple"
)

var version = "devel"

func main() {
flag.Parse()

if flagVersion {
if flags.ShowVersion {
fmt.Fprintln(os.Stderr, version)
os.Exit(0)
}
Expand All @@ -27,7 +27,7 @@ func main() {
}

var haveTuples bool
parser := tuple.NewParser(flagPackagePrefix, flagOffline)
parser := tuple.NewParser(flags.PackagePrefix, flags.Offline, flags.LookupGithubTags)
tuples, errors := parser.Load(args[0])
if len(tuples) != 0 {
fmt.Print(tuples)
Expand All @@ -41,43 +41,3 @@ func main() {
fmt.Println()
}
}

var helpTemplate = template.Must(template.New("help").Parse(`
Vendor package dependencies and then run {{.Name}} on vendor/modules.txt:
$ go mod vendor
$ {{.Name}} vendor/modules.txt
By default, generated GH_TUPLE entries will place packages under "vendor".
This can be changed by passing different prefix using -prefix option (e.g.
-prefix src).
When generating GL_TUPLE entries, modules2tuple will attempt to use Gitlab
API to resolve short commit IDs and tags to the full 40-character IDs as
required by bsd.sites.mk. If network access is not available or not wanted,
this commit ID translation can be disabled with -offline flag.
`))

var (
flagOffline = os.Getenv(apis.OfflineKey) != ""
flagPackagePrefix = "vendor"
flagVersion = false
)

var version = "devel"

func init() {
basename := path.Base(os.Args[0])

flag.BoolVar(&flagOffline, "offline", flagOffline, "disable network access")
flag.StringVar(&flagPackagePrefix, "prefix", "vendor", "package prefix")
flag.BoolVar(&flagVersion, "v", flagVersion, "show version")

flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] modules.txt\n", basename)
flag.PrintDefaults()
helpTemplate.Execute(os.Stderr, map[string]string{
"Name": basename,
})
}
}
7 changes: 4 additions & 3 deletions tuple/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ import (
type Parser struct {
packagePrefix string
offline bool
lookupTags bool
}

// NewParser creates a new modules.txt parser with given options.
func NewParser(packagePrefix string, offline bool) *Parser {
return &Parser{packagePrefix, offline}
func NewParser(packagePrefix string, offline bool, lookupTags bool) *Parser {
return &Parser{packagePrefix, offline, lookupTags}
}

// Read parses tuples from modules.txt contents provided as io.Reader.
Expand Down Expand Up @@ -52,7 +53,7 @@ func (p *Parser) Read(r io.Reader) (Tuples, error) {
if !p.offline {
switch t.Source.(type) {
case GH:
if strings.HasPrefix(t.Tag, "v") {
if p.lookupTags && strings.HasPrefix(t.Tag, "v") {
// Call Gihub API to check tags. Go seem to be able to magically
// translate tags like "v1.0.4" to the "api/v1.0.4" which is really used
// by upstream. We'll try to do the same.
Expand Down
2 changes: 1 addition & 1 deletion tuple/tuple_online_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestUniqueProjectAndTag(t *testing.T) {
ugorji:go:23ab95ef5dc3:ugorji_go/vendor/github.com/ugorji/go
`

tt, err := NewParser("vendor", false).Read(strings.NewReader(given))
tt, err := NewParser("vendor", false, true).Read(strings.NewReader(given))
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions tuple/tuple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ GL_TUPLE= gitlab-org:gitaly-proto:v1.32.0:gitlab_org_gitaly_proto/vendor/gitlab.
# ::v1.2.3:group_name/vendor/some_unknown.vanity_url.net/account/project
`

tt, err := NewParser("vendor", true).Read(strings.NewReader(given))
tt, err := NewParser("vendor", true, false).Read(strings.NewReader(given))
if err == nil {
t.Fatal("expected err to not be nil")
}
Expand All @@ -219,7 +219,7 @@ func TestUniqueGroups(t *testing.T) {
minio:parquet-go:9d767baf1679:minio_parquet_go/vendor/github.com/minio/parquet-go
`

tt, err := NewParser("vendor", true).Read(strings.NewReader(given))
tt, err := NewParser("vendor", true, false).Read(strings.NewReader(given))
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 13f2130

Please sign in to comment.