-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: better xtask run as sudo command
- Loading branch information
1 parent
ed24020
commit 5844958
Showing
10 changed files
with
98 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod surun; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use anyhow::{Context, Result}; | ||
use clap::Parser; | ||
use xshell::{cmd, Shell}; | ||
|
||
#[derive(Debug, Parser)] | ||
pub struct SuRunCommand { | ||
/// Arguments normally passed to `cargo run` | ||
#[clap(name = "ARGS", allow_hyphen_values = true)] | ||
pub run_args: Vec<String>, | ||
} | ||
|
||
impl SuRunCommand { | ||
pub fn run(&self) -> Result<()> { | ||
let cargo = std::env::var("CARGO").unwrap(); | ||
|
||
let target_triple = match self | ||
.run_args | ||
.iter() | ||
.skip_while(|arg| *arg != "--target") | ||
.skip(1) // advance the `--target` identifier | ||
.next() | ||
{ | ||
Some(target_triple) => target_triple.to_owned(), | ||
None => get_default_target(&cargo) | ||
.context(format!("failed to get target triple with {cargo}"))?, | ||
}; | ||
|
||
log::debug!("Detected host triple: {target_triple}"); | ||
|
||
let target_triple_env_runner = { | ||
let tt_env_format = target_triple.to_uppercase().replace("-", "_"); | ||
format!("CARGO_TARGET_{tt_env_format}_RUNNER") | ||
}; | ||
|
||
log::debug!("Overriding env variable: {target_triple_env_runner}"); | ||
|
||
let sh = Shell::new()?; | ||
|
||
sh.set_var(target_triple_env_runner, "sudo -E"); | ||
|
||
let args = &self.run_args; | ||
|
||
cmd!(sh, "{cargo} run {args...}").run()?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
/// Calls `cargo -vV`` in a subprocess and returns the default Clang target triple. | ||
fn get_default_target(cargo_path: &str) -> Result<String> { | ||
/// The [`rustc`][1] output field name that shows the target. | ||
/// | ||
/// [1]: https://doc.rust-lang.org/rustc/what-is-rustc.html | ||
const TARGET_FIELD: &str = "host: "; | ||
|
||
// Query rustc for defaults. | ||
let output = std::process::Command::new(cargo_path) | ||
.arg("-vV") | ||
.output() | ||
.context(format!("failed to execute `{cargo_path} -vV`"))?; | ||
|
||
// Decode stdout. | ||
let stdout = std::str::from_utf8(&output.stdout).context("failed to read stdout into uft8")?; | ||
|
||
// Parse the default target from stdout. | ||
stdout | ||
.lines() | ||
.find(|l| l.starts_with(TARGET_FIELD)) | ||
.map(|l| &l[TARGET_FIELD.len()..]) | ||
.context(format!("failed to parse target from {cargo_path} output")) | ||
.map(str::to_owned) | ||
} |
File renamed without changes.