Documentation
¶
Overview ¶
Package core provides graph building, ecosystem detection, and output formatting for the deps command. Subcommand cmd/run.go imports core for all dependency analysis logic.
Index ¶
- Constants
- Variables
- func BuildGoFullGraph() (map[string][]string, error)
- func BuildGoInternalGraph() (map[string][]string, error)
- func BuildNodeFullGraph() (map[string][]string, error)
- func BuildNodeInternalGraph() (map[string][]string, error)
- func BuildNodeSinglePackageGraph(pkg PackageJSON) (map[string][]string, error)
- func BuildPyprojectGraph(includeDevDeps bool) (map[string][]string, error)
- func BuildRequirementsGraph() (map[string][]string, error)
- func BuildRustFullGraph() (map[string][]string, error)
- func BuildRustInternalGraph() (map[string][]string, error)
- func BuilderNames() []string
- func ExtractPythonPkgName(line string) string
- func GoModulePath(pkgs []GoPackage) string
- func IsStdlib(path string) bool
- func MermaidID(pkg string) string
- func ParsePyprojectArrayItems(line string) []string
- func ParsePyprojectDeps(content string, sectionSuffix string) []string
- func ParseRequirementsTxt(path string) ([]string, error)
- func RenderJSON(graph map[string][]string) string
- func RenderMermaid(graph map[string][]string) string
- func RenderTable(graph map[string][]string) string
- func ShortPkgName(importPath, modPath string) string
- func SortedKeys(m map[string][]string) []string
- type CargoDep
- type CargoMetadata
- type CargoNode
- type CargoPackage
- type CargoResolve
- type CargoTarget
- type GoBuilder
- type GoPackage
- type GraphBuilder
- type NodeBuilder
- type PackageJSON
- type PythonBuilder
- type RustBuilder
- type Workspaces
Constants ¶
const PythonEcosystem = "python"
PythonEcosystem is the ecosystem label for Python projects.
const RustEcosystem = "rust"
RustEcosystem is the ecosystem label for Rust projects.
Variables ¶
var Builders = []GraphBuilder{ &GoBuilder{}, &NodeBuilder{}, &PythonBuilder{}, &RustBuilder{}, }
Builders is the ordered registry of graph builders. Detection walks this list; the first match wins.
Functions ¶
func BuildGoFullGraph ¶
BuildGoFullGraph returns an adjacency list including external dependencies. Stdlib packages are excluded.
Returns:
- map[string][]string: Full dependency graph
- error: Non-nil if go list fails
func BuildGoInternalGraph ¶
BuildGoInternalGraph returns an adjacency list of internal package dependencies. Keys and values use shortened names (module prefix stripped).
Returns:
- map[string][]string: Internal dependency graph
- error: Non-nil if go list fails
func BuildNodeFullGraph ¶
BuildNodeFullGraph returns the full dependency list from package.json. For workspaces, includes all workspace and external deps.
Returns:
- map[string][]string: Full dependency graph
- error: Non-nil if package.json parsing fails
func BuildNodeInternalGraph ¶
BuildNodeInternalGraph builds a workspace-to-workspace dependency graph. For single-package projects, returns an empty graph (no internal deps).
Returns:
- map[string][]string: Workspace dependency graph
- error: Non-nil if package.json parsing fails
func BuildNodeSinglePackageGraph ¶
func BuildNodeSinglePackageGraph(pkg PackageJSON) (map[string][]string, error)
BuildNodeSinglePackageGraph returns deps for a single-package project.
Parameters:
- pkg: Parsed package.json data
Returns:
- map[string][]string: Dependency graph with single key
- error: Always nil
func BuildPyprojectGraph ¶
BuildPyprojectGraph parses pyproject.toml for dependencies. Uses a simple line-based parser - no TOML library needed for this subset.
Parameters:
- includeDevDeps: If true, include dev dependencies
Returns:
- map[string][]string: Dependency graph with "project" key
- error: Non-nil if pyproject.toml cannot be read
func BuildRequirementsGraph ¶
BuildRequirementsGraph parses requirements.txt and returns a flat dep list.
Returns:
- map[string][]string: Dependency graph with "project" key
- error: Non-nil if requirements.txt cannot be read
func BuildRustFullGraph ¶
BuildRustFullGraph returns all dependencies for workspace packages.
Returns:
- map[string][]string: Full dependency graph
- error: Non-nil if cargo metadata fails
func BuildRustInternalGraph ¶
BuildRustInternalGraph returns workspace member dependencies on each other.
Returns:
- map[string][]string: Internal dependency graph
- error: Non-nil if cargo metadata fails
func BuilderNames ¶
func BuilderNames() []string
BuilderNames returns all registered builder names for help text.
Returns:
- []string: Ordered list of ecosystem names
func ExtractPythonPkgName ¶
ExtractPythonPkgName extracts the package name from a requirements line. Handles: package==1.0, package>=1.0, package[extra]>=1.0, package ; markers
Parameters:
- line: Requirements line to parse
Returns:
- string: Lowercase package name
func GoModulePath ¶
GoModulePath reads the module path from the first GoPackage with a Module field.
Parameters:
- pkgs: Parsed go list output
Returns:
- string: Module path, or empty if not found
func IsStdlib ¶
IsStdlib returns true if the import path looks like a Go stdlib package. Heuristic: no dot in the first path component.
Parameters:
- path: Import path to check
Returns:
- bool: True if the path is a stdlib package
func MermaidID ¶
MermaidID converts a package path to a valid Mermaid node ID.
Parameters:
- pkg: Package path to convert
Returns:
- string: Safe Mermaid node identifier
func ParsePyprojectArrayItems ¶
ParsePyprojectArrayItems extracts package names from a TOML array line. Example: "requests>=2.0", "flask",
Parameters:
- line: TOML array line to parse
Returns:
- []string: Extracted package names
func ParsePyprojectDeps ¶
ParsePyprojectDeps extracts dependency names from a TOML array section. Looks for [project.dependencies], [tool.poetry.dependencies], etc.
Parameters:
- content: Full pyproject.toml content
- sectionSuffix: Section name suffix (e.g. "dependencies", "dev")
Returns:
- []string: Extracted dependency names
func ParseRequirementsTxt ¶
ParseRequirementsTxt extracts package names from a requirements.txt file. Handles version specifiers, comments, blank lines, and -r includes.
Parameters:
- path: Path to requirements.txt
Returns:
- []string: Package names
- error: Non-nil if file cannot be read
func RenderJSON ¶
RenderJSON produces a machine-readable JSON adjacency list.
Parameters:
- graph: Adjacency list of package dependencies
Returns:
- string: Pretty-printed JSON
func RenderMermaid ¶
RenderMermaid produces a Mermaid graph TD definition.
Parameters:
- graph: Adjacency list of package dependencies
Returns:
- string: Mermaid graph markup
func RenderTable ¶
RenderTable produces a Package | Imports table.
Parameters:
- graph: Adjacency list of package dependencies
Returns:
- string: Formatted table output
func ShortPkgName ¶
ShortPkgName strips the module prefix for readability.
Parameters:
- importPath: Full import path
- modPath: Module path prefix to strip
Returns:
- string: Shortened path, or original if prefix doesn't match
func SortedKeys ¶
SortedKeys returns the keys of a map sorted alphabetically.
Parameters:
- m: Map to extract keys from
Returns:
- []string: Sorted keys
Types ¶
type CargoMetadata ¶
type CargoMetadata struct {
Packages []CargoPackage `json:"packages"`
WorkspaceMembers []string `json:"workspace_members"`
Resolve *CargoResolve `json:"resolve"`
}
CargoMetadata represents the subset of `cargo metadata` output we need.
func RunCargoMetadata ¶
func RunCargoMetadata() (*CargoMetadata, error)
RunCargoMetadata runs `cargo metadata` and parses the output.
Returns:
- *CargoMetadata: Parsed metadata
- error: Non-nil if cargo is not found or output is malformed
func RunCargoMetadataFull ¶
func RunCargoMetadataFull() (*CargoMetadata, error)
RunCargoMetadataFull runs `cargo metadata` with full dependency resolution.
Returns:
- *CargoMetadata: Parsed metadata with full resolution
- error: Non-nil if cargo is not found or output is malformed
type CargoPackage ¶
type CargoPackage struct {
ID string `json:"id"`
Name string `json:"name"`
Source *string `json:"source"`
Dependencies []CargoDep `json:"dependencies"`
Targets []CargoTarget `json:"targets"`
}
CargoPackage represents a package in cargo metadata output.
type CargoResolve ¶
type CargoResolve struct {
Nodes []CargoNode `json:"nodes"`
}
CargoResolve represents the resolved dependency graph.
type CargoTarget ¶
CargoTarget represents a build target in cargo metadata.
type GoBuilder ¶
type GoBuilder struct{}
GoBuilder implements GraphBuilder for Go projects.
func (*GoBuilder) Build ¶
Build produces an adjacency list of Go dependencies.
Parameters:
- external: If true, include third-party dependencies
Returns:
- map[string][]string: Adjacency list
- error: Non-nil if go list fails
type GoPackage ¶
type GoPackage struct {
ImportPath string `json:"ImportPath"`
Name string `json:"Name"`
Imports []string `json:"Imports"`
Module *struct {
Path string `json:"Path"`
} `json:"Module"`
}
GoPackage represents the subset of `go list -json` output we need.
func ListGoPackages ¶
ListGoPackages runs `go list -json ./...` and parses the output. go list outputs concatenated JSON objects (not an array).
Returns:
- []GoPackage: Parsed packages
- error: Non-nil if go list fails or output is malformed
type GraphBuilder ¶
type GraphBuilder interface {
// Name returns the ecosystem label (e.g. "go", "node", "python", "rust").
Name() string
// Detect returns true if the current directory contains this ecosystem's
// manifest file (e.g., go.mod, package.json).
Detect() bool
// Build produces an adjacency list of dependencies.
// When external is false, only internal/project dependencies are included.
// When external is true, third-party dependencies are included too.
Build(external bool) (map[string][]string, error)
}
GraphBuilder produces a dependency adjacency list for a specific ecosystem.
func DetectBuilder ¶
func DetectBuilder() GraphBuilder
DetectBuilder returns the first builder whose Detect() returns true, or nil if no ecosystem is detected.
Returns:
- GraphBuilder: The detected builder, or nil if none matched
func FindBuilder ¶
func FindBuilder(name string) GraphBuilder
FindBuilder returns the builder matching the given name, or nil.
Parameters:
- name: Ecosystem name to find (e.g. "go", "node")
Returns:
- GraphBuilder: The matching builder, or nil if not found
type NodeBuilder ¶
type NodeBuilder struct{}
NodeBuilder implements GraphBuilder for Node.js projects.
func (*NodeBuilder) Build ¶
func (n *NodeBuilder) Build(external bool) (map[string][]string, error)
Build produces an adjacency list of Node.js dependencies.
Parameters:
- external: If true, include all dependencies
Returns:
- map[string][]string: Adjacency list
- error: Non-nil if package.json parsing fails
func (*NodeBuilder) Detect ¶
func (n *NodeBuilder) Detect() bool
Detect returns true if package.json exists in the current directory.
type PackageJSON ¶
type PackageJSON struct {
Name string `json:"name"`
Dependencies map[string]string `json:"dependencies"`
DevDependencies map[string]string `json:"devDependencies"`
Workspaces Workspaces `json:"workspaces"`
}
PackageJSON represents the fields we need from package.json.
func DiscoverWorkspaces ¶
func DiscoverWorkspaces(patterns []string) ([]PackageJSON, error)
DiscoverWorkspaces finds all workspace package.json files matching the given glob patterns and returns their parsed contents.
Parameters:
- patterns: Glob patterns to match workspace directories
Returns:
- []PackageJSON: Parsed workspace packages
- error: Non-nil if glob matching fails
func ReadPackageJSON ¶
func ReadPackageJSON(path string) (PackageJSON, error)
ReadPackageJSON reads and parses a package.json file.
Parameters:
- path: Path to the package.json file
Returns:
- PackageJSON: Parsed package data
- error: Non-nil if read or parse fails
type PythonBuilder ¶
type PythonBuilder struct{}
PythonBuilder implements GraphBuilder for Python projects.
func (*PythonBuilder) Build ¶
func (p *PythonBuilder) Build(external bool) (map[string][]string, error)
Build produces an adjacency list of Python dependencies.
Parameters:
- external: If true, include dev dependencies from pyproject.toml
Returns:
- map[string][]string: Dependency graph
- error: Non-nil if manifest parsing fails
func (*PythonBuilder) Detect ¶
func (p *PythonBuilder) Detect() bool
Detect returns true if requirements.txt or pyproject.toml exists.
func (*PythonBuilder) Name ¶
func (p *PythonBuilder) Name() string
Name returns the ecosystem label.
type RustBuilder ¶
type RustBuilder struct{}
RustBuilder implements GraphBuilder for Rust projects.
func (*RustBuilder) Build ¶
func (r *RustBuilder) Build(external bool) (map[string][]string, error)
Build produces an adjacency list of Rust dependencies.
Parameters:
- external: If true, include all external dependencies
Returns:
- map[string][]string: Adjacency list
- error: Non-nil if cargo metadata fails
func (*RustBuilder) Detect ¶
func (r *RustBuilder) Detect() bool
Detect returns true if Cargo.toml exists in the current directory.
type Workspaces ¶
type Workspaces struct {
Patterns []string
}
Workspaces handles the two valid package.json workspaces formats: array of globs, or object with "packages" array.
func (*Workspaces) UnmarshalJSON ¶
func (w *Workspaces) UnmarshalJSON(data []byte) error
UnmarshalJSON handles both array and object formats for workspaces.