modules

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 23, 2024 License: MIT Imports: 9 Imported by: 1

README

Modules

This is modules, a Go library for walking a directory tree and performing operations on any Go modules found.

Documentation

Index

Constants

DefaultLoadMode is the default value for the Mode field of the packages.Config used by Walker.LoadEach and Walker.LoadEachGomod.

Variables

View Source
var DefaultLoadConfig = packages.Config{Mode: DefaultLoadMode}

DefaultLoadConfig is the default value for the [Walker.LoadConfig] field used by Walker.LoadEach and Walker.LoadEachGomod.

Functions

func Each

func Each(dir string, f func(string) error) error

Each calls f for each Go module in dir and its subdirectories. A Go module is identified by the presence of a go.mod file. The argument to f is the directory containing the go.mod file, which will have dir as a prefix. This function calls Walker.Each with a default Walker.

func EachGomod

func EachGomod(dir string, f func(string, *modfile.File) error) error

EachGomod calls f for each Go module in dir and its subdirectories. A Go module is identified by the presence of a go.mod file. The arguments to f are the directory containing the go.mod file (which will have dir as a prefix) and the parsed go.mod file. This function calls Walker.EachGomod with a default Walker.

func LoadEach

func LoadEach(dir string, f func(string, []*packages.Package) error) error

LoadEach calls f once for each Go module in dir and its subdirectories, passing it the directory containing the go.mod file (which will have dir as a prefix) and a slice of packages.Package values loaded from that directory using the packages.Load function.

func LoadEachGomod

func LoadEachGomod(dir string, f func(string, *modfile.File, []*packages.Package) error) error

LoadEachGomod combines LoadEach and EachGomod.

Types

type PackageLoadError

type PackageLoadError struct {
	PkgPath string
	Err     error
}

PackageLoadError is an error type that wraps an error that occurred while loading a package.

func (PackageLoadError) Error

func (e PackageLoadError) Error() string

func (PackageLoadError) Unwrap

func (e PackageLoadError) Unwrap() error

type Walker

type Walker struct {
	// IncludeVendor controls whether to walk into vendor directories.
	IncludeVendor bool

	// IncludeTestdata controls whether to walk into testdata directories.
	IncludeTestdata bool

	// ParseLax controls whether to use [modfile.ParseLax] instead of [modfile.Parse].
	ParseLax bool // Use [modfile.ParseLax] to parse go.mod files instead of [modfile.Parse].

	// VersionFixer is a function that can be used to fix version strings in go.mod files.
	VersionFixer modfile.VersionFixer // Use this version-string fixing function when parsing go.mod files.

	// This is the config to pass to [packages.Load]
	// when loading packages in [Walker.LoadEach] and [Walker.LoadEachGomod].
	// If this is the zero config,
	// a default value of [DefaultLoadConfig] is used.
	// If this is not the zero config but LoadConfig.Mode is zero,
	// a default value of [DefaultLoadMode] is used.
	// The Dir field of the config is set to the directory passed to [Walker.LoadEach] or [Walker.LoadEachGomod].
	LoadConfig packages.Config

	// FailOnPackageErrors controls whether to return an error if any package fails to load.
	FailOnPackageErrors bool
}

Walker is a controller for various methods that walk a directory tree of Go modules. The zero value is a valid walker.

func (*Walker) Each

func (w *Walker) Each(dir string, f func(string) error) error

Each calls f for each Go module in dir and its subdirectories. A Go module is identified by the presence of a go.mod file. The arguments to f is the directory containing the go.mod file, which will have dir as a prefix.

func (*Walker) EachGomod

func (w *Walker) EachGomod(dir string, f func(string, *modfile.File) error) error

EachGomod calls f for each Go module in dir and its subdirectories. A Go module is identified by the presence of a go.mod file. The arguments to f are the directory containing the go.mod file (which will have dir as a prefix) and the parsed go.mod file.

func (*Walker) LoadEach

func (w *Walker) LoadEach(dir string, f func(string, []*packages.Package) error) error

LoadEach calls f once for each Go module in dir and its subdirectories, passing it the directory containing the go.mod file (which will have dir as a prefix) and a slice of packages.Package values loaded from that directory using the packages.Load function. You can specify how loading is done by modifying w.LoadConfig. If w.LoadConfig is the zero value, a default value of DefaultLoadConfig is used. If w.LoadConfig is not the zero value but LoadConfig.Mode is zero, a default value of DefaultLoadMode is used.

func (*Walker) LoadEachGomod

func (w *Walker) LoadEachGomod(dir string, f func(string, *modfile.File, []*packages.Package) error) error

LoadEachGomod combines LoadEach and EachGomod.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL