Skip to content

🔧 GetConfigWithGlobal ​

Reads the current value of a Composer configuration item, with the option to read the global or project configuration. Equivalent to running composer config [--global] <setting>.

When to use ​

Use it when you need to obtain the value of a single configuration item (such as preferred-install, bin-dir, vendor-dir) programmatically and make decisions or log based on it. global=true reads the global value from COMPOSER_HOME/config.json; global=false reads the value from the current project's composer.json.

Signature ​

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

Parameters ​

ParameterTypeDescription
settingstringConfiguration item name, such as preferred-install, bin-dir
globalbooltrue reads the global configuration, false reads the project configuration

Return value ​

Return valueTypeDescription
First return valuestringValue of the configuration item (leading and trailing whitespace removed)
Second return valueerrorReturned when execution 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)
	}

	// Read project-level configuration
	install, err := comp.GetConfigWithGlobal("preferred-install", false)
	if err != nil {
		log.Fatalf("failed to read configuration: %v", err)
	}
	fmt.Println("Project preferred-install:", install)

	// Read global configuration
	home, err := comp.GetConfigWithGlobal("home", true)
	if err != nil {
		log.Fatalf("failed to read global configuration: %v", err)
	}
	fmt.Println("Global home:", home)
}

Advanced ​

  • To list all configuration items at once instead of a single one, use ListConfig or the structured version GetConfigStructured.
  • To write a configuration item, use SetConfigWithGlobal.
  • To find out which file a value comes from, use GetConfigSource(key).

Released under the MIT License