github.com/remind101/go-getter@v0.0.0-20180809191950-4bda8fa99001/detect_file.go (about) 1 package getter 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 = filepath.EvalSymlinks(pwd) 36 if err != nil { 37 return "", true, err 38 } 39 40 // The symlink itself might be a relative path, so we have to 41 // resolve this to have a correctly rooted URL. 42 pwd, err = filepath.Abs(pwd) 43 if err != nil { 44 return "", true, err 45 } 46 } 47 } 48 49 src = filepath.Join(pwd, src) 50 } 51 52 return fmtFileURL(src), true, nil 53 } 54 55 func fmtFileURL(path string) string { 56 if runtime.GOOS == "windows" { 57 // Make sure we're using "/" on Windows. URLs are "/"-based. 58 path = filepath.ToSlash(path) 59 return fmt.Sprintf("file://%s", path) 60 } 61 62 // Make sure that we don't start with "/" since we add that below. 63 if path[0] == '/' { 64 path = path[1:] 65 } 66 return fmt.Sprintf("file:///%s", path) 67 }