-
Notifications
You must be signed in to change notification settings - Fork 6.4k
fix(windows) shell_command on windows, minor parsing #6811
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6dcbba3
fix(windows) shell_command on windows, minor parsing
dylan-hurd-oai 752dade
windows tool tests
dylan-hurd-oai f527613
lowercase
dylan-hurd-oai 3f891f2
codex models only
dylan-hurd-oai 6e492df
fix model_tools test
dylan-hurd-oai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,93 @@ | ||
| use std::path::PathBuf; | ||
|
|
||
| use crate::shell::ShellType; | ||
| use crate::shell::detect_shell_type; | ||
|
|
||
| const POWERSHELL_FLAGS: &[&str] = &["-nologo", "-noprofile", "-command", "-c"]; | ||
|
|
||
| /// Extract the PowerShell script body from an invocation such as: | ||
| /// | ||
| /// - ["pwsh", "-NoProfile", "-Command", "Get-ChildItem -Recurse | Select-String foo"] | ||
| /// - ["powershell.exe", "-Command", "Write-Host hi"] | ||
| /// - ["powershell", "-NoLogo", "-NoProfile", "-Command", "...script..."] | ||
| /// | ||
| /// Returns (`shell`, `script`) when the first arg is a PowerShell executable and a | ||
| /// `-Command` (or `-c`) flag is present followed by a script string. | ||
| pub fn extract_powershell_command(command: &[String]) -> Option<(&str, &str)> { | ||
| if command.len() < 3 { | ||
| return None; | ||
| } | ||
|
|
||
| let shell = &command[0]; | ||
| if detect_shell_type(&PathBuf::from(shell)) != Some(ShellType::PowerShell) { | ||
| return None; | ||
| } | ||
|
|
||
| // Find the first occurrence of -Command (accept common short alias -c as well) | ||
| let mut i = 1usize; | ||
| while i + 1 < command.len() { | ||
| let flag = &command[i]; | ||
| // Reject unknown flags | ||
| if !POWERSHELL_FLAGS.contains(&flag.to_ascii_lowercase().as_str()) { | ||
| return None; | ||
| } | ||
| if flag.eq_ignore_ascii_case("-Command") || flag.eq_ignore_ascii_case("-c") { | ||
| let script = &command[i + 1]; | ||
| return Some((shell, script.as_str())); | ||
| } | ||
| i += 1; | ||
| } | ||
| None | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::extract_powershell_command; | ||
|
|
||
| #[test] | ||
| fn extracts_basic_powershell_command() { | ||
| let cmd = vec![ | ||
| "powershell".to_string(), | ||
| "-Command".to_string(), | ||
| "Write-Host hi".to_string(), | ||
| ]; | ||
| let (_shell, script) = extract_powershell_command(&cmd).expect("extract"); | ||
| assert_eq!(script, "Write-Host hi"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn extracts_lowercase_flags() { | ||
| let cmd = vec![ | ||
| "powershell".to_string(), | ||
| "-nologo".to_string(), | ||
| "-command".to_string(), | ||
| "Write-Host hi".to_string(), | ||
| ]; | ||
| let (_shell, script) = extract_powershell_command(&cmd).expect("extract"); | ||
| assert_eq!(script, "Write-Host hi"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn extracts_full_path_powershell_command() { | ||
| let command = if cfg!(windows) { | ||
| "C:\\windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe".to_string() | ||
| } else { | ||
| "/usr/local/bin/powershell.exe".to_string() | ||
| }; | ||
| let cmd = vec![command, "-Command".to_string(), "Write-Host hi".to_string()]; | ||
| let (_shell, script) = extract_powershell_command(&cmd).expect("extract"); | ||
| assert_eq!(script, "Write-Host hi"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn extracts_with_noprofile_and_alias() { | ||
| let cmd = vec![ | ||
| "pwsh".to_string(), | ||
| "-NoProfile".to_string(), | ||
| "-c".to_string(), | ||
| "Get-ChildItem | Select-String foo".to_string(), | ||
| ]; | ||
| let (_shell, script) = extract_powershell_command(&cmd).expect("extract"); | ||
| assert_eq!(script, "Get-ChildItem | Select-String foo"); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As noted in the PR description, I don't feel strongly about prioritizing cross-platform shell support in this PR. If we want to keep things simple, we can branch here.