Skip to content

✅ IsPackageInstalled

Checks whether the specified package is installed, equivalent to running composer show <package-name> and checking whether the command succeeds.

When to use

Use this to confirm whether a package has been installed in the current project before invoking functionality it provides. For example, before a toolchain loads a plugin, use it to check whether the plugin package exists.

Signature

go
func (c *Composer) IsPackageInstalled(packageName string) bool

Parameters

ParameterTypeDescription
packageNamestringThe package name to check, e.g. symfony/console

Return value

Return valueTypeDescription
Single return valueboolReturns true if the package is installed; false if it is not installed or the command fails

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

	if comp.IsPackageInstalled("symfony/console") {
		fmt.Println("symfony/console is installed ✅")
	} else {
		fmt.Println("symfony/console is not installed ❌")
	}
}

Advanced

Released under the MIT License