github.com/bendemaree/terraform@v0.5.4-0.20150613200311-f50d97d6eee6/config/module/detect_file.go (about) 1 package module 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "runtime" 8 ) 9 10 // FileDetector implements Detector to detect file paths. 11 type FileDetector struct{} 12 13 func (d *FileDetector) Detect(src, pwd string) (string, bool, error) { 14 if len(src) == 0 { 15 return "", false, nil 16 } 17 18 if !filepath.IsAbs(src) { 19 if pwd == "" { 20 return "", true, fmt.Errorf( 21 "relative paths require a module with a pwd") 22 } 23 24 // Stat the pwd to determine if its a symbolic link. If it is, 25 // then the pwd becomes the original directory. Otherwise, 26 // `filepath.Join` below does some weird stuff. 27 // 28 // We just ignore if the pwd doesn't exist. That error will be 29 // caught later when we try to use the URL. 30 if fi, err := os.Lstat(pwd); !os.IsNotExist(err) { 31 if err != nil { 32 return "", true, err 33 } 34 if fi.Mode()&os.ModeSymlink != 0 { 35 pwd, err = os.Readlink(pwd) 36 if err != nil { 37 return "", true, err 38 } 39 } 40 } 41 42 src = filepath.Join(pwd, src) 43 } 44 45 return fmtFileURL(src), true, nil 46 } 47 48 func fmtFileURL(path string) string { 49 if runtime.GOOS == "windows" { 50 // Make sure we're using "/" on Windows. URLs are "/"-based. 51 path = filepath.ToSlash(path) 52 return fmt.Sprintf("file://%s", path) 53 } 54 55 // Make sure that we don't start with "/" since we add that below. 56 if path[0] == '/' { 57 path = path[1:] 58 } 59 return fmt.Sprintf("file:///%s", path) 60 }