Skip to content

Commit 5e50956

Browse files
committed
Move sudoedit special case
1 parent 199d5a9 commit 5e50956

File tree

2 files changed

+24
-42
lines changed

2 files changed

+24
-42
lines changed

src/sudo/cli/mod.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![forbid(unsafe_code)]
22

3+
use std::os::unix::ffi::OsStrExt;
34
use std::{borrow::Cow, mem};
45

56
use crate::common::{SudoPath, SudoString};
@@ -731,7 +732,24 @@ impl SudoOptions {
731732
SudoArg::Environment(key, value) => {
732733
options.env_var_list.push((key, value));
733734
}
734-
SudoArg::Rest(rest) => {
735+
SudoArg::Rest(mut rest) => {
736+
if let Some(cmd) = rest.first() {
737+
let cmd = std::path::Path::new(cmd);
738+
// This checks if the last character in the path is a /. This
739+
// works because the OS directly splits at b'/' without regards
740+
// for if it is part of another character (which it can't be
741+
// with UTF-8 anyways).
742+
let is_dir = cmd.as_os_str().as_bytes().ends_with(b"/");
743+
if !options.edit
744+
&& !is_dir
745+
&& (cmd.ends_with("sudoedit") || cmd.ends_with("sudoedit-rs"))
746+
{
747+
eprintln_ignore_io_error!("sudoedit doesn't need to be run via sudo");
748+
options.edit = true;
749+
rest.remove(0);
750+
}
751+
}
752+
735753
options.positional_args = rest;
736754
}
737755
}

src/sudo/mod.rs

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ use crate::system::{time::Duration, timestamp::SessionRecordFile, Process};
1111
pub(crate) use cli::SudoAction;
1212
#[cfg(not(test))]
1313
use cli::SudoAction;
14-
use std::os::unix::ffi::OsStrExt;
15-
use std::path::{Path, PathBuf};
14+
use std::path::PathBuf;
1615

1716
mod cli;
1817
pub(crate) use cli::{SudoEditOptions, SudoListOptions, SudoRunOptions, SudoValidateOptions};
@@ -112,47 +111,12 @@ fn sudo_process() -> Result<(), Error> {
112111
if options.positional_args.is_empty() && !options.shell && !options.login {
113112
eprintln_ignore_io_error!("{}", usage_msg);
114113
std::process::exit(1);
115-
}
114+
} else {
115+
#[cfg(feature = "dev")]
116+
unstable_warning();
116117

117-
if let Some(cmd) = options.positional_args.first() {
118-
let cmd = Path::new(cmd);
119-
// This checks if the last character in the path is a /. This
120-
// works because the OS directly splits at b'/' without regards
121-
// for if it is part of another character (which it can't be
122-
// with UTF-8 anyways).
123-
let is_dir = cmd.as_os_str().as_bytes().ends_with(b"/");
124-
if !is_dir && (cmd.ends_with("sudoedit") || cmd.ends_with("sudoedit-rs")) {
125-
eprintln_ignore_io_error!("sudoedit doesn't need to be run via sudo");
126-
#[cfg(feature = "sudoedit")]
127-
return pipeline::run_edit(SudoEditOptions {
128-
bell: options.bell,
129-
reset_timestamp: options.reset_timestamp,
130-
non_interactive: options.non_interactive,
131-
stdin: options.stdin,
132-
prompt: options.prompt,
133-
chdir: options.chdir,
134-
group: options.group,
135-
user: options.user,
136-
positional_args: {
137-
let mut args = options.positional_args;
138-
args.remove(0); // Remove the sudoedit command
139-
args
140-
},
141-
});
142-
#[cfg(not(feature = "sudoedit"))]
143-
{
144-
eprintln_ignore_io_error!(
145-
"error: `--edit` flag has not yet been implemented"
146-
);
147-
std::process::exit(1);
148-
}
149-
}
118+
pipeline::run(options)
150119
}
151-
152-
#[cfg(feature = "dev")]
153-
unstable_warning();
154-
155-
pipeline::run(options)
156120
}
157121
SudoAction::List(options) => pipeline::run_list(options),
158122
#[cfg(feature = "sudoedit")]

0 commit comments

Comments
 (0)