🔎 Search
Search packages on Packagist by keyword, equivalent to composer search query.
When to use
- 🌐 You want to find a package with a certain capability but don't know the exact name.
- 📦 Compare candidate packages before adding a dependency.
- 🧪 Dynamically discover available packages in a script.
Signature
go
func (c *Composer) Search(query string) (string, error)Parameters
| Parameter | Type | Description |
|---|---|---|
query | string | Search keyword |
Return value
| Value | Type | Description |
|---|---|---|
| Output | string | Standard output of the search results |
| Error | error | Returned when an error occurs during the search |
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("init failed: %v", err)
}
output, err := comp.Search("logger")
if err != nil {
log.Fatalf("search packages failed: %v", err)
}
fmt.Println("search results:", output)
}Advanced
Structured results
When you need to process results programmatically, use SearchInfo instead. It runs composer search query --format=json and parses the output into *SearchResult.
Narrowing the search scope
SearchOnlyName(query): match package names only, filtering out description noise.SearchWithType(query, packageType): filter by type (e.g.composer-plugin).SearchWithFormat(query, format): specify the output format.
🔍 Related methods
SearchInfo: structured search results.RequirePackage: add a found package as a dependency.