Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding env key to config #71

Merged
merged 2 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,15 @@ release:
```

Note that subcommands can be defined as any valid command that `ya` supports, including other subcommands.

### Environment Variables

If you'd like to explicitly list the environment variables that should be passed to a command, you can do so using the `env` key. For example, you might want to pass the `RUST_LOG` environment variable to a command. You can do that like so:

```yml
debug:
prog: cargo
args: ["run"]
env:
RUST_LOG: debug
```
5 changes: 5 additions & 0 deletions examples/envs/.config/ya.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
envs:
cmd: |
echo "$CUSTOM_ENV"
env:
CUSTOM_ENV: "custom value"
7 changes: 7 additions & 0 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ impl Runnable for FullCommand {
let prog = &self.prog.clone();
let cmd = &self.cmd;
let chdir = &self.chdir;
let envs = &self.envs;

let mut command = Command::new(prog);

Expand All @@ -116,6 +117,12 @@ impl Runnable for FullCommand {
command.current_dir(chdir);
}

if let Some(envs) = envs {
for (key, value) in envs {
command.env(key, value);
}
}

let result = command.spawn()?.wait()?;

if !result.success() {
Expand Down
27 changes: 26 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,15 @@ pub struct FullCommand {
pub args: Vec<String>,
pub cmd: Option<String>,
pub chdir: Option<String>,
pub envs: Option<HashMap<String, String>>,
}

impl Validatable for FullCommand {
fn validate(&self, m: Mapping, run_command_flags: &RunCommandFlags) {
if ! run_command_flags.quiet {
let keys = m.keys().filter(|k| {
let k = k.as_str().unwrap_or("");
k != "prog" && k != "args" && k != "cmd" && k != "chdir"
k != "prog" && k != "args" && k != "cmd" && k != "chdir" && k != "env"
}).collect::<Vec<_>>();

if ! keys.is_empty() {
Expand Down Expand Up @@ -157,12 +158,36 @@ impl TryFrom<serde_yaml::Mapping> for FullCommand {
})
.transpose()?;

let envs = m.get("env")
.map(|v| {
v.as_mapping()
.ok_or(anyhow::anyhow!("Invalid FullCommand: `env` is not a mapping"))
})
.transpose()?
.map(|m| {
m.iter()
.map(|(k, v)| {
let k = k.as_str()
.ok_or(anyhow::anyhow!("Invalid FullCommand: `env` key is not a string"))?
.to_string();

let v = v.as_str()
.ok_or(anyhow::anyhow!("Invalid FullCommand: `env` value is not a string"))?
.to_string();

Ok((k, v))
})
.collect::<anyhow::Result<HashMap<String, String>>>()
})
.transpose()?;


Ok(FullCommand {
prog,
args,
cmd,
chdir: chdir.map(|s| s.to_string()),
envs,
})
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,8 +767,12 @@ mod get_templates {
let templates = get_templates(&templates_dir)?;

assert_eq!(templates.len(), 2);
assert_eq!(templates[0], "test.hbs");
assert_eq!(templates[1], "test2.hbs");

let filename1 = "test.hbs".to_owned();
let filename2 = "test2.hbs".to_owned();

assert!(templates.contains(&filename1));
assert!(templates.contains(&filename2));

Ok(())
}
Expand Down
19 changes: 19 additions & 0 deletions tests/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#[cfg(test)]
mod envs {
use anyhow::Result;
use assert_cmd::Command;
fn ya() -> Command {
Command::cargo_bin(env!("CARGO_PKG_NAME")).expect("Error invoking ya")
}

#[test]
fn envs() -> Result<()> {
ya().args(["envs"])
.current_dir("examples/envs")
.assert()
.success()
.stdout("custom value\n");

Ok(())
}
}