core

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

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

View Source
const PythonEcosystem = "python"

PythonEcosystem is the ecosystem label for Python projects.

View Source
const RustEcosystem = "rust"

RustEcosystem is the ecosystem label for Rust projects.

Variables

Builders is the ordered registry of graph builders. Detection walks this list; the first match wins.

Functions

func BuildGoFullGraph

func BuildGoFullGraph() (map[string][]string, error)

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

func BuildGoInternalGraph() (map[string][]string, error)

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

func BuildNodeFullGraph() (map[string][]string, error)

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

func BuildNodeInternalGraph() (map[string][]string, error)

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

func BuildPyprojectGraph(includeDevDeps bool) (map[string][]string, error)

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

func BuildRequirementsGraph() (map[string][]string, error)

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

func BuildRustFullGraph() (map[string][]string, error)

BuildRustFullGraph returns all dependencies for workspace packages.

Returns:

  • map[string][]string: Full dependency graph
  • error: Non-nil if cargo metadata fails

func BuildRustInternalGraph

func BuildRustInternalGraph() (map[string][]string, error)

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

func ExtractPythonPkgName(line string) string

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

func GoModulePath(pkgs []GoPackage) string

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

func IsStdlib(path string) bool

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

func MermaidID(pkg string) string

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

func ParsePyprojectArrayItems(line string) []string

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

func ParsePyprojectDeps(content string, sectionSuffix string) []string

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

func ParseRequirementsTxt(path string) ([]string, error)

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

func RenderJSON(graph map[string][]string) string

RenderJSON produces a machine-readable JSON adjacency list.

Parameters:

  • graph: Adjacency list of package dependencies

Returns:

  • string: Pretty-printed JSON

func RenderMermaid

func RenderMermaid(graph map[string][]string) string

RenderMermaid produces a Mermaid graph TD definition.

Parameters:

  • graph: Adjacency list of package dependencies

Returns:

  • string: Mermaid graph markup

func RenderTable

func RenderTable(graph map[string][]string) string

RenderTable produces a Package | Imports table.

Parameters:

  • graph: Adjacency list of package dependencies

Returns:

  • string: Formatted table output

func ShortPkgName

func ShortPkgName(importPath, modPath string) string

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

func SortedKeys(m map[string][]string) []string

SortedKeys returns the keys of a map sorted alphabetically.

Parameters:

  • m: Map to extract keys from

Returns:

  • []string: Sorted keys

Types

type CargoDep

type CargoDep struct {
	Name string  `json:"name"`
	Kind *string `json:"kind"`
}

CargoDep represents a dependency entry in cargo metadata.

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 CargoNode

type CargoNode struct {
	ID   string   `json:"id"`
	Deps []string `json:"deps,omitempty"`
}

CargoNode represents a node in the resolved dependency graph.

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

type CargoTarget struct {
	Name string   `json:"name"`
	Kind []string `json:"kind"`
}

CargoTarget represents a build target in cargo metadata.

type GoBuilder

type GoBuilder struct{}

GoBuilder implements GraphBuilder for Go projects.

func (*GoBuilder) Build

func (g *GoBuilder) Build(external bool) (map[string][]string, error)

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

func (*GoBuilder) Detect

func (g *GoBuilder) Detect() bool

Detect returns true if go.mod exists in the current directory.

func (*GoBuilder) Name

func (g *GoBuilder) Name() string

Name returns the ecosystem label.

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

func ListGoPackages() ([]GoPackage, error)

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.

func (*NodeBuilder) Name

func (n *NodeBuilder) Name() string

Name returns the ecosystem label.

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.

func (*RustBuilder) Name

func (r *RustBuilder) Name() string

Name returns the ecosystem label.

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.

Jump to

Keyboard shortcuts

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