Skip to content

🔍 GetEnvVariable

Reads the current value of a Composer environment variable. Equivalent to calling the Go standard library's os.Getenv; returns an empty string when not set.

When to use

Use it when you need to check Composer's runtime environment configuration (such as home directory, cache directory, whether interaction is disabled) programmatically. Commonly used for diagnostics or conditional branching based on the environment.

Signature

go
func GetEnvVariable(name EnvironmentVariable) string

Parameters

ParameterTypeDescription
nameEnvironmentVariableEnvironment variable name, using a predefined constant such as EnvComposerCacheDir

Return value

Return valueTypeDescription
Single return valuestringThe variable value; empty string when not set

Example

go
package main

import (
	"fmt"

	"github.com/scagogogo/composer-skills/pkg/composer"
)

func main() {
	home := composer.GetEnvVariable(composer.EnvComposerHome)
	if home == "" {
		home = "(not set, using default)"
	}
	fmt.Printf("COMPOSER_HOME = %s\n", home)
}

Advanced

  • To set an environment variable, use SetEnvVariable.
  • To get all Composer configuration items at once, call GetEnvironmentInfo (based on composer config --list).
  • This is a package-level function that only reads the process environment; it does not affect the Env field of a Composer instance.

Released under the MIT License