Skip to content

📜 Licenses

List licenses of all project dependencies, supporting output by format, custom options, and license compatibility checking.

Composer Skills wraps composer licenses into multiple methods from "raw text" to "structured JSON". The structured method GetLicensesInfo automatically normalizes Composer's {"vendor/package":["MIT"]} mapping into a []LicenseInfo slice for easy iteration.

When to Use

  • ⚖️ Legal/compliance review: confirm all dependency licenses are compatible with your distribution strategy before releasing a product (avoid GPL slipping into closed-source products).
  • 📋 Include license fields when generating third-party component lists (SBOM).
  • 🚧 CI gate: use CheckLicenses to block dependencies with incompatible licenses.
  • 📊 Dashboard statistics of project license distribution (MIT / Apache-2.0 / BSD, etc.).

Structured Types

LicenseInfo

Element in LicensesResult, representing a single package's license info.

go
type LicenseInfo struct {
	Package  string   `json:"package"`
	Version  string   `json:"version,omitempty"`
	Licenses []string `json:"licenses,omitempty"`
}
FieldTypeDescription
PackagestringPackage name, e.g., symfony/console
VersionstringVersion number (usually empty in structured parsing)
Licenses[]stringList of license identifiers, e.g., ["MIT"]

LicensesResult

Top-level result returned by GetLicensesInfo.

go
type LicensesResult struct {
	Licenses []LicenseInfo `json:"licenses,omitempty"`
}

Method Signatures

MethodSignatureDescription
📜 Licensesfunc (c *Composer) Licenses() (string, error)composer licenses text output
🎨 LicensesWithFormatfunc (c *Composer) LicensesWithFormat(format string) (string, error)Specify --format (text/json)
⚙️ LicensesWithOptionsfunc (c *Composer) LicensesWithOptions(options map[string]string) (string, error)Custom options
✅ CheckLicensesfunc (c *Composer) CheckLicenses() (string, error)License compatibility check (--check)
📊 GetLicensesInfofunc (c *Composer) GetLicensesInfo() (*LicensesResult, error)Structured license info

Parameters

LicensesWithFormat

ParameterTypeDescription
formatstringOutput format, text or json

Examples

Structured Get All Dependency Licenses

go
package main

import (
	"fmt"
	"log"

	"github.com/scagogogo/composer-skills/pkg/composer"
)

func main() {
	comp, err := composer.New(composer.DefaultOptions())
	if err != nil {
		log.Fatalf("Initialization failed: %v", err)
	}

	result, err := comp.GetLicensesInfo()
	if err != nil {
		log.Fatalf("Failed to get license info: %v", err)
	}

	for _, li := range result.Licenses {
		fmt.Printf("%-30s %v\n", li.Package, li.Licenses)
	}
}

License Distribution Statistics

go
result, _ := comp.GetLicensesInfo()
dist := map[string]int{}
for _, li := range result.Licenses {
	for _, lic := range li.Licenses {
		dist[lic]++
	}
}
for lic, n := range dist {
	fmt.Printf("%-15s %d packages\n", lic, n)
}

CI Gate: Check License Compatibility

go
out, err := comp.CheckLicenses()
if err != nil {
	log.Fatalf("License check failed: %v\n%s", err, out)
}
fmt.Println("✅ License compatibility check passed")

Specify JSON Format

go
out, err := comp.LicensesWithFormat("json")
if err != nil {
	log.Fatal(err)
}
fmt.Println(out)

Custom Options

go
out, err := comp.LicensesWithOptions(map[string]string{
	"format": "json",
	"no-dev": "",
})
if err != nil {
	log.Fatal(err)
}
fmt.Println(out)

Advanced

GetLicensesInfo Normalization

Composer's licenses --format=json raw output is a {"vendor/package":["MIT"]} mapping. ParseLicensesResult converts it to []LicenseInfo, with one record per package, convenient for iteration and serialization.

CheckLicenses Depends on composer.json Config

composer licenses --check requires allowed licenses to be pre-declared in composer.json's config.allow-list / config.filed-license, otherwise the check is meaningless. This method only executes the command; it doesn't define the policy for you.

Released under the MIT License