Issue #120: Add configuration to disable env auto expansion

This commit is contained in:
Berger Eugene 2024-05-18 00:00:41 +03:00
parent 8877e7f4b5
commit dda6ac7429
4 changed files with 71 additions and 17 deletions

View File

@ -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"
@ -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
@ -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

View File

@ -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)

View File

@ -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

View File

@ -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'
@ -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)