github.com/michaellihs/golab@v0.1.0-beta3.0.20180726222757-f5cdabc76dfd/cmd/helpers/git_helper.go (about) 1 // Copyright © 2018 Michael Lihs 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package helpers 22 23 import ( 24 "errors" 25 "net" 26 "net/url" 27 "os/exec" 28 "regexp" 29 "strings" 30 ) 31 32 type gitHelper struct { 33 } 34 35 func GitHelper() gitHelper { 36 return gitHelper{} 37 } 38 39 func (g gitHelper) GetRemotes() (string, error) { 40 out, err := exec.Command("git", "remote", "-v").Output() 41 return string(out), err 42 } 43 44 func (g gitHelper) GetRemoteUrl(remotes string) (string, error) { 45 re := regexp.MustCompile("origin\\s*(.+?)\\s*\\(fetch\\)") 46 match := re.FindStringSubmatch(remotes) 47 if len(match) > 1 { 48 return match[1], nil 49 } 50 return "", errors.New("Could not find URL in " + remotes) 51 } 52 53 func (g gitHelper) GetWebUrl(remoteUrl string) (string, error) { 54 if strings.HasPrefix(remoteUrl, "http") { 55 return webifyHttpRemote(remoteUrl) 56 } else if strings.HasPrefix(remoteUrl, "git@") { 57 return webifyGitRemote(remoteUrl) 58 } else if strings.HasPrefix(remoteUrl, "ssh") { 59 return webifySshRemote(remoteUrl) 60 } else { 61 return "", errors.New("Cannot parse remote URL: " + remoteUrl) 62 } 63 } 64 65 func webifySshRemote(remoteUrl string) (string, error) { 66 u, err := url.Parse(remoteUrl) 67 if err != nil { 68 return "", err 69 } 70 host := "" 71 if strings.Contains(u.Host, ":") { 72 host, _, err = net.SplitHostPort(u.Host) 73 } else { 74 host = u.Host 75 } 76 return "https://" + host + strings.TrimSuffix(u.Path, ".git"), nil 77 } 78 79 func webifyGitRemote(remoteUrl string) (string, error) { 80 url := strings.TrimRight(remoteUrl[4:], ".git") 81 url = strings.Replace(url, ":", "/", 1) 82 return "https://" + url, nil 83 } 84 85 func webifyHttpRemote(remoteUrl string) (string, error) { 86 if strings.HasSuffix(remoteUrl, ".git") { 87 return strings.TrimSuffix(remoteUrl, ".git"), nil 88 } 89 return remoteUrl, nil 90 }