Skip to content

Commit 246ad0e

Browse files
committed
chore: snapshot
1 parent b4b5565 commit 246ad0e

File tree

7 files changed

+154
-70
lines changed

7 files changed

+154
-70
lines changed

internal/chezmoi/github.go

+39-7
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,16 @@ import (
44
"context"
55
"net/http"
66
"os"
7+
"strings"
78

89
"github.com/google/go-github/v62/github"
910
"golang.org/x/oauth2"
1011
)
1112

1213
// NewGitHubClient returns a new github.Client configured with an access token
1314
// and a http client, if available.
14-
func NewGitHubClient(ctx context.Context, httpClient *http.Client) *github.Client {
15-
for _, key := range []string{
16-
"CHEZMOI_GITHUB_ACCESS_TOKEN",
17-
"CHEZMOI_GITHUB_TOKEN",
18-
"GITHUB_ACCESS_TOKEN",
19-
"GITHUB_TOKEN",
20-
} {
15+
func NewGitHubClient(ctx context.Context, httpClient *http.Client, host string) *github.Client {
16+
for _, key := range accessTokenEnvKeys(host) {
2117
if accessToken := os.Getenv(key); accessToken != "" {
2218
httpClient = oauth2.NewClient(
2319
context.WithValue(ctx, oauth2.HTTPClient, httpClient),
@@ -29,3 +25,39 @@ func NewGitHubClient(ctx context.Context, httpClient *http.Client) *github.Clien
2925
}
3026
return github.NewClient(httpClient)
3127
}
28+
29+
func accessTokenEnvKeys(host string) []string {
30+
var keys []string
31+
for _, chezmoiKey := range []string{"CHEZMOI", ""} {
32+
hostKeys := []string{makeHostKey(host)}
33+
if host == "github.com" {
34+
hostKeys = append(hostKeys, "GITHUB")
35+
}
36+
for _, hostKey := range hostKeys {
37+
for _, accessKey := range []string{"ACCESS", ""} {
38+
key := strings.Join(nonEmpty([]string{chezmoiKey, hostKey, accessKey, "TOKEN"}), "_")
39+
keys = append(keys, key)
40+
}
41+
}
42+
}
43+
return keys
44+
}
45+
46+
func makeHostKey(host string) string {
47+
// FIXME split host on non-ASCII characters
48+
// FIXME convert everything to uppercase
49+
// FIXME join components with _
50+
return host
51+
}
52+
53+
func nonEmpty[S []T, T comparable](s S) S {
54+
// FIXME use something from slices for this
55+
result := make([]T, 0, len(s))
56+
var zero T
57+
for _, e := range s {
58+
if e != zero {
59+
result = append(result, e)
60+
}
61+
}
62+
return result
63+
}

internal/chezmoi/github_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package chezmoi
2+
3+
import (
4+
"testing"
5+
6+
"github.com/alecthomas/assert/v2"
7+
)
8+
9+
func TestAccessTokenEnvKeys(t *testing.T) {
10+
for _, tc := range []struct {
11+
host string
12+
expected []string
13+
}{
14+
{
15+
expected: []string{
16+
"CHEZMOI_GITHUB_ACCESS_TOKEN",
17+
"CHEZMOI_GITHUB_TOKEN",
18+
"GITHUB_ACCESS_TOKEN",
19+
"GITHUB_TOKEN",
20+
},
21+
},
22+
} {
23+
t.Run(tc.host, func(t *testing.T) {
24+
assert.Equal(t, tc.expected, accessTokenEnvKeys(tc.host))
25+
})
26+
}
27+
}

internal/cmd/config.go

+5
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,11 @@ func newConfig(options ...configOption) (*Config, error) {
340340
homeDir: userHomeDir,
341341
templateFuncs: sprig.TxtFuncMap(),
342342

343+
// Password manager data.
344+
gitHub: gitHubData{
345+
clientsByHost: make(map[string]gitHubClientResult),
346+
},
347+
343348
// Command configurations.
344349
apply: applyCmdConfig{
345350
filter: chezmoi.NewEntryTypeFilter(chezmoi.EntryTypesAll, chezmoi.EntryTypesNone),

internal/cmd/doctorcmd.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ func (c *latestVersionCheck) Run(system chezmoi.System, homeDirAbsPath chezmoi.A
659659

660660
ctx := context.Background()
661661

662-
gitHubClient := chezmoi.NewGitHubClient(ctx, c.httpClient)
662+
gitHubClient := chezmoi.NewGitHubClient(ctx, c.httpClient, "github.com")
663663
rr, _, err := gitHubClient.Repositories.GetLatestRelease(ctx, "twpayne", "chezmoi")
664664
var rateLimitErr *github.RateLimitError
665665
var abuseRateLimitErr *github.AbuseRateLimitError

0 commit comments

Comments
 (0)