getModuleVersions returns a module information at the directory src.
(targetPlatform string, targetArch string, src string)
| 240 | |
| 241 | // getModuleVersions returns a module information at the directory src. |
| 242 | func getModuleVersions(targetPlatform string, targetArch string, src string) (*modfile.File, error) { |
| 243 | cmd := exec.Command("go", "list") |
| 244 | cmd.Env = append(os.Environ(), "GOOS="+platformOS(targetPlatform), "GOARCH="+targetArch) |
| 245 | |
| 246 | tags := append(buildTags[:], platformTags(targetPlatform)...) |
| 247 | |
| 248 | // TODO(hyangah): probably we don't need to add all the dependencies. |
| 249 | cmd.Args = append(cmd.Args, "-m", "-json", "-tags="+strings.Join(tags, ","), "all") |
| 250 | cmd.Dir = src |
| 251 | |
| 252 | output, err := cmd.Output() |
| 253 | if err != nil { |
| 254 | // Module information is not available at src. |
| 255 | return nil, nil |
| 256 | } |
| 257 | |
| 258 | type Module struct { |
| 259 | Main bool |
| 260 | Path string |
| 261 | Version string |
| 262 | Dir string |
| 263 | Replace *Module |
| 264 | } |
| 265 | |
| 266 | f := &modfile.File{} |
| 267 | if err := f.AddModuleStmt("gobind"); err != nil { |
| 268 | return nil, err |
| 269 | } |
| 270 | e := json.NewDecoder(bytes.NewReader(output)) |
| 271 | for { |
| 272 | var mod *Module |
| 273 | err := e.Decode(&mod) |
| 274 | if err != nil && err != io.EOF { |
| 275 | return nil, err |
| 276 | } |
| 277 | if mod != nil { |
| 278 | if mod.Replace != nil { |
| 279 | p, v := mod.Replace.Path, mod.Replace.Version |
| 280 | if modfile.IsDirectoryPath(p) { |
| 281 | // replaced by a local directory |
| 282 | p = mod.Replace.Dir |
| 283 | } |
| 284 | if err := f.AddReplace(mod.Path, mod.Version, p, v); err != nil { |
| 285 | return nil, err |
| 286 | } |
| 287 | } else { |
| 288 | // When the version part is empty, the module is local and mod.Dir represents the location. |
| 289 | if v := mod.Version; v == "" { |
| 290 | if err := f.AddReplace(mod.Path, mod.Version, mod.Dir, ""); err != nil { |
| 291 | return nil, err |
| 292 | } |
| 293 | } else { |
| 294 | if err := f.AddRequire(mod.Path, v); err != nil { |
| 295 | return nil, err |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | } |
no test coverage detected