github.com/hashicorp/go-getter/v2@v2.2.2/detect_ssh.go (about) 1 package getter 2 3 import ( 4 "fmt" 5 "net/url" 6 "regexp" 7 "strings" 8 ) 9 10 // Note that we do not have an SSH-getter currently so this file serves 11 // only to hold the detectSSH helper that is used by other detectors. 12 13 // sshPattern matches SCP-like SSH patterns (user@host:path) 14 var sshPattern = regexp.MustCompile("^(?:([^@]+)@)?([^:]+):/?(.+)$") 15 16 // detectSSH determines if the src string matches an SSH-like URL and 17 // converts it into a net.URL compatible string. This returns nil if the 18 // string doesn't match the SSH pattern. 19 // 20 // This function is tested indirectly via detect_git_test.go 21 func detectSSH(src string) (*url.URL, error) { 22 matched := sshPattern.FindStringSubmatch(src) 23 if matched == nil { 24 return nil, nil 25 } 26 27 user := matched[1] 28 host := matched[2] 29 path := matched[3] 30 qidx := strings.Index(path, "?") 31 if qidx == -1 { 32 qidx = len(path) 33 } 34 35 var u url.URL 36 u.Scheme = "ssh" 37 u.User = url.User(user) 38 u.Host = host 39 u.Path = path[0:qidx] 40 if qidx < len(path) { 41 q, err := url.ParseQuery(path[qidx+1:]) 42 if err != nil { 43 return nil, fmt.Errorf("error parsing GitHub SSH URL: %s", err) 44 } 45 u.RawQuery = q.Encode() 46 } 47 48 return &u, nil 49 }