Struct tauri::scope::ShellScope

source ·
pub struct ShellScope(_);
Expand description

Scope for filesystem access.

Implementations§

Validates argument inputs and creates a Tauri sidecar Command.

Examples found in repository?
src/endpoints/shell.rs (line 119)
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
  fn execute<R: Runtime>(
    context: InvokeContext<R>,
    program: String,
    args: ExecuteArgs,
    on_event_fn: CallbackFn,
    options: CommandOptions,
  ) -> super::Result<ChildId> {
    let mut command = if options.sidecar {
      #[cfg(not(shell_sidecar))]
      return Err(crate::Error::ApiNotAllowlisted("shell > sidecar".to_string()).into_anyhow());
      #[cfg(shell_sidecar)]
      {
        let program = PathBuf::from(program);
        let program_as_string = program.display().to_string();
        let program_no_ext_as_string = program.with_extension("").display().to_string();
        let configured_sidecar = context
          .config
          .tauri
          .bundle
          .external_bin
          .as_ref()
          .map(|bins| {
            bins
              .iter()
              .find(|b| b == &&program_as_string || b == &&program_no_ext_as_string)
          })
          .unwrap_or_default();
        if let Some(sidecar) = configured_sidecar {
          context
            .window
            .state::<Scopes>()
            .shell
            .prepare_sidecar(&program.to_string_lossy(), sidecar, args)
            .map_err(crate::error::into_anyhow)?
        } else {
          return Err(crate::Error::SidecarNotAllowed(program).into_anyhow());
        }
      }
    } else {
      #[cfg(not(shell_execute))]
      return Err(crate::Error::ApiNotAllowlisted("shell > execute".to_string()).into_anyhow());
      #[cfg(shell_execute)]
      match context
        .window
        .state::<Scopes>()
        .shell
        .prepare(&program, args)
      {
        Ok(cmd) => cmd,
        Err(e) => {
          #[cfg(debug_assertions)]
          eprintln!("{}", e);
          return Err(crate::Error::ProgramNotAllowed(PathBuf::from(program)).into_anyhow());
        }
      }
    };
    #[cfg(any(shell_execute, shell_sidecar))]
    {
      if let Some(cwd) = options.cwd {
        command = command.current_dir(cwd);
      }
      if let Some(env) = options.env {
        command = command.envs(env);
      } else {
        command = command.env_clear();
      }
      if let Some(encoding) = options.encoding {
        if let Some(encoding) = crate::api::process::Encoding::for_label(encoding.as_bytes()) {
          command = command.encoding(encoding);
        } else {
          return Err(anyhow::anyhow!(format!("unknown encoding {}", encoding)));
        }
      }
      let (mut rx, child) = command.spawn()?;

      let pid = child.pid();
      command_child_store().lock().unwrap().insert(pid, child);

      crate::async_runtime::spawn(async move {
        while let Some(event) = rx.recv().await {
          if matches!(event, crate::api::process::CommandEvent::Terminated(_)) {
            command_child_store().lock().unwrap().remove(&pid);
          }
          let js = crate::api::ipc::format_callback(on_event_fn, &event)
            .expect("unable to serialize CommandEvent");

          let _ = context.window.eval(js.as_str());
        }
      });

      Ok(pid)
    }
  }

Validates argument inputs and creates a Tauri Command.

Examples found in repository?
src/endpoints/shell.rs (line 133)
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
  fn execute<R: Runtime>(
    context: InvokeContext<R>,
    program: String,
    args: ExecuteArgs,
    on_event_fn: CallbackFn,
    options: CommandOptions,
  ) -> super::Result<ChildId> {
    let mut command = if options.sidecar {
      #[cfg(not(shell_sidecar))]
      return Err(crate::Error::ApiNotAllowlisted("shell > sidecar".to_string()).into_anyhow());
      #[cfg(shell_sidecar)]
      {
        let program = PathBuf::from(program);
        let program_as_string = program.display().to_string();
        let program_no_ext_as_string = program.with_extension("").display().to_string();
        let configured_sidecar = context
          .config
          .tauri
          .bundle
          .external_bin
          .as_ref()
          .map(|bins| {
            bins
              .iter()
              .find(|b| b == &&program_as_string || b == &&program_no_ext_as_string)
          })
          .unwrap_or_default();
        if let Some(sidecar) = configured_sidecar {
          context
            .window
            .state::<Scopes>()
            .shell
            .prepare_sidecar(&program.to_string_lossy(), sidecar, args)
            .map_err(crate::error::into_anyhow)?
        } else {
          return Err(crate::Error::SidecarNotAllowed(program).into_anyhow());
        }
      }
    } else {
      #[cfg(not(shell_execute))]
      return Err(crate::Error::ApiNotAllowlisted("shell > execute".to_string()).into_anyhow());
      #[cfg(shell_execute)]
      match context
        .window
        .state::<Scopes>()
        .shell
        .prepare(&program, args)
      {
        Ok(cmd) => cmd,
        Err(e) => {
          #[cfg(debug_assertions)]
          eprintln!("{}", e);
          return Err(crate::Error::ProgramNotAllowed(PathBuf::from(program)).into_anyhow());
        }
      }
    };
    #[cfg(any(shell_execute, shell_sidecar))]
    {
      if let Some(cwd) = options.cwd {
        command = command.current_dir(cwd);
      }
      if let Some(env) = options.env {
        command = command.envs(env);
      } else {
        command = command.env_clear();
      }
      if let Some(encoding) = options.encoding {
        if let Some(encoding) = crate::api::process::Encoding::for_label(encoding.as_bytes()) {
          command = command.encoding(encoding);
        } else {
          return Err(anyhow::anyhow!(format!("unknown encoding {}", encoding)));
        }
      }
      let (mut rx, child) = command.spawn()?;

      let pid = child.pid();
      command_child_store().lock().unwrap().insert(pid, child);

      crate::async_runtime::spawn(async move {
        while let Some(event) = rx.recv().await {
          if matches!(event, crate::api::process::CommandEvent::Terminated(_)) {
            command_child_store().lock().unwrap().remove(&pid);
          }
          let js = crate::api::ipc::format_callback(on_event_fn, &event)
            .expect("unable to serialize CommandEvent");

          let _ = context.window.eval(js.as_str());
        }
      });

      Ok(pid)
    }
  }

Validates argument inputs and creates a Tauri Command.

Examples found in repository?
src/scope/shell.rs (line 219)
213
214
215
216
217
218
219
220
221
222
223
224
225
226
  pub fn prepare_sidecar(
    &self,
    command_name: &str,
    command_script: &str,
    args: ExecuteArgs,
  ) -> Result<Command, ScopeError> {
    self._prepare(command_name, args, Some(command_script))
  }

  /// Validates argument inputs and creates a Tauri [`Command`].
  #[cfg(shell_execute)]
  pub fn prepare(&self, command_name: &str, args: ExecuteArgs) -> Result<Command, ScopeError> {
    self._prepare(command_name, args, None)
  }

Open a path in the default (or specified) browser.

The path is validated against the tauri > allowlist > shell > open validation regex, which defaults to ^https?://.

Examples found in repository?
src/api/shell.rs (line 114)
108
109
110
111
112
113
114
115
116
pub fn open<P: AsRef<str>>(
  scope: &ShellScope,
  path: P,
  with: Option<Program>,
) -> crate::api::Result<()> {
  scope
    .open(path.as_ref(), with)
    .map_err(|err| crate::api::Error::Shell(format!("failed to open: {}", err)))
}

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more