📦 ExtractPackageNamesFromOutput
Extracts all Composer package names of the form vendor/package from any text output, deduplicates them, and returns the list.
When to use
Use it when you have a log or command output (such as the multi-line output of install/require) and need to quickly collect all package names mentioned within. It matches Composer's package name convention with a regex and automatically deduplicates.
Signature
go
func ExtractPackageNamesFromOutput(output string) []stringParameters
| Parameter | Type | Description |
|---|---|---|
output | string | Any command output text |
Return value
[]string: the deduplicated list of package names extracted, preserving the order of first appearance;nilwhen there are no matches.
Example
go
package main
import (
"fmt"
"github.com/scagogogo/composer-skills/pkg/composer"
)
func main() {
output := `Installing symfony/console (v5.4.0)
Loading from cache monolog/monolog (2.9.1)
Downloading psr/log (1.1.4)
symfony/console already installed`
names := composer.ExtractPackageNamesFromOutput(output)
fmt.Printf("Extracted %d package names:\n", len(names))
for _, n := range names {
fmt.Println(" -", n)
}
// Output: symfony/console, monolog/monolog, psr/log (duplicate symfony/console deduplicated)
}Advanced
- 🔍 The regex used is
([a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9](([_.]|-{1,2})?[a-z0-9]+)*), which conforms to the Composer package name convention (lowercase letters/digits/hyphens/underscores/dots, invendor/packageform). AfterFindAllString, a map is used for deduplication. - ⚠️ The regex is case-sensitive (matches lowercase only); output containing uppercase package names will be missed. The Composer package name convention itself requires lowercase, so this usually does not matter.
- 🔗 To extract only version numbers, use
ExtractVersionFromOutput. - 📋 To parse the package list output of
composer show, you can also useParsePackageList(output)(returns[]string, defined in convenience.go).