|
1 |
| -use std::{collections::HashSet, env, fs, path, sync::RwLock}; |
| 1 | +use std::{collections::HashSet, fs, path, sync::RwLock}; |
2 | 2 |
|
3 |
| -use anyhow::anyhow; |
4 | 3 | use tauri::api::dialog::blocking::{ask, FileDialogBuilder, MessageDialogBuilder};
|
5 | 4 |
|
6 | 5 | use crate::restic::*;
|
@@ -116,119 +115,16 @@ impl SharedAppState {
|
116 | 115 |
|
117 | 116 | // -------------------------------------------------------------------------------------------------
|
118 | 117 |
|
119 |
| -struct LocationInfo<'a> { |
120 |
| - prefix: &'a str, |
121 |
| - credentials: Vec<&'a str>, |
122 |
| -} |
123 |
| - |
124 |
| -fn location_infos() -> Vec<LocationInfo<'static>> { |
125 |
| - vec![ |
126 |
| - LocationInfo { |
127 |
| - prefix: "bs", |
128 |
| - credentials: vec![], |
129 |
| - }, |
130 |
| - LocationInfo { |
131 |
| - prefix: "sftp", |
132 |
| - credentials: vec![], |
133 |
| - }, |
134 |
| - LocationInfo { |
135 |
| - prefix: "rest", |
136 |
| - credentials: vec![], |
137 |
| - }, |
138 |
| - LocationInfo { |
139 |
| - prefix: "rclone", |
140 |
| - credentials: vec![], |
141 |
| - }, |
142 |
| - LocationInfo { |
143 |
| - prefix: "s3", |
144 |
| - credentials: vec!["AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY"], |
145 |
| - }, |
146 |
| - LocationInfo { |
147 |
| - prefix: "b2", |
148 |
| - credentials: vec!["B2_ACCOUNT_ID", "B2_ACCOUNT_KEY"], |
149 |
| - }, |
150 |
| - LocationInfo { |
151 |
| - prefix: "azure", |
152 |
| - credentials: vec!["AZURE_ACCOUNT_NAME", "AZURE_ACCOUNT_KEY"], |
153 |
| - }, |
154 |
| - ] |
155 |
| -} |
156 |
| - |
157 |
| -// defaultRepo determines the default restic repository location from the environment. |
158 |
| -fn default_repo() -> anyhow::Result<String> { |
159 |
| - if let Ok(repository_file) = env::var("RESTIC_REPOSITORY_FILE") { |
160 |
| - let content = fs::read_to_string(repository_file.clone()); |
161 |
| - if let Ok(content) = content { |
162 |
| - return Ok(content.trim().replace('\n', "")); |
163 |
| - } else { |
164 |
| - return Err(anyhow!("{repository_file} does not exist")); |
165 |
| - } |
166 |
| - } |
167 |
| - if let Ok(repository) = env::var("RESTIC_REPOSITORY") { |
168 |
| - Ok(repository) |
169 |
| - } else { |
170 |
| - Err(anyhow!("No repository set")) |
171 |
| - } |
172 |
| -} |
173 |
| - |
174 |
| -// defaultRepoPassword determines the default restic repository password from the environment. |
175 |
| -fn default_repo_password() -> anyhow::Result<String> { |
176 |
| - if let Ok(password_file) = env::var("RESTIC_PASSWORD_FILE") { |
177 |
| - let content = fs::read_to_string(password_file.clone()); |
178 |
| - if let Ok(content) = content { |
179 |
| - return Ok(content.trim().replace('\n', "")); |
180 |
| - } else { |
181 |
| - return Err(anyhow!("{password_file} does not exist")); |
182 |
| - } |
183 |
| - } |
184 |
| - if let Ok(repository) = env::var("RESTIC_PASSWORD") { |
185 |
| - Ok(repository) |
186 |
| - } else { |
187 |
| - Err(anyhow!("No repository password set")) |
188 |
| - } |
| 118 | +#[tauri::command] |
| 119 | +pub fn open_file_or_url(path: String) -> Result<(), String> { |
| 120 | + open::that(path).map_err(|err| err.to_string()) |
189 | 121 | }
|
190 | 122 |
|
191 | 123 | // -------------------------------------------------------------------------------------------------
|
192 | 124 |
|
193 | 125 | #[tauri::command]
|
194 |
| -pub fn default_repo_location() -> Result<Location, String> { |
195 |
| - // read default location from env |
196 |
| - let mut location = Location { |
197 |
| - path: default_repo().unwrap_or_default(), |
198 |
| - password: default_repo_password().unwrap_or_default(), |
199 |
| - credentials: vec![], |
200 |
| - prefix: "".to_string(), |
201 |
| - }; |
202 |
| - if location.path.is_empty() { |
203 |
| - // skip reading other location info when no repo is present |
204 |
| - return Ok(location); |
205 |
| - } |
206 |
| - location.prefix = "".to_string(); |
207 |
| - for location_info in location_infos() { |
208 |
| - if location |
209 |
| - .path |
210 |
| - .starts_with(&(location_info.prefix.to_string() + ":")) |
211 |
| - { |
212 |
| - location.prefix = location_info.prefix.to_string(); |
213 |
| - location.path = |
214 |
| - location |
215 |
| - .path |
216 |
| - .replacen(&(location_info.prefix.to_string() + ":"), "", 1); |
217 |
| - for credential in location_info.credentials { |
218 |
| - location.credentials.push(EnvValue { |
219 |
| - name: credential.to_string(), |
220 |
| - value: env::var(credential).unwrap_or_default(), |
221 |
| - }) |
222 |
| - } |
223 |
| - break; |
224 |
| - } |
225 |
| - } |
226 |
| - Ok(location) |
227 |
| -} |
228 |
| - |
229 |
| -#[tauri::command] |
230 |
| -pub fn open_file_or_url(path: String) -> Result<(), String> { |
231 |
| - open::that(path).map_err(|err| err.to_string()) |
| 126 | +pub fn default_repo_location(app_state: tauri::State<SharedAppState>) -> Result<Location, String> { |
| 127 | + Ok(app_state.get()?.location) |
232 | 128 | }
|
233 | 129 |
|
234 | 130 | // -------------------------------------------------------------------------------------------------
|
|
0 commit comments