Skip to content

Commit d8abd35

Browse files
committed
add install command
1 parent e83dc3b commit d8abd35

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
_url "net/url"
66
"os"
7+
"strings"
78
"time"
89

910
"github.com/adohkan/git-remote-https-iap/internal/git"
@@ -30,14 +31,19 @@ var (
3031
versionCmd = &cobra.Command{
3132
Use: "version",
3233
Short: "Print version number",
33-
Run: func(cmd *cobra.Command, args []string) {
34-
fmt.Printf("%s %s\n", BinaryName, version)
35-
},
34+
Run: printVersion,
35+
}
36+
37+
installProtocolCmd = &cobra.Command{
38+
Use: "install",
39+
Short: "Install protocol in Git config",
40+
Run: installGitProtocol,
3641
}
3742
)
3843

3944
func init() {
4045
rootCmd.AddCommand(versionCmd)
46+
rootCmd.AddCommand(installProtocolCmd)
4147

4248
// set default log level
4349
zerolog.SetGlobalLevel(zerolog.DebugLevel)
@@ -58,6 +64,16 @@ func execute(cmd *cobra.Command, args []string) {
5864
git.PassThruRemoteHTTPSHelper(remote, url)
5965
}
6066

67+
func printVersion(cmd *cobra.Command, args []string) {
68+
fmt.Printf("%s %s\n", BinaryName, version)
69+
}
70+
71+
func installGitProtocol(cmd *cobra.Command, args []string) {
72+
p := strings.TrimLeft(BinaryName, "git-remote-")
73+
git.InstallProtocol(p)
74+
log.Info().Msgf("%s protocol configured in git!", p)
75+
}
76+
6177
func handleIAPAuthCookieFor(url string) {
6278
// All our work will be based on the basedomain of the provided URL
6379
// as IAP would be setup for the whole domain.

internal/git/git.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,12 @@ func GetCredentials(protocol, host, username string) (string, error) {
9191

9292
return "", fmt.Errorf("GetCredentials - not found for protocol=%s,host=%s,username=%s", protocol, host, username)
9393
}
94+
95+
func InstallProtocol(protocol string) {
96+
protocol = fmt.Sprintf("protocol.%s.allow", protocol)
97+
args := []string{"config", "--global", protocol, "always"}
98+
cmd := exec.Command(GitBinary, args...)
99+
if err := cmd.Run(); err != nil {
100+
log.Fatal().Msgf("InstallProtocol - %s", err)
101+
}
102+
}

0 commit comments

Comments
 (0)