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{
26 new(BitBucketDetector),
32 // Detect turns a source string into another source string if it is
33 // detected to be of a known pattern.
35 // The third parameter should be the list of detectors to use in the
36 // order to try them. If you don't want to configure this, just use
37 // the global Detectors variable.
39 // This is safe to be called with an already valid source string: Detect
40 // will just return it.
41 func Detect(src string, pwd string, ds []Detector) (string, error) {
42 getForce, getSrc := getForcedGetter(src)
44 // Separate out the subdir if there is one, we don't pass that to detect
45 getSrc, subDir := SourceDirSubdir(getSrc)
47 u, err := url.Parse(getSrc)
48 if err == nil && u.Scheme != "" {
53 for _, d := range ds {
54 result, ok, err := d.Detect(getSrc, pwd)
62 var detectForce string
63 detectForce, result = getForcedGetter(result)
64 result, detectSubdir := SourceDirSubdir(result)
66 // If we have a subdir from the detection, then prepend it to our
68 if detectSubdir != "" {
70 subDir = filepath.Join(detectSubdir, subDir)
76 u, err := url.Parse(result)
78 return "", fmt.Errorf("Error parsing URL: %s", err)
80 u.Path += "//" + subDir
84 // Preserve the forced getter if it exists. We try to use the
85 // original set force first, followed by any force set by the
88 result = fmt.Sprintf("%s::%s", getForce, result)
89 } else if detectForce != "" {
90 result = fmt.Sprintf("%s::%s", detectForce, result)
96 return "", fmt.Errorf("invalid source string: %s", src)