📜 LicensesWithFormat
以指定格式输出依赖包的许可证信息,相当于执行 composer licenses --format=<format>。
何时使用
当你需要将许可证信息导出为 JSON(便于程序处理)或 summary(便于阅读)等格式时使用。
签名
go
func (c *Composer) LicensesWithFormat(format string) (string, error)参数
| 参数 | 类型 | 说明 |
|---|---|---|
format | string | 输出格式,常用取值:json、summary、text |
返回值
string:对应格式的composer licenses原始输出。error:执行命令过程中发生错误时返回相应错误信息。
示例
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("初始化失败: %v", err)
}
// 以 JSON 格式输出,便于后续解析
jsonOutput, err := comp.LicensesWithFormat("json")
if err != nil {
log.Fatalf("获取许可证信息失败: %v", err)
}
fmt.Println(jsonOutput)
}进阶
- 若用
json格式且需要直接拿到结构体,使用GetLicensesInfo(内部调用并解析)。 - 默认纯文本输出用
Licenses。 - 需要同时传多个选项(如
--format=json --no-dev)用LicensesWithOptions。