Skip to content

Commit a1a39ce

Browse files
committed
Allows a single configure call for multiple subdomains
1 parent a587531 commit a1a39ce

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
@@ -112,11 +112,23 @@ func configureIAP(cmd *cobra.Command, args []string) {
112112
git.SetGlobalConfig(https, "iap", "clientID", clientID)
113113

114114
// let users manipulate standard 'https://' urls
115-
httpsIAP := fmt.Sprintf("https+iap://%s", repo.Host)
116-
git.SetGlobalConfig(httpsIAP, "url", "insteadOf", https)
115+
insteadOf := &git.GitConfig{
116+
Url: fmt.Sprintf("https+iap://%s", repo.Host),
117+
Section: "url",
118+
Key: "insteadOf",
119+
Value: https,
120+
}
121+
if strings.Contains(repo.Host, "*") {
122+
log.Warn().Msg("While config is valid for wildcard hosts, git IAP auth requires \"insteadOf\" config for actual hosts")
123+
log.Info().Msg("Actual hosts must be manually configured as follows (* replaced with subdomain):")
124+
log.Info().Msg(insteadOf.CommandSuggestGlobal())
125+
} else {
126+
git.SetConfigGlobal(insteadOf)
127+
}
117128

118129
// set cookie path
119130
domainSlug := strings.ReplaceAll(repo.Host, ".", "-")
131+
domainSlug = strings.ReplaceAll(domainSlug, "*", "_wildcard_")
120132
cookiePath := fmt.Sprintf("~/.config/gcp-iap/%s.cookie", domainSlug)
121133
git.SetGlobalConfig(https, "http", "cookieFile", cookiePath)
122134
}

internal/git/git.go

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

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

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

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

0 commit comments

Comments
 (0)