Skip to content

Commit 13f2130

Browse files
committed
make tags lookup off by default
1 parent 8eb4824 commit 13f2130

File tree

7 files changed

+79
-60
lines changed

7 files changed

+79
-60
lines changed

apis/apis.go

-6
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ import (
88
"strings"
99
)
1010

11-
const (
12-
OfflineKey = "M2T_OFFLINE"
13-
GithubCredentialsKey = "M2T_GITHUB"
14-
// GitlabsCredentialsKey = "M2T_GITLAB"
15-
)
16-
1711
func get(url string, credsKey string) ([]byte, error) {
1812
req, err := http.NewRequest("GET", url, nil)
1913
if err != nil {

apis/github.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"fmt"
77
"net/url"
88
"strings"
9+
10+
"github.com/dmgk/modules2tuple/flags"
911
)
1012

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

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

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

50-
resp, err := get(url, GithubCredentialsKey)
54+
resp, err := get(url, flags.GithubCredentialsKey)
5155
if err != nil {
5256
if strings.Contains(err.Error(), "API rate limit exceeded") {
5357
return "", errors.New(githubRateLimitError)

flags/flags.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package flags
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"html/template"
7+
"os"
8+
"path"
9+
)
10+
11+
const (
12+
GithubCredentialsKey = "M2T_GITHUB"
13+
// GitlabsCredentialsKey = "M2T_GITLAB"
14+
LookupGithubTagsKey = "M2T_GHTAGS"
15+
OfflineKey = "M2T_OFFLINE"
16+
PrefixKey = "M2T_PREFIX"
17+
)
18+
19+
var (
20+
LookupGithubTags = os.Getenv(LookupGithubTagsKey) == "1"
21+
Offline = os.Getenv(OfflineKey) == "1"
22+
PackagePrefix = os.Getenv(PrefixKey)
23+
ShowVersion = false
24+
)
25+
26+
var helpTemplate = template.Must(template.New("help").Parse(`
27+
Vendor package dependencies and then run {{.Name}} on vendor/modules.txt:
28+
29+
$ go mod vendor
30+
$ {{.Name}} vendor/modules.txt
31+
32+
By default, generated GH_TUPLE entries will place packages under "vendor".
33+
This can be changed by passing different prefix using -prefix option (e.g.
34+
-prefix src).
35+
36+
When generating GL_TUPLE entries, modules2tuple will attempt to use Gitlab
37+
API to resolve short commit IDs and tags to the full 40-character IDs as
38+
required by bsd.sites.mk. If network access is not available or not wanted,
39+
this commit ID translation can be disabled with -offline flag.
40+
`))
41+
42+
func init() {
43+
basename := path.Base(os.Args[0])
44+
if PackagePrefix == "" {
45+
PackagePrefix = "vendor"
46+
}
47+
48+
flag.BoolVar(&LookupGithubTags, "ghtags", LookupGithubTags, fmt.Sprintf("lookup tags with Github API (env %s)", LookupGithubTagsKey))
49+
flag.BoolVar(&Offline, "offline", Offline, fmt.Sprintf("disable all network access (env %s)", OfflineKey))
50+
flag.StringVar(&PackagePrefix, "prefix", PackagePrefix, fmt.Sprintf("package prefix (env %s)", PrefixKey))
51+
flag.BoolVar(&ShowVersion, "v", false, "show version")
52+
53+
flag.Usage = func() {
54+
fmt.Fprintf(os.Stderr, "Usage: %s [options] modules.txt\n", basename)
55+
flag.PrintDefaults()
56+
helpTemplate.Execute(os.Stderr, map[string]string{
57+
"Name": basename,
58+
})
59+
}
60+
}

main.go

+5-45
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import (
44
"flag"
55
"fmt"
66
"os"
7-
"path"
8-
"text/template"
97

10-
"github.com/dmgk/modules2tuple/apis"
8+
"github.com/dmgk/modules2tuple/flags"
119
"github.com/dmgk/modules2tuple/tuple"
1210
)
1311

12+
var version = "devel"
13+
1414
func main() {
1515
flag.Parse()
1616

17-
if flagVersion {
17+
if flags.ShowVersion {
1818
fmt.Fprintln(os.Stderr, version)
1919
os.Exit(0)
2020
}
@@ -27,7 +27,7 @@ func main() {
2727
}
2828

2929
var haveTuples bool
30-
parser := tuple.NewParser(flagPackagePrefix, flagOffline)
30+
parser := tuple.NewParser(flags.PackagePrefix, flags.Offline, flags.LookupGithubTags)
3131
tuples, errors := parser.Load(args[0])
3232
if len(tuples) != 0 {
3333
fmt.Print(tuples)
@@ -41,43 +41,3 @@ func main() {
4141
fmt.Println()
4242
}
4343
}
44-
45-
var helpTemplate = template.Must(template.New("help").Parse(`
46-
Vendor package dependencies and then run {{.Name}} on vendor/modules.txt:
47-
48-
$ go mod vendor
49-
$ {{.Name}} vendor/modules.txt
50-
51-
By default, generated GH_TUPLE entries will place packages under "vendor".
52-
This can be changed by passing different prefix using -prefix option (e.g.
53-
-prefix src).
54-
55-
When generating GL_TUPLE entries, modules2tuple will attempt to use Gitlab
56-
API to resolve short commit IDs and tags to the full 40-character IDs as
57-
required by bsd.sites.mk. If network access is not available or not wanted,
58-
this commit ID translation can be disabled with -offline flag.
59-
`))
60-
61-
var (
62-
flagOffline = os.Getenv(apis.OfflineKey) != ""
63-
flagPackagePrefix = "vendor"
64-
flagVersion = false
65-
)
66-
67-
var version = "devel"
68-
69-
func init() {
70-
basename := path.Base(os.Args[0])
71-
72-
flag.BoolVar(&flagOffline, "offline", flagOffline, "disable network access")
73-
flag.StringVar(&flagPackagePrefix, "prefix", "vendor", "package prefix")
74-
flag.BoolVar(&flagVersion, "v", flagVersion, "show version")
75-
76-
flag.Usage = func() {
77-
fmt.Fprintf(os.Stderr, "Usage: %s [options] modules.txt\n", basename)
78-
flag.PrintDefaults()
79-
helpTemplate.Execute(os.Stderr, map[string]string{
80-
"Name": basename,
81-
})
82-
}
83-
}

tuple/parser.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ import (
1515
type Parser struct {
1616
packagePrefix string
1717
offline bool
18+
lookupTags bool
1819
}
1920

2021
// NewParser creates a new modules.txt parser with given options.
21-
func NewParser(packagePrefix string, offline bool) *Parser {
22-
return &Parser{packagePrefix, offline}
22+
func NewParser(packagePrefix string, offline bool, lookupTags bool) *Parser {
23+
return &Parser{packagePrefix, offline, lookupTags}
2324
}
2425

2526
// Read parses tuples from modules.txt contents provided as io.Reader.
@@ -52,7 +53,7 @@ func (p *Parser) Read(r io.Reader) (Tuples, error) {
5253
if !p.offline {
5354
switch t.Source.(type) {
5455
case GH:
55-
if strings.HasPrefix(t.Tag, "v") {
56+
if p.lookupTags && strings.HasPrefix(t.Tag, "v") {
5657
// Call Gihub API to check tags. Go seem to be able to magically
5758
// translate tags like "v1.0.4" to the "api/v1.0.4" which is really used
5859
// by upstream. We'll try to do the same.

tuple/tuple_online_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestUniqueProjectAndTag(t *testing.T) {
1616
ugorji:go:23ab95ef5dc3:ugorji_go/vendor/github.com/ugorji/go
1717
`
1818

19-
tt, err := NewParser("vendor", false).Read(strings.NewReader(given))
19+
tt, err := NewParser("vendor", false, true).Read(strings.NewReader(given))
2020
if err != nil {
2121
t.Fatal(err)
2222
}

tuple/tuple_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ GL_TUPLE= gitlab-org:gitaly-proto:v1.32.0:gitlab_org_gitaly_proto/vendor/gitlab.
193193
# ::v1.2.3:group_name/vendor/some_unknown.vanity_url.net/account/project
194194
`
195195

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

222-
tt, err := NewParser("vendor", true).Read(strings.NewReader(given))
222+
tt, err := NewParser("vendor", true, false).Read(strings.NewReader(given))
223223
if err != nil {
224224
t.Fatal(err)
225225
}

0 commit comments

Comments
 (0)