Skip to content

🧾 GetLicensesInfo

Gets the license information for the current project's dependencies in a structured way, returning *LicensesResult.

When to use

Use it when you want to categorize and tally licenses, perform compliance checks, or generate custom license reports within an application. Internally it runs composer licenses --format json and parses it.

Signature

go
func (c *Composer) GetLicensesInfo() (*LicensesResult, error)

Parameters

This method does not accept any parameters.

Return value

  • *LicensesResult: structured license result containing each package's name, version, and license list.
  • error: returns the corresponding error message when command execution or JSON parsing fails.

Related structures:

go
type LicenseInfo struct {
	Package  string   `json:"package"`
	Version  string   `json:"version,omitempty"`
	Licenses []string `json:"licenses,omitempty"`
}

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

Example

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("%s (%s): %v\n", li.Package, li.Version, li.Licenses)
	}
}

Advanced

  • Parsing is performed by ParseLicensesResult, which can be used separately to parse the raw output of composer licenses --format=json.
  • The underlying formatted output is provided by LicensesWithFormat; for the default plain text, see Licenses.
  • Note: the raw output of composer licenses --format=json is a {"vendor/package":["MIT"],...} map; during parsing, the map's key is filled into LicenseInfo.Package.

Released under the MIT License