Tauri 限制對(duì) API 的訪問Command

2024-02-05 16:06 更新

允許列表對(duì)象有一個(gè)字段,用于定義可以使用的 CLI 數(shù)組。 每個(gè)CLI都是一個(gè)配置對(duì)象。shellscope{ name: string, cmd: string, sidecar?: bool, args?: boolean | Arg[] }

  • name:命令的唯一標(biāo)識(shí)符,傳遞給 Command 構(gòu)造函數(shù)。 如果是 sidecar,則此值必須是 上定義的值。tauri.conf.json > tauri > bundle > externalBin
  • cmd:在此配置上執(zhí)行的程序。如果是 sidecar,則忽略此值。
  • sidecar:對(duì)象是配置挎斗程序還是系統(tǒng)程序。
  • args:可以傳遞給程序的參數(shù)。默認(rèn)情況下,不允許使用任何參數(shù)。
    • true表示允許任何參數(shù)列表。
    • false表示不允許任何參數(shù)。
    • 否則,可以配置陣列。每個(gè)項(xiàng)目都是一個(gè)表示固定參數(shù)值的字符串 或定義驗(yàn)證參數(shù)值的正則表達(dá)式。{ validator: string }

示例范圍配置

命令行界面:git commit -m "the commit message"

配置:

{
"scope": [
{
"name": "run-git-commit",
"cmd": "git",
"args": ["commit", "-m", { "validator": "\\S+" }]
}
]
}

用法:

import { Command } from '@tauri-apps/api/shell'
new Command('run-git-commit', ['commit', '-m', 'the commit message'])

嘗試使用未在作用域上配置的程序執(zhí)行任何 API,會(huì)導(dǎo)致由于拒絕訪問而拒絕承諾。


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)