github.com/hashicorp/go-getter/v2@v2.2.2/detect.go (about) 1 package getter 2 3 import ( 4 "fmt" 5 6 "github.com/hashicorp/go-getter/v2/helper/url" 7 ) 8 9 // Detector defines the interface that an invalid URL or a URL with a blank 10 // scheme is passed through in order to determine if its shorthand for 11 // something else well-known. 12 type Detector interface { 13 // Detect will detect whether the string matches a known pattern to 14 // turn it into a proper URL. 15 Detect(string, string) (string, bool, error) 16 } 17 18 // Detect is a method used to detect if a Getter is a candidate for downloading an artifact 19 // by calling the Getter.Detect(*Request) method 20 func Detect(req *Request, getter Getter) (bool, error) { 21 originalSrc := req.Src 22 23 getForce, getSrc := getForcedGetter(req.Src) 24 if getForce != "" { 25 req.Forced = getForce 26 } 27 28 req.Src = getSrc 29 ok, err := getter.Detect(req) 30 if err != nil { 31 return true, err 32 } 33 if !ok { 34 // Write back the original source 35 req.Src = originalSrc 36 return ok, nil 37 } 38 39 result, detectSubdir := SourceDirSubdir(req.Src) 40 41 // If we have a subdir from the detection, then prepend it to our 42 // requested subdir. 43 if detectSubdir != "" { 44 u, err := url.Parse(result) 45 if err != nil { 46 return true, fmt.Errorf("Error parsing URL: %s", err) 47 } 48 u.Path += "//" + detectSubdir 49 50 // a subdir may contain wildcards, but in order to support them we 51 // have to ensure the path isn't escaped. 52 u.RawPath = u.Path 53 54 result = u.String() 55 } 56 57 req.Src = result 58 return true, nil 59 }