Skip to content

Commit 5f2b7f7

Browse files
authored
Merge pull request #4 from solsson/wildcard-hosts
Add support for wildcard hosts
2 parents 4d8b466 + 5017518 commit 5f2b7f7

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

cmd/git-remote-https+iap/main.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,23 @@ func configureIAP(cmd *cobra.Command, args []string) {
126126
git.SetGlobalConfig(https, "iap", "clientID", clientID)
127127

128128
// let users manipulate standard 'https://' urls
129-
httpsIAP := fmt.Sprintf("https+iap://%s", repo.Host)
130-
git.SetGlobalConfig(httpsIAP, "url", "insteadOf", https)
129+
insteadOf := &git.GitConfig{
130+
Url: fmt.Sprintf("https+iap://%s", repo.Host),
131+
Section: "url",
132+
Key: "insteadOf",
133+
Value: https,
134+
}
135+
if strings.Contains(repo.Host, "*") {
136+
log.Warn().Msg("While config is valid for wildcard hosts, transparent support for https:// remotes require \"insteadOf\" config")
137+
log.Info().Msg("Actual hosts must be manually configured as follows (with * replaced by subdomain):")
138+
log.Info().Msg(insteadOf.CommandSuggestGlobal())
139+
} else {
140+
git.SetConfigGlobal(insteadOf)
141+
}
131142

132143
// set cookie path
133144
domainSlug := strings.ReplaceAll(repo.Host, ".", "-")
145+
domainSlug = strings.ReplaceAll(domainSlug, "*", "_wildcard_")
134146
cookiePath := fmt.Sprintf("~/.config/gcp-iap/%s.cookie", domainSlug)
135147
git.SetGlobalConfig(https, "http", "cookieFile", cookiePath)
136148
}

internal/git/git.go

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,25 @@ const (
1717
GitBinary = "git"
1818
)
1919

20+
type GitConfig struct {
21+
Url string
22+
Section string
23+
Key string
24+
Value string
25+
}
26+
27+
func (c *GitConfig) Name() string {
28+
return fmt.Sprintf("%s.%s.%s", c.Section, c.Url, c.Key)
29+
}
30+
31+
func (c *GitConfig) ArgsGlobal() []string {
32+
return []string{"config", "--global", c.Name(), c.Value}
33+
}
34+
35+
func (c *GitConfig) CommandSuggestGlobal() string {
36+
return fmt.Sprintf("git %s", strings.Join(c.ArgsGlobal(), " "))
37+
}
38+
2039
// ConfigGetURLMatch call 'git config --get-urlmatch' underneath
2140
func ConfigGetURLMatch(key, url string) string {
2241
var stdout bytes.Buffer
@@ -32,16 +51,23 @@ func ConfigGetURLMatch(key, url string) string {
3251
return strings.TrimSpace(string(stdout.Bytes()))
3352
}
3453

54+
// SetConfigGlobal is a new signature for SetGlobalConfig
55+
func SetConfigGlobal(config *GitConfig) {
56+
cmd := exec.Command(GitBinary, config.ArgsGlobal()...)
57+
if err := cmd.Run(); err != nil {
58+
log.Fatal().Msgf("SetGlobalConfig - could not set config '%s': %s", config.Name(), err)
59+
}
60+
}
61+
3562
// SetGlobalConfig allows to set system-wide Git configuration.
3663
// The application exits in case of error.
3764
func SetGlobalConfig(url, section, key, value string) {
38-
x := fmt.Sprintf("%s.%s.%s", section, url, key)
39-
args := []string{"config", "--global", x, value}
40-
cmd := exec.Command(GitBinary, args...)
41-
42-
if err := cmd.Run(); err != nil {
43-
log.Fatal().Msgf("SetGlobalConfig - could not set config '%s': %s", x, err)
44-
}
65+
SetConfigGlobal(&GitConfig{
66+
Url: url,
67+
Section: section,
68+
Key: key,
69+
Value: value,
70+
})
4571
}
4672

4773
// PassThruRemoteHTTPSHelper exec the git-remote-https helper,

0 commit comments

Comments
 (0)