@@ -298,67 +298,58 @@ impl Sudoers {
298298 ) -> PathBuf {
299299 self . specify_host_user_runas ( on_host, am_user, Some ( target_user) ) ;
300300
301- let env_editor = self . settings . env_editor ( ) ;
302- self . select_editor ( env_editor)
301+ select_editor ( & self . settings , self . settings . env_editor ( ) )
303302 }
303+ }
304304
305- #[ cfg_attr( not( feature = "sudoedit" ) , allow( unused) ) ]
306- pub ( crate ) fn sudoedit_editor_path < User : UnixUser + PartialEq < User > > (
307- & self ,
308- on_host : & system:: Hostname ,
309- am_user : & User ,
310- target_user : & User ,
311- ) -> PathBuf {
312- self . select_editor ( true )
313- }
314-
315- fn select_editor ( & self , trusted_env : bool ) -> PathBuf {
316- let blessed_editors = self . settings . editor ( ) . expect ( "editor is always defined" ) ;
317-
318- let is_whitelisted = |path : & Path | -> bool {
319- trusted_env || blessed_editors. split ( ':' ) . any ( |x| Path :: new ( x) == path)
320- } ;
321-
322- // find editor in environment, if possible
305+ /// Retrieve the chosen editor from a settings object, filtering based on whether the
306+ /// environment is trusted (sudoedit) or maybe less so (visudo)
307+ fn select_editor ( settings : & Settings , trusted_env : bool ) -> PathBuf {
308+ let blessed_editors = settings. editor ( ) . expect ( "editor is always defined" ) ;
323309
324- for key in [ "SUDO_EDITOR" , "VISUAL" , "EDITOR" ] {
325- if let Some ( editor ) = std :: env :: var_os ( key ) {
326- let editor = PathBuf :: from ( editor ) ;
310+ let is_whitelisted = | path : & Path | -> bool {
311+ trusted_env || blessed_editors . split ( ':' ) . any ( |x| Path :: new ( x ) == path )
312+ } ;
327313
328- let editor = if can_execute ( & editor) {
329- editor
330- } else if let Some ( editor) = resolve_path (
331- & editor,
332- & std:: env:: var ( "PATH" ) . unwrap_or ( env ! ( "SUDO_PATH_DEFAULT" ) . to_string ( ) ) ,
333- ) {
334- editor
335- } else {
336- continue ;
337- } ;
314+ // find editor in environment, if possible
315+
316+ for key in [ "SUDO_EDITOR" , "VISUAL" , "EDITOR" ] {
317+ if let Some ( editor) = std:: env:: var_os ( key) {
318+ let editor = PathBuf :: from ( editor) ;
319+
320+ let editor = if can_execute ( & editor) {
321+ editor
322+ } else if let Some ( editor) = resolve_path (
323+ & editor,
324+ & std:: env:: var ( "PATH" ) . unwrap_or ( env ! ( "SUDO_PATH_DEFAULT" ) . to_string ( ) ) ,
325+ ) {
326+ editor
327+ } else {
328+ continue ;
329+ } ;
338330
339- if is_whitelisted ( & editor) {
340- return editor;
341- }
331+ if is_whitelisted ( & editor) {
332+ return editor;
342333 }
343334 }
335+ }
344336
345- // no acceptable editor found in environment, fallback on config
337+ // no acceptable editor found in environment, fallback on config
346338
347- for editor in blessed_editors. split ( ':' ) {
348- let editor = Path :: new ( editor) ;
349- if can_execute ( editor) {
350- return editor. to_owned ( ) ;
351- }
339+ for editor in blessed_editors. split ( ':' ) {
340+ let editor = Path :: new ( editor) ;
341+ if can_execute ( editor) {
342+ return editor. to_owned ( ) ;
352343 }
344+ }
353345
354- // fallback on hardcoded path -- always provide something to the caller
346+ // fallback on hardcoded path -- always provide something to the caller
355347
356- PathBuf :: from ( if cfg ! ( target_os = "linux" ) {
357- "/usr/bin/editor"
358- } else {
359- "/usr/bin/vi"
360- } )
361- }
348+ PathBuf :: from ( if cfg ! ( target_os = "linux" ) {
349+ "/usr/bin/editor"
350+ } else {
351+ "/usr/bin/vi"
352+ } )
362353}
363354
364355// a `take_while` variant that does not consume the first non-matching item
0 commit comments