Refactor UpdateMode

This commit is contained in:
Gregory Schier
2024-05-13 07:28:45 -07:00
parent 967590c7ff
commit b640f0c357
2 changed files with 85 additions and 80 deletions

View File

@@ -57,7 +57,7 @@ use crate::models::{
}; };
use crate::plugin::{ImportResult, run_plugin_export_curl, run_plugin_import}; use crate::plugin::{ImportResult, run_plugin_export_curl, run_plugin_import};
use crate::render::render_request; use crate::render::render_request;
use crate::updates::{update_mode_from_str, UpdateMode, YaakUpdater}; use crate::updates::{UpdateMode, YaakUpdater};
use crate::window_menu::app_menu; use crate::window_menu::app_menu;
mod analytics; mod analytics;
@@ -218,8 +218,8 @@ async fn cmd_grpc_go(
..Default::default() ..Default::default()
}, },
) )
.await .await
.map_err(|e| e.to_string())? .map_err(|e| e.to_string())?
}; };
let conn_id = conn.id.clone(); let conn_id = conn.id.clone();
@@ -316,8 +316,8 @@ async fn cmd_grpc_go(
..base_msg.clone() ..base_msg.clone()
}, },
) )
.await .await
.unwrap(); .unwrap();
}); });
return; return;
} }
@@ -332,8 +332,8 @@ async fn cmd_grpc_go(
..base_msg.clone() ..base_msg.clone()
}, },
) )
.await .await
.unwrap(); .unwrap();
}); });
} }
Ok(IncomingMsg::Commit) => { Ok(IncomingMsg::Commit) => {
@@ -372,8 +372,8 @@ async fn cmd_grpc_go(
..base_event.clone() ..base_event.clone()
}, },
) )
.await .await
.unwrap(); .unwrap();
async move { async move {
let (maybe_stream, maybe_msg) = match ( let (maybe_stream, maybe_msg) = match (
@@ -419,8 +419,8 @@ async fn cmd_grpc_go(
..base_event.clone() ..base_event.clone()
}, },
) )
.await .await
.unwrap(); .unwrap();
} }
match maybe_msg { match maybe_msg {
@@ -434,13 +434,13 @@ async fn cmd_grpc_go(
} else { } else {
"Received response with metadata" "Received response with metadata"
} }
.to_string(), .to_string(),
event_type: GrpcEventType::Info, event_type: GrpcEventType::Info,
..base_event.clone() ..base_event.clone()
}, },
) )
.await .await
.unwrap(); .unwrap();
upsert_grpc_event( upsert_grpc_event(
&w, &w,
&GrpcEvent { &GrpcEvent {
@@ -449,8 +449,8 @@ async fn cmd_grpc_go(
..base_event.clone() ..base_event.clone()
}, },
) )
.await .await
.unwrap(); .unwrap();
upsert_grpc_event( upsert_grpc_event(
&w, &w,
&GrpcEvent { &GrpcEvent {
@@ -460,8 +460,8 @@ async fn cmd_grpc_go(
..base_event.clone() ..base_event.clone()
}, },
) )
.await .await
.unwrap(); .unwrap();
} }
Some(Err(e)) => { Some(Err(e)) => {
upsert_grpc_event( upsert_grpc_event(
@@ -484,8 +484,8 @@ async fn cmd_grpc_go(
}, },
}), }),
) )
.await .await
.unwrap(); .unwrap();
} }
None => { None => {
// Server streaming doesn't return initial message // Server streaming doesn't return initial message
@@ -503,13 +503,13 @@ async fn cmd_grpc_go(
} else { } else {
"Received response with metadata" "Received response with metadata"
} }
.to_string(), .to_string(),
event_type: GrpcEventType::Info, event_type: GrpcEventType::Info,
..base_event.clone() ..base_event.clone()
}, },
) )
.await .await
.unwrap(); .unwrap();
stream.into_inner() stream.into_inner()
} }
Some(Err(e)) => { Some(Err(e)) => {
@@ -533,8 +533,8 @@ async fn cmd_grpc_go(
}, },
}), }),
) )
.await .await
.unwrap(); .unwrap();
return; return;
} }
None => return, None => return,
@@ -552,8 +552,8 @@ async fn cmd_grpc_go(
..base_event.clone() ..base_event.clone()
}, },
) )
.await .await
.unwrap(); .unwrap();
} }
Ok(None) => { Ok(None) => {
let trailers = stream let trailers = stream
@@ -571,8 +571,8 @@ async fn cmd_grpc_go(
..base_event.clone() ..base_event.clone()
}, },
) )
.await .await
.unwrap(); .unwrap();
break; break;
} }
Err(status) => { Err(status) => {
@@ -586,8 +586,8 @@ async fn cmd_grpc_go(
..base_event.clone() ..base_event.clone()
}, },
) )
.await .await
.unwrap(); .unwrap();
} }
} }
} }
@@ -688,7 +688,7 @@ async fn cmd_send_ephemeral_request(
None, None,
&mut cancel_rx, &mut cancel_rx,
) )
.await .await
} }
#[tauri::command] #[tauri::command]
@@ -755,7 +755,7 @@ async fn cmd_import_data(
AnalyticsAction::Import, AnalyticsAction::Import,
Some(json!({ "plugin": plugin_name })), Some(json!({ "plugin": plugin_name })),
) )
.await; .await;
result = Some(r); result = Some(r);
break; break;
} }
@@ -786,7 +786,7 @@ async fn cmd_import_data(
let maybe_gen_id_opt = |id: Option<String>, let maybe_gen_id_opt = |id: Option<String>,
model: ModelType, model: ModelType,
ids: &mut HashMap<String, String>| ids: &mut HashMap<String, String>|
-> Option<String> { -> Option<String> {
match id { match id {
Some(id) => Some(maybe_gen_id(id.as_str(), model, ids)), Some(id) => Some(maybe_gen_id(id.as_str(), model, ids)),
None => None, None => None,
@@ -932,7 +932,7 @@ async fn cmd_export_data(
AnalyticsAction::Export, AnalyticsAction::Export,
None, None,
) )
.await; .await;
Ok(()) Ok(())
} }
@@ -983,8 +983,8 @@ async fn cmd_send_http_request(
None, None,
None, None,
) )
.await .await
.expect("Failed to create response"); .expect("Failed to create response");
let download_path = if let Some(p) = download_dir { let download_path = if let Some(p) = download_dir {
Some(std::path::Path::new(p).to_path_buf()) Some(std::path::Path::new(p).to_path_buf())
@@ -1009,7 +1009,7 @@ async fn cmd_send_http_request(
download_path, download_path,
&mut cancel_rx, &mut cancel_rx,
) )
.await .await
} }
async fn response_err( async fn response_err(
@@ -1117,8 +1117,8 @@ async fn cmd_create_cookie_jar(
..Default::default() ..Default::default()
}, },
) )
.await .await
.map_err(|e| e.to_string()) .map_err(|e| e.to_string())
} }
#[tauri::command] #[tauri::command]
@@ -1137,8 +1137,8 @@ async fn cmd_create_environment(
..Default::default() ..Default::default()
}, },
) )
.await .await
.map_err(|e| e.to_string()) .map_err(|e| e.to_string())
} }
#[tauri::command] #[tauri::command]
@@ -1159,8 +1159,8 @@ async fn cmd_create_grpc_request(
..Default::default() ..Default::default()
}, },
) )
.await .await
.map_err(|e| e.to_string()) .map_err(|e| e.to_string())
} }
#[tauri::command] #[tauri::command]
@@ -1194,8 +1194,8 @@ async fn cmd_create_http_request(
..Default::default() ..Default::default()
}, },
) )
.await .await
.map_err(|e| e.to_string()) .map_err(|e| e.to_string())
} }
#[tauri::command] #[tauri::command]
@@ -1287,8 +1287,8 @@ async fn cmd_create_folder(
..Default::default() ..Default::default()
}, },
) )
.await .await
.map_err(|e| e.to_string()) .map_err(|e| e.to_string())
} }
#[tauri::command] #[tauri::command]
@@ -1418,8 +1418,8 @@ async fn cmd_list_cookie_jars(
..Default::default() ..Default::default()
}, },
) )
.await .await
.expect("Failed to create CookieJar"); .expect("Failed to create CookieJar");
Ok(vec![cookie_jar]) Ok(vec![cookie_jar])
} else { } else {
Ok(cookie_jars) Ok(cookie_jars)
@@ -1488,8 +1488,8 @@ async fn cmd_list_workspaces(w: WebviewWindow) -> Result<Vec<Workspace>, String>
..Default::default() ..Default::default()
}, },
) )
.await .await
.expect("Failed to create Workspace"); .expect("Failed to create Workspace");
Ok(vec![workspace]) Ok(vec![workspace])
} else { } else {
Ok(workspaces) Ok(workspaces)
@@ -1731,16 +1731,16 @@ fn create_window(handle: &AppHandle, url: Option<&str>) -> WebviewWindow {
window_id, window_id,
WebviewUrl::App(url.unwrap_or_default().into()), WebviewUrl::App(url.unwrap_or_default().into()),
) )
.resizable(true) .resizable(true)
.fullscreen(false) .fullscreen(false)
.disable_drag_drop_handler() // Required for frontend Dnd on windows .disable_drag_drop_handler() // Required for frontend Dnd on windows
.inner_size(1100.0, 600.0) .inner_size(1100.0, 600.0)
.position( .position(
// Randomly offset so windows don't stack exactly // Randomly offset so windows don't stack exactly
100.0 + random::<f64>() * 30.0, 100.0 + random::<f64>() * 30.0,
100.0 + random::<f64>() * 30.0, 100.0 + random::<f64>() * 30.0,
) )
.title(handle.package_info().name.to_string()); .title(handle.package_info().name.to_string());
// Add macOS-only things // Add macOS-only things
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
@@ -1820,7 +1820,7 @@ fn create_window(handle: &AppHandle, url: Option<&str>) -> WebviewWindow {
async fn get_update_mode(h: &AppHandle) -> UpdateMode { async fn get_update_mode(h: &AppHandle) -> UpdateMode {
let settings = get_or_create_settings(h).await; let settings = get_or_create_settings(h).await;
update_mode_from_str(settings.update_channel.as_str()) UpdateMode::new(settings.update_channel.as_str())
} }
fn safe_uri(endpoint: &str) -> Result<Uri, InvalidUri> { fn safe_uri(endpoint: &str) -> Result<Uri, InvalidUri> {

View File

@@ -1,3 +1,4 @@
use std::fmt::{Display, Formatter};
use std::time::SystemTime; use std::time::SystemTime;
use log::info; use log::info;
@@ -20,6 +21,25 @@ pub enum UpdateMode {
Beta, Beta,
} }
impl Display for UpdateMode {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let s = match self {
UpdateMode::Stable => "stable",
UpdateMode::Beta => "beta",
};
write!(f, "{}", s)
}
}
impl UpdateMode {
pub fn new(mode: &str) -> UpdateMode {
match mode {
"beta" => UpdateMode::Beta,
_ => UpdateMode::Stable,
}
}
}
impl YaakUpdater { impl YaakUpdater {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
@@ -33,11 +53,10 @@ impl YaakUpdater {
) -> Result<bool, tauri_plugin_updater::Error> { ) -> Result<bool, tauri_plugin_updater::Error> {
self.last_update_check = SystemTime::now(); self.last_update_check = SystemTime::now();
let update_mode = get_update_mode_str(mode);
let enabled = !is_dev(); let enabled = !is_dev();
info!( info!(
"Checking for updates mode={} enabled={}", "Checking for updates mode={} enabled={}",
update_mode, enabled mode, enabled
); );
if !enabled { if !enabled {
@@ -46,7 +65,7 @@ impl YaakUpdater {
match app_handle match app_handle
.updater_builder() .updater_builder()
.header("X-Update-Mode", update_mode)? .header("X-Update-Mode", mode.to_string())?
.build()? .build()?
.check() .check()
.await .await
@@ -102,17 +121,3 @@ impl YaakUpdater {
self.force_check(app_handle, mode).await self.force_check(app_handle, mode).await
} }
} }
pub fn update_mode_from_str(mode: &str) -> UpdateMode {
match mode {
"beta" => UpdateMode::Beta,
_ => UpdateMode::Stable,
}
}
fn get_update_mode_str(mode: UpdateMode) -> &'static str {
match mode {
UpdateMode::Stable => "stable",
UpdateMode::Beta => "beta",
}
}