resolve

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: MIT Imports: 9 Imported by: 1

README

resolve

Parses raw package manager CLI output into a normalized dependency graph with PURLs.

Takes the output bytes from a manager's resolve command (e.g. npm ls --json, go mod graph, uv tree) and returns a structured Result with the dependency tree and PURL for each package.

Installation

go get github.com/git-pkgs/resolve

Usage

import (
	"github.com/git-pkgs/resolve"
	_ "github.com/git-pkgs/resolve/parsers" // register all parsers
)

output, _ := exec.Command("npm", "ls", "--depth", "Infinity", "--json", "--long").Output()

result, err := resolve.Parse("npm", output)
// result.Manager   == "npm"
// result.Ecosystem == "npm"
// result.Direct    == []*Dep{ {PURL: "pkg:npm/express@4.18.2", Name: "express", Version: "4.18.2", Deps: [...]}, ... }

Parse is the only entry point. It dispatches to the correct parser based on the manager name and returns ErrUnsupportedManager for unknown managers.

Each Dep includes the ecosystem-native package name, resolved version, a PURL string, and a Deps slice for transitive dependencies. Deps is nil for managers that only produce flat lists (pip, conda, bundler, helm, etc.) and non-nil for managers that provide tree structure.

Supported managers

Manager Ecosystem Output format
npm npm JSON tree
pnpm npm JSON tree
yarn npm NDJSON tree
bun npm Text tree
cargo cargo JSON graph
gomod golang Edge list
pip pypi JSON flat
uv pypi Text tree
poetry pypi Text tree
conda conda JSON flat
bundler gem Text flat
maven maven Text tree
gradle maven Text tree
composer packagist Text tree
nuget nuget Tabular
swift swift JSON tree
pub pub Text tree
mix hex Text tree
rebar3 hex Text tree
stack hackage JSON flat
lein clojars Text tree
conan conan Custom
deno deno JSON flat
helm helm Tabular

License

MIT.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrResolveNotSupported = errors.New("manager does not support resolve")

ErrResolveNotSupported is returned when the manager does not support the resolve operation.

View Source
var ErrUnsupportedManager = errors.New("unsupported manager")

ErrUnsupportedManager is returned when Parse is called with an unknown manager name.

Functions

func EcosystemForManager

func EcosystemForManager(manager string) (string, bool)

EcosystemForManager returns the ecosystem name for a registered manager.

func HasDirParser

func HasDirParser(manager string) bool

HasDirParser reports whether manager registered a directory-based parser.

func MakePURL

func MakePURL(ecosystem, name, version string) string

MakePURL constructs a PURL string for a dependency.

func Managers

func Managers() []string

Managers returns the list of manager names that have registered parsers.

func Register

func Register(manager, ecosystem string, fn func([]byte) ([]*Dep, error))

Register adds a parser for a manager. Called from parser init() functions.

func RegisterDir

func RegisterDir(manager, ecosystem string, fn func(string) ([]*Dep, error))

RegisterDir adds a parser that reads files from the project directory rather than command stdout. Used for managers whose lockfile is the canonical resolved-graph source (e.g. composer.lock).

func TabContentParser

func TabContentParser(content string) (string, string, bool)

TabContentParser splits tab-separated "name\tversion" content for BuildTree. Used by parsers that encode name and version as a tab-delimited pair.

Types

type Dep

type Dep struct {
	PURL    string // pkg:npm/%40scope/name@1.0.0
	Name    string // ecosystem-native name (@scope/name)
	Version string // resolved version (1.0.0)
	Deps    []*Dep // transitive deps; nil for flat-list managers
}

Dep is a single resolved dependency.

func BuildTree

func BuildTree(lines []TreeLine, ecosystem string, contentParser func(string) (string, string, bool)) []*Dep

BuildTree takes tree lines and a content-parser function, and builds a []*Dep tree. The contentParser receives the content string and returns (name, version, deps-placeholder). Deps is set to non-nil empty slice to indicate tree structure is available.

type InputDep

type InputDep struct {
	Name    string // package name in ecosystem-native format
	Version string // version constraint (optional)
}

InputDep describes a dependency to add to the generated project.

type Result

type Result struct {
	Manager   string // "npm", "cargo", etc.
	Ecosystem string // "npm", "cargo", "golang", etc.
	Direct    []*Dep // top-level dependencies
}

Result is the parsed dependency graph for one manager invocation.

func Parse

func Parse(manager string, output []byte) (*Result, error)

Parse dispatches to the per-manager parser and returns the dependency graph.

func ParseDir

func ParseDir(manager, dir string) (*Result, error)

ParseDir dispatches to a directory-based parser. The parser reads whatever files it needs (manifest, lockfile) from dir.

func ResolveDeps

func ResolveDeps(ctx context.Context, manager string, deps []InputDep) (*Result, error)

ResolveDeps creates a temporary project, adds the given dependencies using the specified package manager, runs resolution, and parses the output into a dependency graph.

The package manager CLI must be installed and available on PATH.

type TreeLine

type TreeLine struct {
	Depth   int
	Content string
}

TreeLine is a parsed line from a text tree.

func ParseTreeLines

func ParseTreeLines(lines []string, opts TreeOptions) []TreeLine

ParseTreeLines reads indented tree output and returns depth + content for each line.

type TreeOptions

type TreeOptions struct {
	// Prefixes are the tree-drawing characters that indicate a child node.
	// e.g. "├── ", "└── ", "+- ", "\- "
	Prefixes []string

	// Continuations are the tree-drawing characters that indicate depth continuation.
	// e.g. "│   ", "|  ", "│  "
	Continuations []string
}

TreeOptions configures how tree lines are parsed.

func BoxDrawingOptions

func BoxDrawingOptions() TreeOptions

BoxDrawingOptions returns TreeOptions for Unicode box-drawing trees (├── └── │).

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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