See https://go.dev/ref/mod#go-mod-file for the spec.
(path string)
| 17 | |
| 18 | // See https://go.dev/ref/mod#go-mod-file for the spec. |
| 19 | func ParseModule(path string) (Module, error) { |
| 20 | data, err := os.ReadFile(path) |
| 21 | if err != nil { |
| 22 | return Module{}, err |
| 23 | } |
| 24 | |
| 25 | for _, line := range strings.Split(string(data), "\n") { |
| 26 | line = strings.TrimSpace(line) |
| 27 | // is it a module defintion line? |
| 28 | if strings.HasPrefix(line, "module ") { |
| 29 | // trim the prefix |
| 30 | modulename := strings.TrimPrefix(line, "module ") |
| 31 | // cut any trailing comments, if there are any |
| 32 | modulename, _, _ = strings.Cut(modulename, "//") |
| 33 | modulename = strings.TrimSpace(modulename) |
| 34 | |
| 35 | return Module{ |
| 36 | Prefix: modulename + "/", |
| 37 | Dir: filepath.Dir(path), |
| 38 | }, nil |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return Module{}, errors.New("invalid module") |
| 43 | } |
| 44 | |
| 45 | func TranslatePath(pkgpath string, modules []Module) (path string, found bool) { |
| 46 | for _, module := range modules { |