mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-18 23:44:12 +01:00
Hacky Yaak import complete!
This commit is contained in:
@@ -11,12 +11,12 @@ use std::collections::HashMap;
|
||||
use std::env::current_dir;
|
||||
use std::fs::{create_dir_all, File};
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
use std::process::exit;
|
||||
|
||||
use base64::Engine;
|
||||
use http::header::{HeaderName, ACCEPT, USER_AGENT};
|
||||
use http::{HeaderMap, HeaderValue, Method};
|
||||
use log::info;
|
||||
use rand::random;
|
||||
use reqwest::redirect::Policy;
|
||||
use serde::Serialize;
|
||||
@@ -35,6 +35,7 @@ use tokio::sync::Mutex;
|
||||
use window_ext::TrafficLightWindowExt;
|
||||
|
||||
use crate::analytics::{track_event, AnalyticsAction, AnalyticsResource};
|
||||
use crate::plugin::ImportResources;
|
||||
|
||||
mod analytics;
|
||||
mod models;
|
||||
@@ -266,32 +267,89 @@ async fn import_data(
|
||||
window: Window<Wry>,
|
||||
db_instance: State<'_, Mutex<Pool<Sqlite>>>,
|
||||
file_paths: Vec<&str>,
|
||||
) -> Result<plugin::ImportedResources, String> {
|
||||
) -> Result<ImportResources, String> {
|
||||
let pool = &*db_instance.lock().await;
|
||||
let imported = plugin::run_plugin_import(
|
||||
let mut resources = plugin::run_plugin_import(
|
||||
&window.app_handle(),
|
||||
pool,
|
||||
"insomnia-importer",
|
||||
file_paths.first().unwrap(),
|
||||
)
|
||||
.await;
|
||||
Ok(imported)
|
||||
println!("Resources: {:?}", resources);
|
||||
|
||||
if resources.is_none() {
|
||||
resources = plugin::run_plugin_import(
|
||||
&window.app_handle(),
|
||||
"yaak-importer",
|
||||
file_paths.first().unwrap(),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
println!("Resources: {:?}", resources);
|
||||
|
||||
match resources {
|
||||
None => Err("Failed to import data".to_string()),
|
||||
Some(r) => {
|
||||
let mut imported_resources = ImportResources::default();
|
||||
|
||||
info!("Importing resources");
|
||||
for w in r.workspaces {
|
||||
let x = models::upsert_workspace(pool, w)
|
||||
.await
|
||||
.expect("Failed to create workspace");
|
||||
imported_resources.workspaces.push(x.clone());
|
||||
info!("Imported workspace: {}", x.name);
|
||||
}
|
||||
|
||||
for e in r.environments {
|
||||
let x = models::upsert_environment(pool, e)
|
||||
.await
|
||||
.expect("Failed to create environment");
|
||||
imported_resources.environments.push(x.clone());
|
||||
info!("Imported environment: {}", x.name);
|
||||
}
|
||||
|
||||
for f in r.folders {
|
||||
let x = models::upsert_folder(pool, f)
|
||||
.await
|
||||
.expect("Failed to create folder");
|
||||
imported_resources.folders.push(x.clone());
|
||||
info!("Imported folder: {}", x.name);
|
||||
}
|
||||
|
||||
for r in r.requests {
|
||||
let x = models::upsert_request(pool, r)
|
||||
.await
|
||||
.expect("Failed to create request");
|
||||
imported_resources.requests.push(x.clone());
|
||||
info!("Imported request: {}", x.name);
|
||||
}
|
||||
|
||||
Ok(imported_resources)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn export_data(
|
||||
app_handle: AppHandle<Wry>,
|
||||
db_instance: State<'_, Mutex<Pool<Sqlite>>>,
|
||||
root_dir: &str,
|
||||
export_path: &str,
|
||||
workspace_id: &str,
|
||||
) -> Result<(), String> {
|
||||
let path = Path::new(root_dir).join("yaak-export.json");
|
||||
let pool = &*db_instance.lock().await;
|
||||
let imported = models::get_workspace_export_resources(pool, workspace_id).await;
|
||||
println!("Exporting {:?}", path);
|
||||
let f = File::create(path).expect("Unable to create file");
|
||||
serde_json::to_writer_pretty(f, &imported)
|
||||
let export_data = models::get_workspace_export_resources(&app_handle, pool, workspace_id).await;
|
||||
let f = File::options()
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.write(true)
|
||||
.open(export_path)
|
||||
.expect("Unable to create file");
|
||||
serde_json::to_writer_pretty(&f, &export_data)
|
||||
.map_err(|e| e.to_string())
|
||||
.expect("Failed to write");
|
||||
f.sync_all().expect("Failed to sync");
|
||||
info!("Exported Yaak workspace to {:?}", export_path);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -775,37 +833,6 @@ fn main() {
|
||||
|
||||
let _ = models::cancel_pending_responses(&pool).await;
|
||||
|
||||
// TODO: Move this somewhere better
|
||||
match app.get_cli_matches() {
|
||||
Ok(matches) => {
|
||||
let cmd = matches.subcommand.unwrap_or_default();
|
||||
if cmd.name == "import" {
|
||||
let arg_file = cmd
|
||||
.matches
|
||||
.args
|
||||
.get("file")
|
||||
.unwrap()
|
||||
.value
|
||||
.as_str()
|
||||
.unwrap();
|
||||
plugin::run_plugin_import(
|
||||
&app.handle(),
|
||||
&pool,
|
||||
"insomnia-importer",
|
||||
arg_file,
|
||||
)
|
||||
.await;
|
||||
exit(0);
|
||||
} else if cmd.name == "hello" {
|
||||
plugin::run_plugin_hello(&app.handle(), "hello-world");
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Nothing found: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})
|
||||
})
|
||||
|
||||
@@ -6,6 +6,7 @@ use serde::{Deserialize, Serialize};
|
||||
use sqlx::types::chrono::NaiveDateTime;
|
||||
use sqlx::types::{Json, JsonValue};
|
||||
use sqlx::{Pool, Sqlite};
|
||||
use tauri::AppHandle;
|
||||
|
||||
#[derive(sqlx::FromRow, Debug, Clone, Serialize, Deserialize, Default)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
@@ -796,6 +797,16 @@ pub fn generate_id(prefix: Option<&str>) -> String {
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Deserialize, Serialize)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct WorkspaceExport {
|
||||
yaak_version: String,
|
||||
yaak_schema: i64,
|
||||
timestamp: NaiveDateTime,
|
||||
resources: WorkspaceExportResources,
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Deserialize, Serialize)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct WorkspaceExportResources {
|
||||
workspaces: Vec<Workspace>,
|
||||
environments: Vec<Environment>,
|
||||
@@ -803,23 +814,29 @@ pub struct WorkspaceExportResources {
|
||||
requests: Vec<HttpRequest>,
|
||||
}
|
||||
|
||||
pub(crate) async fn get_workspace_export_resources(
|
||||
pub async fn get_workspace_export_resources(
|
||||
app_handle: &AppHandle,
|
||||
pool: &Pool<Sqlite>,
|
||||
workspace_id: &str,
|
||||
) -> WorkspaceExportResources {
|
||||
) -> WorkspaceExport {
|
||||
let workspace = get_workspace(workspace_id, pool)
|
||||
.await
|
||||
.expect("Failed to get workspace");
|
||||
return WorkspaceExportResources {
|
||||
workspaces: vec![workspace],
|
||||
environments: find_environments(workspace_id, pool)
|
||||
.await
|
||||
.expect("Failed to get environments"),
|
||||
folders: find_folders(workspace_id, pool)
|
||||
.await
|
||||
.expect("Failed to get folders"),
|
||||
requests: find_requests(workspace_id, pool)
|
||||
.await
|
||||
.expect("Failed to get requests"),
|
||||
return WorkspaceExport {
|
||||
yaak_version: app_handle.package_info().version.clone().to_string(),
|
||||
yaak_schema: 1,
|
||||
timestamp: chrono::Utc::now().naive_utc(),
|
||||
resources: WorkspaceExportResources {
|
||||
workspaces: vec![workspace],
|
||||
environments: find_environments(workspace_id, pool)
|
||||
.await
|
||||
.expect("Failed to get environments"),
|
||||
folders: find_folders(workspace_id, pool)
|
||||
.await
|
||||
.expect("Failed to get folders"),
|
||||
requests: find_requests(workspace_id, pool)
|
||||
.await
|
||||
.expect("Failed to get requests"),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -8,32 +8,25 @@ use boa_engine::{
|
||||
Context, JsArgs, JsNativeError, JsValue, Module, NativeFunction, Source,
|
||||
};
|
||||
use boa_runtime::Console;
|
||||
use log::info;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
use sqlx::{Pool, Sqlite};
|
||||
use tauri::AppHandle;
|
||||
|
||||
use crate::models::{self, Environment, Folder, HttpRequest, Workspace};
|
||||
|
||||
pub fn run_plugin_hello(app_handle: &AppHandle, plugin_name: &str) {
|
||||
run_plugin(app_handle, plugin_name, "hello", &[]);
|
||||
}
|
||||
use crate::models::{Environment, Folder, HttpRequest, Workspace};
|
||||
|
||||
#[derive(Default, Debug, Deserialize, Serialize)]
|
||||
pub struct ImportedResources {
|
||||
workspaces: Vec<Workspace>,
|
||||
environments: Vec<Environment>,
|
||||
folders: Vec<Folder>,
|
||||
requests: Vec<HttpRequest>,
|
||||
pub struct ImportResources {
|
||||
pub workspaces: Vec<Workspace>,
|
||||
pub environments: Vec<Environment>,
|
||||
pub folders: Vec<Folder>,
|
||||
pub requests: Vec<HttpRequest>,
|
||||
}
|
||||
|
||||
pub async fn run_plugin_import(
|
||||
app_handle: &AppHandle,
|
||||
pool: &Pool<Sqlite>,
|
||||
plugin_name: &str,
|
||||
file_path: &str,
|
||||
) -> ImportedResources {
|
||||
) -> Option<ImportResources> {
|
||||
let file = fs::read_to_string(file_path)
|
||||
.unwrap_or_else(|_| panic!("Unable to read file {}", file_path));
|
||||
let file_contents = file.as_str();
|
||||
@@ -43,44 +36,14 @@ pub async fn run_plugin_import(
|
||||
"pluginHookImport",
|
||||
&[js_string!(file_contents).into()],
|
||||
);
|
||||
let resources: ImportedResources =
|
||||
|
||||
if result_json.is_null() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let resources: ImportResources =
|
||||
serde_json::from_value(result_json).expect("failed to parse result json");
|
||||
let mut imported_resources = ImportedResources::default();
|
||||
|
||||
info!("Importing resources");
|
||||
for w in resources.workspaces {
|
||||
let x = models::upsert_workspace(pool, w)
|
||||
.await
|
||||
.expect("Failed to create workspace");
|
||||
imported_resources.workspaces.push(x.clone());
|
||||
info!("Imported workspace: {}", x.name);
|
||||
}
|
||||
|
||||
for e in resources.environments {
|
||||
let x = models::upsert_environment(pool, e)
|
||||
.await
|
||||
.expect("Failed to create environment");
|
||||
imported_resources.environments.push(x.clone());
|
||||
info!("Imported environment: {}", x.name);
|
||||
}
|
||||
|
||||
for f in resources.folders {
|
||||
let x = models::upsert_folder(pool, f)
|
||||
.await
|
||||
.expect("Failed to create folder");
|
||||
imported_resources.folders.push(x.clone());
|
||||
info!("Imported folder: {}", x.name);
|
||||
}
|
||||
|
||||
for r in resources.requests {
|
||||
let x = models::upsert_request(pool, r)
|
||||
.await
|
||||
.expect("Failed to create request");
|
||||
imported_resources.requests.push(x.clone());
|
||||
info!("Imported request: {}", x.name);
|
||||
}
|
||||
|
||||
imported_resources
|
||||
Some(resources)
|
||||
}
|
||||
|
||||
fn run_plugin(
|
||||
|
||||
Reference in New Issue
Block a user