🔍 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) stringParameters
| Parameter | Type | Description |
|---|---|---|
name | EnvironmentVariable | Environment variable name, using a predefined constant such as EnvComposerCacheDir |
Return value
| Return value | Type | Description |
|---|---|---|
| Single return value | string | The 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
Envfield of aComposerinstance.