(src, pwd string)
| 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" { |
nothing calls this directly
no test coverage detected