|
| 1 | +// Copyright 2019-2023 Tauri Programme within The Commons Conservancy |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +use std::{collections::HashSet, path::PathBuf}; |
| 6 | + |
| 7 | +use clap::Parser; |
| 8 | +use tauri_utils::acl::capability::{Capability, PermissionEntry}; |
| 9 | + |
| 10 | +use crate::{ |
| 11 | + acl::FileFormat, |
| 12 | + helpers::{app_paths::tauri_dir, prompts}, |
| 13 | + Result, |
| 14 | +}; |
| 15 | + |
| 16 | +#[derive(Debug, Parser)] |
| 17 | +#[clap(about = "Create a new permission file")] |
| 18 | +pub struct Options { |
| 19 | + /// Capability identifier. |
| 20 | + identifier: Option<String>, |
| 21 | + /// Capability description |
| 22 | + #[clap(long)] |
| 23 | + description: Option<String>, |
| 24 | + /// Capability windows |
| 25 | + #[clap(long)] |
| 26 | + windows: Option<Vec<String>>, |
| 27 | + /// Capability permissions |
| 28 | + #[clap(long)] |
| 29 | + permission: Option<Vec<String>>, |
| 30 | + /// Output file format. |
| 31 | + #[clap(long, default_value_t = FileFormat::Json)] |
| 32 | + format: FileFormat, |
| 33 | + /// The output file. |
| 34 | + #[clap(short, long)] |
| 35 | + out: Option<PathBuf>, |
| 36 | +} |
| 37 | + |
| 38 | +pub fn command(options: Options) -> Result<()> { |
| 39 | + let identifier = match options.identifier { |
| 40 | + Some(i) => i, |
| 41 | + None => prompts::input("What's the capability identifier?", None, false, false)?.unwrap(), |
| 42 | + }; |
| 43 | + |
| 44 | + let description = match options.description { |
| 45 | + Some(d) => Some(d), |
| 46 | + None => prompts::input::<String>("What's the capability description?", None, false, true)? |
| 47 | + .and_then(|d| if d.is_empty() { None } else { Some(d) }), |
| 48 | + }; |
| 49 | + |
| 50 | + let windows = match options.windows.map(FromIterator::from_iter) { |
| 51 | + Some(w) => w, |
| 52 | + None => prompts::input::<String>( |
| 53 | + "Which windows should be affected by this? (comma separated)", |
| 54 | + Some("main".into()), |
| 55 | + false, |
| 56 | + false, |
| 57 | + )? |
| 58 | + .and_then(|d| { |
| 59 | + if d.is_empty() { |
| 60 | + None |
| 61 | + } else { |
| 62 | + Some(d.split(',').map(ToString::to_string).collect()) |
| 63 | + } |
| 64 | + }) |
| 65 | + .unwrap_or_default(), |
| 66 | + }; |
| 67 | + |
| 68 | + let permissions: HashSet<String> = match options.permission.map(FromIterator::from_iter) { |
| 69 | + Some(p) => p, |
| 70 | + None => prompts::input::<String>( |
| 71 | + "What permissions to enable? (comma separated)", |
| 72 | + None, |
| 73 | + false, |
| 74 | + true, |
| 75 | + )? |
| 76 | + .and_then(|p| { |
| 77 | + if p.is_empty() { |
| 78 | + None |
| 79 | + } else { |
| 80 | + Some(p.split(',').map(ToString::to_string).collect()) |
| 81 | + } |
| 82 | + }) |
| 83 | + .unwrap_or_default(), |
| 84 | + }; |
| 85 | + |
| 86 | + let capability = Capability { |
| 87 | + identifier, |
| 88 | + description: description.unwrap_or_default(), |
| 89 | + remote: None, |
| 90 | + local: true, |
| 91 | + windows, |
| 92 | + webviews: Vec::new(), |
| 93 | + permissions: permissions |
| 94 | + .into_iter() |
| 95 | + .map(|p| { |
| 96 | + PermissionEntry::PermissionRef( |
| 97 | + p.clone() |
| 98 | + .try_into() |
| 99 | + .unwrap_or_else(|_| panic!("invalid permission {}", p)), |
| 100 | + ) |
| 101 | + }) |
| 102 | + .collect(), |
| 103 | + platforms: Vec::new(), |
| 104 | + }; |
| 105 | + |
| 106 | + let path = match options.out { |
| 107 | + Some(o) => o.canonicalize()?, |
| 108 | + None => { |
| 109 | + let dir = tauri_dir(); |
| 110 | + let capabilities_dir = dir.join("capabilities"); |
| 111 | + capabilities_dir.join(format!( |
| 112 | + "{}.{}", |
| 113 | + capability.identifier, |
| 114 | + options.format.extension() |
| 115 | + )) |
| 116 | + } |
| 117 | + }; |
| 118 | + |
| 119 | + if path.exists() { |
| 120 | + let msg = format!( |
| 121 | + "Capability already exists at {}", |
| 122 | + dunce::simplified(&path).display() |
| 123 | + ); |
| 124 | + let overwrite = prompts::confirm(&format!("{msg}, overwrite?"), Some(false))?; |
| 125 | + if overwrite { |
| 126 | + std::fs::remove_file(&path)?; |
| 127 | + } else { |
| 128 | + anyhow::bail!(msg); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + if let Some(parent) = path.parent() { |
| 133 | + std::fs::create_dir_all(parent)?; |
| 134 | + } |
| 135 | + |
| 136 | + std::fs::write(&path, options.format.serialize(&capability)?)?; |
| 137 | + |
| 138 | + log::info!(action = "Created"; "capability at {}", dunce::simplified(&path).display()); |
| 139 | + |
| 140 | + Ok(()) |
| 141 | +} |
0 commit comments