Lucas Nogueira 2 жил өмнө
parent
commit
397b1b8c40

+ 24 - 26
core/tauri-codegen/src/context.rs

@@ -595,33 +595,31 @@ fn get_allowed_clis(root: &TokenStream, scope: &ShellAllowlistScope) -> TokenStr
       ShellAllowedArgs::Flag(false) => {
         // args are disallowed by default on ShellScopeAllowedCommandBuilder
       }
-      ShellAllowedArgs::List(list) => {
-        let args = list.iter().map(|arg| match arg {
-          ShellAllowedArg::Fixed(fixed) => {
-            quote!(#root::scope::ShellScopeAllowedArg::Fixed(#fixed.into()))
-          }
-          ShellAllowedArg::Var { validator } => {
-            let validator = match regex::Regex::new(validator) {
-              Ok(regex) => {
-                let regex = regex.as_str();
-                quote!(#root::regex::Regex::new(#regex).unwrap())
-              }
-              Err(error) => {
-                let error = error.to_string();
-                quote!({
-                  compile_error!(#error);
-                  #root::regex::Regex::new(#validator).unwrap()
-                })
-              }
-            };
-
-            quote!(#root::scope::ShellScopeAllowedArg::Var { validator: #validator })
-          }
-          _ => panic!("unknown shell scope arg, unable to prepare"),
-        });
-
+      ShellAllowedArgs::List(args) => {
         for arg in args {
-          code.append_all(quote!(.arg(#arg)));
+          match arg {
+            ShellAllowedArg::Fixed(fixed) => {
+              code.append_all(quote!(.arg(#fixed)));
+            }
+            ShellAllowedArg::Var { validator } => {
+              let validator = match regex::Regex::new(validator) {
+                Ok(regex) => {
+                  let regex = regex.as_str();
+                  quote!(#root::regex::Regex::new(#regex).unwrap())
+                }
+                Err(error) => {
+                  let error = error.to_string();
+                  quote!({
+                    compile_error!(#error);
+                    #root::regex::Regex::new(#validator).unwrap()
+                  })
+                }
+              };
+
+              code.append_all(quote!(.validated_arg(#validator)));
+            }
+            _ => panic!("unknown shell scope arg, unable to prepare"),
+          }
         }
       }
       _ => panic!("unknown shell scope command, unable to prepare"),

+ 34 - 4
core/tauri/src/scope/shell.rs

@@ -179,9 +179,39 @@ impl ScopeAllowedCommandBuilder {
     self
   }
 
-  /// Appends an argument to the command.
-  pub fn arg(mut self, arg: ScopeAllowedArg) -> Self {
-    self.0.args.get_or_insert_with(Default::default).push(arg);
+  /// Appends a fixed argument to the command.
+  ///
+  /// # Examples
+  ///
+  /// ```
+  /// tauri::scope::ShellScopeAllowedCommandBuilder::new("cargo")
+  ///   .arg("build")
+  ///   .build();
+  /// ```
+  pub fn arg(mut self, arg: impl Into<String>) -> Self {
+    self
+      .0
+      .args
+      .get_or_insert_with(Default::default)
+      .push(ScopeAllowedArg::Fixed(arg.into()));
+    self
+  }
+
+  /// Appends a validated argument to the command.
+  ///
+  /// # Examples
+  ///
+  /// ```
+  /// tauri::scope::ShellScopeAllowedCommandBuilder::new("cargo")
+  ///   .validated_arg("\\S+".parse().expect("invalid regex"))
+  ///   .build();
+  /// ```
+  pub fn validated_arg(mut self, validator: Regex) -> Self {
+    self
+      .0
+      .args
+      .get_or_insert_with(Default::default)
+      .push(ScopeAllowedArg::Var { validator });
     self
   }
 
@@ -322,7 +352,7 @@ impl Scope {
   /// use tauri::{Manager, scope::ShellScopeAllowedCommandBuilder};
   /// tauri::Builder::default()
   ///   .setup(|app| {
-  ///     app.shell_scope().allow("java", ShellScopeAllowedCommandBuilder::new("java").build());
+  ///     app.shell_scope().allow("cargo", ShellScopeAllowedCommandBuilder::new("cargo").arg("build").build());
   ///     app.shell_scope().allow("server-sidecar", ShellScopeAllowedCommandBuilder::sidecar("server").build());
   ///     Ok(())
   ///   });

+ 1 - 1
examples/api/src/views/Shell.svelte

@@ -29,7 +29,7 @@
     const command = new Command(cmd, [...args, script], {
       cwd: cwd || null,
       env: _getEnv(),
-      encoding,
+      encoding: encoding || null,
     })
 
     command.on('close', (data) => {