Skip to content

🏷️ SearchWithType

Search packages of a specific type on Packagist by keyword. Equivalent to composer search <query> --type=<packageType>, useful for searching only libraries, plugins (composer-plugin), project templates (project), and other specific types.

📋 Signature

go
func (c *Composer) SearchWithType(query string, packageType string) (string, error)

📥 Parameters

ParameterTypeDescription
querystringSearch keyword, e.g. "logger"
packageTypestringPackage type, e.g. "library", "composer-plugin", "project", "metapackage"

📤 Return value

Return valueTypeDescription
First return valuestringStandard output text of composer search <query> --type=<packageType>
Second return valueerrorReturned when an error occurs during execution; nil indicates success

📝 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 Composer failed: %v", err)
	}

	// Only search packages of the composer-plugin type
	output, err := comp.SearchWithType("logger", "composer-plugin")
	if err != nil {
		log.Fatalf("search packages by type failed: %v", err)
	}
	fmt.Println("search results for the specified type:")
	fmt.Println(output)
}

🚀 Advanced

  • 📋 Regular search without type restriction: use Search.
  • 🔎 Match package names only and filter out description noise: use SearchOnlyName.
  • 📦 When you need results in json format for programmatic parsing: use SearchWithFormat(query, format).
  • ⚠️ packageType is appended to the command line as --type=<packageType>. Passing an empty string results in --type=, which may cause Composer to error. Always pass a valid type value.

Released under the MIT License