Skip to content

📦 BatchRequire

Batch adds multiple Composer dependency packages, with support for continue-on-error and a development-dependency flag.

When to use

Use when initializing a project, migrating a dependency manifest, or script-installing a group of related packages, and you want to complete it in a single call and get per-package success/failure statistics.

Signature

go
func (c *Composer) BatchRequire(packages map[string]string, dev bool, continueOnError bool) (*BatchRequireResult, error)

Parameters

ParameterTypeDescription
packagesmap[string]stringMapping of package name to version constraint, e.g. {"symfony/console": "^5.4"}
devboolWhether to add as require-dev (development dependencies)
continueOnErrorboolWhether to continue adding subsequent packages on error; when false, stops at the first error

Return value

  • *BatchRequireResult: Batch add result, including per-package results and counts
  • error: The first error encountered (only returned when continueOnError is false; otherwise nil)
go
type BatchRequireResult struct {
    Results      []RequireResult `json:"results,omitempty"`
    SuccessCount int             `json:"success_count"`
    FailCount    int             `json:"fail_count"`
    TotalCount   int             `json:"total_count"`
}

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.Fatal(err)
    }

    packages := map[string]string{
        "symfony/console": "^5.4",
        "monolog/monolog": "^2.0",
        "psr/log":         "^1.1",
    }

    result, err := comp.BatchRequire(packages, false, true)
    if err != nil {
        log.Printf("some additions failed: %v", err)
    }
    fmt.Printf("Success: %d, Failed: %d, Total: %d\n",
        result.SuccessCount, result.FailCount, result.TotalCount)
}

Advanced

  • This method calls RequirePackage for each package; failure information is written to the corresponding RequireResult.Warnings
  • For atomicity (all or rollback), set continueOnError=false and handle error yourself
  • The corresponding removal operation is BatchRemove
  • For single-package addition, use RequirePackage or RequirePackageWithOptions (see packages.go)

Released under the MIT License