forked from rtk-ai/rtk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrewrite_cmd.rs
More file actions
50 lines (44 loc) · 1.26 KB
/
Copy pathrewrite_cmd.rs
File metadata and controls
50 lines (44 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use crate::discover::registry;
/// Run the `rtk rewrite` command.
///
/// Prints the RTK-rewritten command to stdout and exits 0.
/// Exits 1 (without output) if the command has no RTK equivalent.
///
/// Used by shell hooks to rewrite commands transparently:
/// ```bash
/// REWRITTEN=$(rtk rewrite "$CMD") || exit 0
/// [ "$CMD" = "$REWRITTEN" ] && exit 0 # already RTK, skip
/// ```
pub fn run(cmd: &str) -> anyhow::Result<()> {
let excluded = crate::config::Config::load()
.map(|c| c.hooks.exclude_commands)
.unwrap_or_default();
match registry::rewrite_command(cmd, &excluded) {
Some(rewritten) => {
print!("{}", rewritten);
Ok(())
}
None => {
std::process::exit(1);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_run_supported_command_succeeds() {
assert!(registry::rewrite_command("git status", &[]).is_some());
}
#[test]
fn test_run_unsupported_returns_none() {
assert!(registry::rewrite_command("htop", &[]).is_none());
}
#[test]
fn test_run_already_rtk_returns_some() {
assert_eq!(
registry::rewrite_command("rtk git status", &[]),
Some("rtk git status".into())
);
}
}