Skip to content

Commit

Permalink
Issue #120: Add configuration to disable env auto expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
F1bonacc1 committed May 17, 2024
1 parent 8877e7f commit dda6ac7
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 17 deletions.
9 changes: 6 additions & 3 deletions process-compose.override.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ is_strict: true
environment:
- 'ABC=222'
log_location: ./pc.log
disable_env_expansion: false
shell:
shell_command: "zsh"
shell_argument: "-c"
Expand Down Expand Up @@ -46,7 +47,9 @@ processes:
ready_log_line: "test loop 1"

dep-on-log-line-ready:
command: "echo log is ready"
command: "echo log is $ENV_TEST"
environment:
- 'ENV_TEST=ready'
availability:
restart: "on_failure"
backoff_seconds: 2
Expand Down Expand Up @@ -182,7 +185,7 @@ processes:
- /tmp
- /

vim:
nvim:
description: "run a foreground process"
command: "vim process-compose.override.yaml"
command: "nvim process-compose.override.yaml"
is_foreground: true
14 changes: 12 additions & 2 deletions src/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,25 @@ func loadProjectFromFile(inputFile string) *types.Project {
// .env is optional we don't care if it errors
_ = godotenv.Load()

yamlFile = []byte(os.ExpandEnv(string(yamlFile)))
const envEscaped = "##PC_ENV_ESCAPED##"
// replace escaped $$ env vars in yaml
temp := strings.ReplaceAll(string(yamlFile), "$$", envEscaped)
temp = os.ExpandEnv(temp)
temp = strings.ReplaceAll(temp, envEscaped, "$")

project := &types.Project{
LogLength: defaultLogLength,
}
err = yaml.Unmarshal(yamlFile, project)
err = yaml.Unmarshal([]byte(temp), project)
if err != nil {
log.Fatal().Err(err).Msgf("Failed to parse %s", inputFile)
}
if project.DisableEnvExpansion {
err = yaml.Unmarshal(yamlFile, project)
if err != nil {
log.Fatal().Err(err).Msgf("Failed to parse %s", inputFile)
}
}

if err != nil {
log.Fatal().Err(err).Msgf("Failed to validate %s", inputFile)
Expand Down
25 changes: 13 additions & 12 deletions src/types/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ import (
type Vars map[string]any

type Project struct {
Version string `yaml:"version"`
LogLocation string `yaml:"log_location,omitempty"`
LogLevel string `yaml:"log_level,omitempty"`
LogLength int `yaml:"log_length,omitempty"`
LoggerConfig *LoggerConfig `yaml:"log_configuration,omitempty"`
LogFormat string `yaml:"log_format,omitempty"`
Processes Processes `yaml:"processes"`
Environment Environment `yaml:"environment,omitempty"`
ShellConfig *command.ShellConfig `yaml:"shell,omitempty"`
IsStrict bool `yaml:"is_strict"`
Vars Vars `yaml:"vars"`
FileNames []string
Version string `yaml:"version"`
LogLocation string `yaml:"log_location,omitempty"`
LogLevel string `yaml:"log_level,omitempty"`
LogLength int `yaml:"log_length,omitempty"`
LoggerConfig *LoggerConfig `yaml:"log_configuration,omitempty"`
LogFormat string `yaml:"log_format,omitempty"`
Processes Processes `yaml:"processes"`
Environment Environment `yaml:"environment,omitempty"`
ShellConfig *command.ShellConfig `yaml:"shell,omitempty"`
IsStrict bool `yaml:"is_strict"`
Vars Vars `yaml:"vars"`
DisableEnvExpansion bool `yaml:"disable_env_expansion"`
FileNames []string
}

type ProcessFunc func(process ProcessConfig) error
Expand Down
40 changes: 40 additions & 0 deletions www/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Default environment variables:
`PC_REPLICA_NUM` - Defines the process replica number. Useful for port collision avoidance for processes with multiple replicas.

## .env file

```.env
VERSION='1.2.3'
DB_USER='USERNAME'
Expand All @@ -49,6 +50,45 @@ processes:
- 'OUTPUT_DIR=/path/to/B/data'
```

## Disable Automatic Expansion

Process Compose provides 2 ways to disable the automatic environment variables expansion:

1. Escape the environment variables with `$$`. Example:
```yaml
processes:
foo:
command: echo I am $$ENV_TEST
environment:
- 'ENV_TEST=ready'
```

**Output**: `I am ready`

2. Globally disable the automatic expansion with `disable_env_expansion: true`. Example:
```yaml
disable_env_expansion: true
processes:
foo:
command: echo I am $ENV_TEST
environment:
- 'ENV_TEST=ready'
```

**Output**: `I am ready`

> :bulb: Note: The default behavior for the following `process-compose.yaml`:
>
> ```yaml
> processes:
> foo:
> command: echo I am $ENV_TEST
> environment:
> - 'ENV_TEST=ready'
> ```
>
> **Output**: `I am `

## Variables

Variables in Process Compose rely on [Go template engine](https://pkg.go.dev/text/template)
Expand Down

0 comments on commit dda6ac7

Please sign in to comment.