gomod
package gomod
import "go.abhg.dev/doc2go/internal/gomod"
Package gomod provides means of interacting with go.mod files. This includes parsing them and searching through them effectively.
Index
Types
type Builder
type Builder struct { Logger *log.Logger // required }
Builder builds a Tree containing module data for the workspace based on the given packages.
func (*Builder) Build
func (b *Builder) Build(pkgs []*gosrc.PackageRef) *Tree
Build creates a Tree populated with module dependencies from the go.mod files of the given packages.
The returned Tree may be empty.
type Module
type Module struct { // Path is the module path (e.g., "go.uber.org/zap"). Path string // required // Version is the module version (e.g., "v1.27.1"). // This may be a pseudo-version. Version string // required }
Module identifies a Go module.
type Tree
type Tree struct { // contains filtered or unexported fields }
Tree provides module version information inside a multi-module environment.
As a workspace may have multiple go.mod files (e.g., in a monorepo), we need to resolve which go.mod file applies when looking at an import path. We do so by also looking at the source import path (i.e., the package importing the target), and finding _its_ go.mod file.
So lookups are two-level:
- The first level resolves which go.mod file to use.
- The second level resolves the module path for an import path within the selected go.mod file.
Zero value of tree is an empty tree.
func (*Tree) LookupModuleDep
func (t *Tree) LookupModuleDep(sourceImportPath, targetImportPath string) *Module
LookupModuleDep retreives information about the Go module that targetImportPath belongs to, as a dependency of sourceImportPath.
This operates by finding the go.mod file for sourceImportPath, and then looking for a Go module inside its dependencies that matches targetImportPath.
Returns nil if a known module dependency is not found.
func (*Tree) PutModuleDeps
func (t *Tree) PutModuleDeps(modulePath string, deps []*Module)
PutModuleDeps registers a module and its dependencies. modulePath is the module root (e.g., "example.com/myproject"). deps is the list of dependencies from that module's go.mod.