Skip to content

⚙️ SetConfigWithGlobal

Writes the value of a Composer config option, optionally to the global config or the project config. Equivalent to running composer config [--global] <setting> <value>.

When to use

Use this when you need to modify Composer configuration programmatically (e.g. setting preferred-install to dist, or specifying a custom vendor-dir). global=true writes to COMPOSER_HOME/config.json; global=false writes to the current project's composer.json.

Signature

go
func (c *Composer) SetConfigWithGlobal(setting string, value string, global bool) error

Parameters

ParameterTypeDescription
settingstringConfig option name, e.g. preferred-install
valuestringValue to write
globalbooltrue writes to the global config, false writes to the project config

Return value

Return valueTypeDescription
Sole return valueerrorReturned when the write 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("init failed: %v", err)
	}

	// Set a project-level config
	if err := comp.SetConfigWithGlobal("preferred-install", "dist", false); err != nil {
		log.Fatalf("set project config failed: %v", err)
	}
	fmt.Println("set project preferred-install = dist")

	// Set a global config
	if err := comp.SetConfigWithGlobal("optimize-autoloader", "true", true); err != nil {
		log.Fatalf("set global config failed: %v", err)
	}
	fmt.Println("set global optimize-autoloader = true")
}

Advanced

  • To read a config option, use GetConfigWithGlobal.
  • To remove a config option, use UnsetConfig(key).
  • Writes are flushed directly to the corresponding JSON file; before performing the operation, confirm the working directory is correct (see SetWorkingDir).

Released under the MIT License