github.com/jgadling/terraform@v0.3.8-0.20150227214559-abd68c2c87bc/config/module/detect_file.go (about) 1 package module 2 3 import ( 4 "fmt" 5 "path/filepath" 6 "runtime" 7 ) 8 9 // FileDetector implements Detector to detect file paths. 10 type FileDetector struct{} 11 12 func (d *FileDetector) Detect(src, pwd string) (string, bool, error) { 13 if len(src) == 0 { 14 return "", false, nil 15 } 16 17 if !filepath.IsAbs(src) { 18 if pwd == "" { 19 return "", true, fmt.Errorf( 20 "relative paths require a module with a pwd") 21 } 22 23 src = filepath.Join(pwd, src) 24 } 25 return fmtFileURL(src), true, nil 26 } 27 28 func fmtFileURL(path string) string { 29 if runtime.GOOS == "windows" { 30 // Make sure we're using "/" on Windows. URLs are "/"-based. 31 path = filepath.ToSlash(path) 32 return fmt.Sprintf("file://%s", path) 33 } 34 35 // Make sure that we don't start with "/" since we add that below. 36 if path[0] == '/' { 37 path = path[1:] 38 } 39 return fmt.Sprintf("file:///%s", path) 40 }