ls.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. use clap::Parser;
  5. use crate::{helpers::app_paths::tauri_dir, Result};
  6. use colored::Colorize;
  7. use tauri_utils::acl::plugin::Manifest;
  8. use std::{collections::BTreeMap, fs::read_to_string};
  9. #[derive(Debug, Parser)]
  10. #[clap(about = "List permissions available to your application")]
  11. pub struct Options {
  12. /// Name of the plugin to list permissions.
  13. plugin: Option<String>,
  14. /// Permission identifier filter.
  15. #[clap(short, long)]
  16. filter: Option<String>,
  17. }
  18. pub fn command(options: Options) -> Result<()> {
  19. let tauri_dir = tauri_dir();
  20. let plugin_manifests_path = tauri_dir
  21. .join("gen")
  22. .join("schemas")
  23. .join("plugin-manifests.json");
  24. if plugin_manifests_path.exists() {
  25. let plugin_manifest_json = read_to_string(&plugin_manifests_path)?;
  26. let acl = serde_json::from_str::<BTreeMap<String, Manifest>>(&plugin_manifest_json)?;
  27. for (plugin, manifest) in acl {
  28. if options
  29. .plugin
  30. .as_ref()
  31. .map(|p| p != &plugin)
  32. .unwrap_or_default()
  33. {
  34. continue;
  35. }
  36. let mut permissions = Vec::new();
  37. if let Some(default) = manifest.default_permission {
  38. if options
  39. .filter
  40. .as_ref()
  41. .map(|f| "default".contains(f))
  42. .unwrap_or(true)
  43. {
  44. permissions.push(format!(
  45. "{}:{}\n{}\nPermissions: {}",
  46. plugin.magenta(),
  47. "default".cyan(),
  48. default.description,
  49. default
  50. .permissions
  51. .iter()
  52. .map(|c| c.cyan().to_string())
  53. .collect::<Vec<_>>()
  54. .join(", ")
  55. ));
  56. }
  57. }
  58. for set in manifest.permission_sets.values() {
  59. if options
  60. .filter
  61. .as_ref()
  62. .map(|f| set.identifier.contains(f))
  63. .unwrap_or(true)
  64. {
  65. permissions.push(format!(
  66. "{}:{}\n{}\nPermissions: {}",
  67. plugin.magenta(),
  68. set.identifier.cyan(),
  69. set.description,
  70. set
  71. .permissions
  72. .iter()
  73. .map(|c| c.cyan().to_string())
  74. .collect::<Vec<_>>()
  75. .join(", ")
  76. ));
  77. }
  78. }
  79. for permission in manifest.permissions.into_values() {
  80. if options
  81. .filter
  82. .as_ref()
  83. .map(|f| permission.identifier.contains(f))
  84. .unwrap_or(true)
  85. {
  86. permissions.push(format!(
  87. "{}:{}{}{}{}",
  88. plugin.magenta(),
  89. permission.identifier.cyan(),
  90. permission
  91. .description
  92. .map(|d| format!("\n{d}"))
  93. .unwrap_or_default(),
  94. if permission.commands.allow.is_empty() {
  95. "".to_string()
  96. } else {
  97. format!(
  98. "\n{}: {}",
  99. "Allow commands".bold(),
  100. permission
  101. .commands
  102. .allow
  103. .iter()
  104. .map(|c| c.green().to_string())
  105. .collect::<Vec<_>>()
  106. .join(", ")
  107. )
  108. },
  109. if permission.commands.deny.is_empty() {
  110. "".to_string()
  111. } else {
  112. format!(
  113. "\n{}: {}",
  114. "Deny commands".bold(),
  115. permission
  116. .commands
  117. .deny
  118. .iter()
  119. .map(|c| c.red().to_string())
  120. .collect::<Vec<_>>()
  121. .join(", ")
  122. )
  123. },
  124. ));
  125. }
  126. }
  127. if !permissions.is_empty() {
  128. println!("{}\n", permissions.join("\n\n"));
  129. }
  130. }
  131. Ok(())
  132. } else {
  133. anyhow::bail!("permission file not found, please build your application once first")
  134. }
  135. }