1
+ <!DOCTYPE html> < html lang ="en "> < head > < meta charset ="utf-8 "> < meta name ="viewport " content ="width=device-width, initial-scale=1.0 "> < meta name ="generator " content ="rustdoc "> < meta name ="description " content ="Executes a system command with `sudo` privileges, requiring elevated permissions. "> < title > run_sudo_command in glwiz::functionality::commands - Rust</ title > < script > if ( window . location . protocol !== "file:" ) document . head . insertAdjacentHTML ( "beforeend" , "SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2" . split ( "," ) . map ( f => `<link rel="preload" as="font" type="font/woff2" crossorigin href="../../../static.files/${ f } ">` ) . join ( "" ) ) </ script > < link rel ="stylesheet " href ="../../../static.files/normalize-9960930a.css "> < link rel ="stylesheet " href ="../../../static.files/rustdoc-84e720fa.css "> < meta name ="rustdoc-vars " data-root-path ="../../../ " data-static-root-path ="../../../static.files/ " data-current-crate ="glwiz " data-themes ="" data-resource-suffix ="" data-rustdoc-version ="1.89.0 (29483883e 2025-08-04) " data-channel ="1.89.0 " data-search-js ="search-92309212.js " data-settings-js ="settings-5514c975.js " > < script src ="../../../static.files/storage-4e99c027.js "> </ script > < script defer src ="sidebar-items.js "> </ script > < script defer src ="../../../static.files/main-fd3af306.js "> </ script > < noscript > < link rel ="stylesheet " href ="../../../static.files/noscript-32bb7600.css "> </ noscript > < link rel ="alternate icon " type ="image/png " href ="../../../static.files/favicon-32x32-6580c154.png "> < link rel ="icon " type ="image/svg+xml " href ="../../../static.files/favicon-044be391.svg "> </ head > < body class ="rustdoc fn "> <!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--> < nav class ="mobile-topbar "> < button class ="sidebar-menu-toggle " title ="show sidebar "> </ button > </ nav > < nav class ="sidebar "> < div class ="sidebar-crate "> < h2 > < a href ="../../../glwiz/index.html "> glwiz</ a > < span class ="version "> 1.0.2</ span > </ h2 > </ div > < div class ="sidebar-elems "> < section id ="rustdoc-toc "> < h2 class ="location "> < a href ="# "> run_< wbr > sudo_< wbr > command</ a > </ h2 > < h3 > < a href ="# "> Sections</ a > </ h3 > < ul class ="block top-toc "> < li > < a href ="#arguments " title ="Arguments "> Arguments</ a > </ li > < li > < a href ="#returns " title ="Returns "> Returns</ a > </ li > < li > < a href ="#errors " title ="Errors "> Errors</ a > </ li > < li > < a href ="#example " title ="Example "> Example</ a > </ li > < li > < a href ="#safety " title ="Safety "> Safety</ a > </ li > </ ul > </ section > < div id ="rustdoc-modnav "> < h2 > < a href ="index.html "> In glwiz::< wbr > functionality::< wbr > commands</ a > </ h2 > </ div > </ div > </ nav > < div class ="sidebar-resizer " title ="Drag to resize sidebar "> </ div > < main > < div class ="width-limiter "> < rustdoc-search > </ rustdoc-search > < section id ="main-content " class ="content "> < div class ="main-heading "> < div class ="rustdoc-breadcrumbs "> < a href ="../../index.html "> glwiz</ a > ::< wbr > < a href ="../index.html "> functionality</ a > ::< wbr > < a href ="index.html "> commands</ a > </ div > < h1 > Function < span class ="fn "> run_sudo_command</ span > < button id ="copy-path " title ="Copy item path to clipboard "> Copy item path</ button > </ h1 > < rustdoc-toolbar > </ rustdoc-toolbar > < span class ="sub-heading "> < a class ="src " href ="../../../src/glwiz/functionality/commands.rs.html#61-77 "> Source</ a > </ span > </ div > < pre class ="rust item-decl "> < code > pub fn run_sudo_command(command: &< a class ="primitive " href ="https://doc.rust-lang.org/1.89.0/std/primitive.str.html "> str</ a > , args: &[&< a class ="primitive " href ="https://doc.rust-lang.org/1.89.0/std/primitive.str.html "> str</ a > ]) -> < a class ="enum " href ="https://doc.rust-lang.org/1.89.0/core/result/enum.Result.html " title ="enum core::result::Result "> Result</ a > << a class ="primitive " href ="https://doc.rust-lang.org/1.89.0/std/primitive.unit.html "> ()</ a > , < a class ="struct " href ="https://doc.rust-lang.org/1.89.0/alloc/string/struct.String.html " title ="struct alloc::string::String "> String</ a > ></ code > </ pre > < details class ="toggle top-doc " open > < summary class ="hideme "> < span > Expand description</ span > </ summary > < div class ="docblock "> < p > Executes a system command with < code > sudo</ code > privileges, requiring elevated permissions.</ p >
2
+ < p > This function runs a specified command with < code > sudo</ code > , passing the provided arguments, and captures
3
+ its output. It is used in the “glwiz” project for tasks requiring root access, such as copying
4
+ configuration files to system directories or applying iptables rules. The function ensures robust
5
+ error handling by reporting command failures with detailed stdout and stderr messages.</ p >
6
+ < h2 id ="arguments "> < a class ="doc-anchor " href ="#arguments "> §</ a > Arguments</ h2 >
7
+ < ul >
8
+ < li > < code > command</ code > - The command to execute (e.g., < code > "cp"</ code > , < code > "iptables-restore"</ code > ).</ li >
9
+ < li > < code > args</ code > - A slice of arguments to pass to the command (e.g., < code > &["-r", "/src", "/dest"]</ code > ).</ li >
10
+ </ ul >
11
+ < h2 id ="returns "> < a class ="doc-anchor " href ="#returns "> §</ a > Returns</ h2 >
12
+ < ul >
13
+ < li > < code > Ok(())</ code > - The command executed successfully.</ li >
14
+ < li > < code > Err(String)</ code > - An error message describing the failure, including stdout and stderr if applicable.</ li >
15
+ </ ul >
16
+ < h2 id ="errors "> < a class ="doc-anchor " href ="#errors "> §</ a > Errors</ h2 >
17
+ < p > Returns an error if:</ p >
18
+ < ul >
19
+ < li > The < code > sudo</ code > command fails to spawn (e.g., < code > sudo</ code > is not installed).</ li >
20
+ < li > The command exits with a non-zero status, indicating failure.</ li >
21
+ < li > The command’s output cannot be captured or processed.</ li >
22
+ </ ul >
23
+ < h2 id ="example "> < a class ="doc-anchor " href ="#example "> §</ a > Example</ h2 >
24
+ < div class ="example-wrap "> < pre class ="rust rust-example-rendered "> < code > < span class ="kw "> use </ span > glwiz::functionality::commands::run_sudo_command;
25
+ < span class ="kw "> let </ span > result = run_sudo_command(< span class ="string "> "cp"</ span > , < span class ="kw-2 "> &</ span > [< span class ="string "> "/src/file"</ span > , < span class ="string "> "/dest/file"</ span > ]);
26
+ < span class ="kw "> match </ span > result {
27
+ < span class ="prelude-val "> Ok</ span > (()) => < span class ="macro "> println!</ span > (< span class ="string "> "File copied successfully"</ span > ),
28
+ < span class ="prelude-val "> Err</ span > (e) => < span class ="macro "> eprintln!</ span > (< span class ="string "> "Failed to copy file: {}"</ span > , e),
29
+ }</ code > </ pre > </ div >
30
+ < h2 id ="safety "> < a class ="doc-anchor " href ="#safety "> §</ a > Safety</ h2 >
31
+ < p > Use caution when running commands with < code > sudo</ code > , as they execute with root privileges and can modify
32
+ critical system files. Ensure < code > command</ code > and < code > args</ code > are validated to prevent unintended consequences.</ p >
33
+ </ div > </ details > </ section > </ div > </ main > </ body > </ html >
0 commit comments