🔎 SearchInfo
Search packages and return structured results. Internally runs composer search query --format=json and parses the output into *SearchResult.
When to use
- 🤖 Process search results programmatically and filter by name/description.
- 📦 Automatically discover and compare candidate packages in scripts.
- 🌐 Obtain structured data when building a "dependency recommendation" tool.
Signature
go
func (c *Composer) SearchInfo(query string) (*SearchResult, error)Parameters
| Parameter | Type | Description |
|---|---|---|
query | string | Search keyword |
Return value
| Value | Type | Description |
|---|---|---|
| Result | *SearchResult | Parsed search results |
| Error | error | Returned when execution or JSON parsing fails |
SearchResult and entry structures:
go
type SearchResult struct {
Results []SearchResultItem `json:"results"`
Total int `json:"total,omitempty"`
}
type SearchResultItem struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
URL string `json:"url,omitempty"`
Repository string `json:"repository,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("init failed: %v", err)
}
result, err := comp.SearchInfo("logger")
if err != nil {
log.Fatalf("search failed: %v", err)
}
fmt.Printf("%d results in total\n", result.Total)
for _, pkg := range result.Results {
fmt.Printf("%s: %s\n", pkg.Name, pkg.Description)
}
}Advanced
Narrowing the search scope
SearchOnlyName(query): match package names only.SearchWithType(query, packageType): filter by type.SearchWithFormat(query, format): specify the output format (includingjson).
Text output
Use Search (composer search query) when you only need the raw text output.
Parser function
ParseSearchResult(output string) can parse any composer search --format=json output standalone.
🔍 Related methods
Search: text search results.RequirePackage: add a found package as a dependency.