gocli

package
v2.0.0-...-1802b75 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2021 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListModules

func ListModules() (map[string]Module, error)

List go modules with metadata in workdir using go CLI list command. Modules with replace directive are returned as the replaced module instead.

Types

type BinaryMetadata

type BinaryMetadata struct {
	// The main module used to build the binary.
	// e.g. github.com//google/go-licenses/v2/tests/modules/cli02
	Main Module
	// Detailed metadata of all the module dependencies.
	// Does not include the main module.
	Deps []Module
}

Module metadata extracted from binary and local go module workspace.

func ExtractBinaryMetadata

func ExtractBinaryMetadata(path string) (*BinaryMetadata, error)

List dependencies from module metadata in a go binary. Modules with replace directives are returned as the replaced module instead.

Prerequisites: * The go binary must be built with go modules without any further modifications. * The command must run with working directory same as to build the analyzed go binary, because we need the exact go modules info used to build it.

Here, I am using [1] as a short term solution. It runs [4] go version -m and parses output. This is preferred over [2], because [2] is an alternative implemention for go version -m, and I expect better long term compatibility for go version -m.

The parsing command output hack is still unfavorable in the long term. As dicussed in [3], golang community will move go version parsing into an individual module in golang.org/x. We can use that module instead after it is built.

References of similar implementations or dicussions: 1. https://github.com/uw-labs/lichen/blob/be9752894a5958f6ba7be9e05dc370b7a73b58db/internal/module/extract.go#L16 2. https://github.com/mitchellh/golicense/blob/8c09a94a11ac73299a72a68a7b41e3a737119f91/module/module.go#L27 3. https://github.com/golang/go/issues/39301 4. https://golang.org/pkg/cmd/go/internal/version/

type Module

type Module struct {
	// Differences from packages.Module:
	// * Replace field is removed, it's only an implementation detail in this package.
	//   If a module is replaced, we'll directly return the replaced module.
	// * ModuleError field is removed, it's only used in packages.Module.
	Path      string     // module path
	Version   string     // module version
	Time      *time.Time // time version was created
	Main      bool       // is this the main module?
	Indirect  bool       // is this module only an indirect dependency of main module?
	Dir       string     // directory holding files for this module, if any
	GoMod     string     // path to go.mod file used when loading this module, if any
	GoVersion string     // go version used in module
}

Module provides module information for a package.

func ListDeps

func ListDeps(importPaths ...string) ([]Module, error)

ListDeps lists direct and transitive module dependencies of the import path packages. It leverages golang.org/x/tools/go/packages under the hood.

Jump to

Keyboard shortcuts

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