7 "github.com/hashicorp/go-getter/helper/url"
10 // Detector defines the interface that an invalid URL or a URL with a blank
11 // scheme is passed through in order to determine if its shorthand for
12 // something else well-known.
13 type Detector interface {
14 // Detect will detect whether the string matches a known pattern to
15 // turn it into a proper URL.
16 Detect(string, string) (string, bool, error)
19 // Detectors is the list of detectors that are tried on an invalid URL.
20 // This is also the order they're tried (index 0 is first).
21 var Detectors []Detector
24 Detectors = []Detector{
27 new(BitBucketDetector),
34 // Detect turns a source string into another source string if it is
35 // detected to be of a known pattern.
37 // The third parameter should be the list of detectors to use in the
38 // order to try them. If you don't want to configure this, just use
39 // the global Detectors variable.
41 // This is safe to be called with an already valid source string: Detect
42 // will just return it.
43 func Detect(src string, pwd string, ds []Detector) (string, error) {
44 getForce, getSrc := getForcedGetter(src)
46 // Separate out the subdir if there is one, we don't pass that to detect
47 getSrc, subDir := SourceDirSubdir(getSrc)
49 u, err := url.Parse(getSrc)
50 if err == nil && u.Scheme != "" {
55 for _, d := range ds {
56 result, ok, err := d.Detect(getSrc, pwd)
64 var detectForce string
65 detectForce, result = getForcedGetter(result)
66 result, detectSubdir := SourceDirSubdir(result)
68 // If we have a subdir from the detection, then prepend it to our
70 if detectSubdir != "" {
72 subDir = filepath.Join(detectSubdir, subDir)
79 u, err := url.Parse(result)
81 return "", fmt.Errorf("Error parsing URL: %s", err)
83 u.Path += "//" + subDir
85 // a subdir may contain wildcards, but in order to support them we
86 // have to ensure the path isn't escaped.
92 // Preserve the forced getter if it exists. We try to use the
93 // original set force first, followed by any force set by the
96 result = fmt.Sprintf("%s::%s", getForce, result)
97 } else if detectForce != "" {
98 result = fmt.Sprintf("%s::%s", detectForce, result)
104 return "", fmt.Errorf("invalid source string: %s", src)