Skip to content

Commit c132c6c

Browse files
committed
add env.variables for setting variables directly
1 parent 6912f8a commit c132c6c

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

docs/cross_toml.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ For example:
1818
[build.env]
1919
volumes = ["VOL1_ARG", "VOL2_ARG"]
2020
passthrough = ["IMPORTANT_ENV_VARIABLES"]
21+
var.ARG1 = "value"
2122
```
2223

2324
# `target.TARGET`
@@ -38,4 +39,5 @@ This is similar to `build.env`, but allows you to be more specific per target.
3839
[target.x86_64-unknown-linux-gnu.env]
3940
volumes = ["VOL1_ARG", "VOL2_ARG"]
4041
passthrough = ["IMPORTANT_ENV_VARIABLES"]
41-
```
42+
var.ARG1 = "value"
43+
```

src/config.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,18 @@ impl Config {
185185
Ok(collected)
186186
}
187187

188+
189+
pub fn env_variables(&self, target: &Target) -> HashMap<String, String> {
190+
// Does not support env variable config, that would make not much sense
191+
if let Some(ref toml) = self.toml {
192+
let mut map = toml.env_variable_target(target);
193+
map.extend(toml.env_variable_build());
194+
map
195+
} else {
196+
HashMap::new()
197+
}
198+
}
199+
188200
pub fn target(&self, target_list: &TargetList) -> Option<Target> {
189201
if let Some(env_value) = self.env.target() {
190202
return Some(Target::from(&env_value, target_list));

src/cross_toml.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ pub struct CrossEnvConfig {
1212
volumes: Vec<String>,
1313
#[serde(default)]
1414
passthrough: Vec<String>,
15+
#[serde(default, alias = "var")]
16+
variable: HashMap<String, String>,
1517
}
1618

1719
/// Build configuration
@@ -79,17 +81,28 @@ impl CrossToml {
7981
.map_or(Vec::new(), |t| t.env.passthrough.clone())
8082
}
8183

82-
/// Returns the list of environment variables to pass through for `build`,
84+
/// Returns the list of environment variables to use for/as volumes in `build`,
8385
pub fn env_volumes_build(&self) -> Vec<String> {
8486
self.build.env.volumes.clone()
8587
}
8688

87-
/// Returns the list of environment variables to pass through for `target`,
89+
/// Returns the list of environment variables to use for/as volumes in `target`,
8890
pub fn env_volumes_target(&self, target: &Target) -> Vec<String> {
8991
self.get_target(target)
9092
.map_or(Vec::new(), |t| t.env.volumes.clone())
9193
}
9294

95+
/// Returns the list of environment variables to set and pass through
96+
pub fn env_variable_target(&self, target: &Target) -> HashMap<String, String> {
97+
self.get_target(target)
98+
.map_or(HashMap::new(), |t| t.env.variable.clone())
99+
}
100+
101+
/// Returns the list of environment variables to set and pass through
102+
pub fn env_variable_build(&self) -> HashMap<String, String> {
103+
self.build.env.variable.clone()
104+
}
105+
93106
/// Returns the default target to build,
94107
pub fn default_target(&self, target_list: &TargetList) -> Option<Target> {
95108
self.build
@@ -129,6 +142,7 @@ mod tests {
129142
env: CrossEnvConfig {
130143
volumes: vec!["VOL1_ARG".to_string(), "VOL2_ARG".to_string()],
131144
passthrough: vec!["VAR1".to_string(), "VAR2".to_string()],
145+
variable: [("ARG1".to_owned(), "value".to_owned())].into(),
132146
},
133147
xargo: Some(true),
134148
default_target: None,
@@ -142,6 +156,7 @@ mod tests {
142156
[build.env]
143157
volumes = ["VOL1_ARG", "VOL2_ARG"]
144158
passthrough = ["VAR1", "VAR2"]
159+
var.ARG1 = "value"
145160
"#;
146161
let parsed_cfg = CrossToml::from_str(test_str)?;
147162

@@ -161,6 +176,7 @@ mod tests {
161176
env: CrossEnvConfig {
162177
passthrough: vec!["VAR1".to_string(), "VAR2".to_string()],
163178
volumes: vec!["VOL1_ARG".to_string(), "VOL2_ARG".to_string()],
179+
variable: [("ARG1".to_owned(), "value".to_owned())].into(),
164180
},
165181
xargo: Some(false),
166182
image: Some("test-image".to_string()),
@@ -177,6 +193,7 @@ mod tests {
177193
[target.aarch64-unknown-linux-gnu.env]
178194
volumes = ["VOL1_ARG", "VOL2_ARG"]
179195
passthrough = ["VAR1", "VAR2"]
196+
var.ARG1 = "value"
180197
[target.aarch64-unknown-linux-gnu]
181198
xargo = false
182199
image = "test-image"

src/docker.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ pub fn run(
133133
// flag forwards the value from the parent shell
134134
docker.args(&["-e", var]);
135135
}
136+
137+
for (ref var, ref value) in config.env_variables(target) {
138+
validate_env_var(var)?;
139+
140+
docker.args(&["-e", &format!("{var}={value}")]);
141+
}
142+
136143
let mut env_volumes = false;
137144
for ref var in config.env_volumes(target)? {
138145
validate_env_var(var)?;

0 commit comments

Comments
 (0)