📜 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
CheckLicensesto 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.
type LicenseInfo struct {
Package string `json:"package"`
Version string `json:"version,omitempty"`
Licenses []string `json:"licenses,omitempty"`
}| Field | Type | Description |
|---|---|---|
Package | string | Package name, e.g., symfony/console |
Version | string | Version number (usually empty in structured parsing) |
Licenses | []string | List of license identifiers, e.g., ["MIT"] |
LicensesResult
Top-level result returned by GetLicensesInfo.
type LicensesResult struct {
Licenses []LicenseInfo `json:"licenses,omitempty"`
}Method Signatures
| Method | Signature | Description |
|---|---|---|
| 📜 Licenses | func (c *Composer) Licenses() (string, error) | composer licenses text output |
| 🎨 LicensesWithFormat | func (c *Composer) LicensesWithFormat(format string) (string, error) | Specify --format (text/json) |
| ⚙️ LicensesWithOptions | func (c *Composer) LicensesWithOptions(options map[string]string) (string, error) | Custom options |
| ✅ CheckLicenses | func (c *Composer) CheckLicenses() (string, error) | License compatibility check (--check) |
| 📊 GetLicensesInfo | func (c *Composer) GetLicensesInfo() (*LicensesResult, error) | Structured license info |
Parameters
LicensesWithFormat
| Parameter | Type | Description |
|---|---|---|
format | string | Output format, text or json |
Examples
Structured Get All Dependency Licenses
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
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
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
out, err := comp.LicensesWithFormat("json")
if err != nil {
log.Fatal(err)
}
fmt.Println(out)Custom Options
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.