Skip to content

Commit

Permalink
Allow setting env vars in cargo_metadata command
Browse files Browse the repository at this point in the history
Just like with `other_options`, this is a reasonably common use-case,
and needing to re-implement `exec` just to pass an extra env var is a
bit annoying.
  • Loading branch information
illicitonion committed Jan 30, 2023
1 parent 2ab28c6 commit 8559be2
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,9 @@ pub struct MetadataCommand {
/// Arbitrary command line flags to pass to `cargo`. These will be added
/// to the end of the command line invocation.
other_options: Vec<String>,
/// Arbitrary environment variables to set when running `cargo`. These will be merged into
/// the calling environment, overriding any which clash.
env: HashMap<String, String>,
/// Show stderr
verbose: bool,
}
Expand Down Expand Up @@ -689,6 +692,13 @@ impl MetadataCommand {
self
}

/// Arbitrary environment variables to set when running `cargo`. These will be merged into
/// the calling environment, overriding any which clash.
pub fn env(&mut self, key: String, val: String) -> &mut MetadataCommand {
self.env.insert(key, val);
self
}

/// Set whether to show stderr
pub fn verbose(&mut self, verbose: bool) -> &mut MetadataCommand {
self.verbose = verbose;
Expand Down Expand Up @@ -729,6 +739,8 @@ impl MetadataCommand {
}
cmd.args(&self.other_options);

cmd.envs(&self.env);

cmd
}

Expand Down

0 comments on commit 8559be2

Please sign in to comment.