Skip to content

🧩 AuditWithJSON

Performs a security audit on the current project's dependencies and parses the JSON output into a structured *AuditResult. Equivalent to running composer audit --format=json and automatically deserializing it.

When to use

Use when you need to obtain structured fields such as the vulnerable package name, version, severity, and link in code for further processing (e.g. filtering, alerting, reporting). This is the underlying implementation for several other methods: HasVulnerabilities, GetHighSeverityVulnerabilities, GetAbandonedPackages.

Signature

go
func (c *Composer) AuditWithJSON() (*AuditResult, error)

Parameters

This method takes no parameters.

Return value

Return valueTypeDescription
First return value*AuditResultParsed audit result, including the vulnerability list and counts
Second return valueerrorReturned when execution or JSON parsing fails

Main fields of AuditResult:

FieldTypeDescription
Vulnerabilities[]VulnerabilityList of vulnerability details
FoundintNumber of vulnerabilities found
AbandonedstringAssociated advisory identifier (may be empty)
WithoutDevboolWhether development dependencies were excluded

Example

go
package main

import (
	"fmt"
	"log"

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

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

	result, err := comp.AuditWithJSON()
	if err != nil {
		log.Fatalf("failed to run security audit: %v", err)
	}

	fmt.Printf("Found %d vulnerabilities\n", result.Found)
	for _, vuln := range result.Vulnerabilities {
		fmt.Printf("Package: %s %s\n", vuln.Package, vuln.Version)
		fmt.Printf("Vulnerability: %s\n", vuln.Title)
		fmt.Printf("Severity: %s\n", vuln.Severity)
		fmt.Printf("Details: %s\n\n", vuln.Link)
	}
}

Advanced

  • For finer-grained advisory information (including reportedAt, etc.), use GetAuditInfo, which returns an *AuditInfoResult.
  • For a JSON version with custom audit options, use GetAuditInfoWithOptions(options).
  • Related methods: Audit, AuditWithFormat, HasVulnerabilities.

Released under the MIT License