Skip to content

Commit

Permalink
Refactor file path handling and add error handling for file open
Browse files Browse the repository at this point in the history
  • Loading branch information
rushi3691 committed Mar 24, 2024
1 parent b261631 commit 13a3929
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
17 changes: 17 additions & 0 deletions src-tauri/src/creds_window.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use tauri::AppHandle;

pub fn open_window(app_handle: &AppHandle) {
let creds_window = tauri::WindowBuilder::new(
app_handle,
"addcreds", /* the unique window label */
tauri::WindowUrl::App("index.html".into()),
)
.title("fortinet-connect")
.center()
.build()
.ok();

if let Some(creds_window) = creds_window {
creds_window.show().ok();
}
}
14 changes: 10 additions & 4 deletions src-tauri/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use tauri::Manager;

use crate::{worker::worker, Credentials};



#[tauri::command]
pub async fn store_credentials(
app_handle: tauri::AppHandle,
Expand All @@ -16,8 +14,16 @@ pub async fn store_credentials(
println!("{}", s);

// write to file
let binding = app_handle.path_resolver().app_data_dir().unwrap();
let app_data_dir = binding.to_str().unwrap();
let app_data_path = app_handle.path_resolver().app_data_dir().unwrap();
// print if path does not exist
let exists = app_data_path.exists();
if !exists {
println!("Path does not exist: {:?}", app_data_path);
// create it
std::fs::create_dir_all(&app_data_path).unwrap();
}

let app_data_dir = app_data_path.to_str().unwrap();
// println!("App data dir: {:?}", app_data_dir);
let file_path = format!("{}/creds.txt", app_data_dir);
let mut file = File::create(file_path).unwrap();
Expand Down
27 changes: 11 additions & 16 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

mod creds_window;
mod fortinet;
mod handler;
mod setup;
Expand Down Expand Up @@ -29,9 +30,14 @@ fn main() {
.setup(|app| {
let state: tauri::State<Credentials> = app.state();

let path = app.handle().path_resolver().app_data_dir().unwrap();
println!("Path: {:?}", path);
let file_path = format!("{}/creds.txt", path.to_str().unwrap());
let app_data_path_buf = app.path_resolver().app_data_dir().unwrap();

println!("Path: {:?}", app_data_path_buf);
println!("Logging path: {:?}", app.path_resolver().app_log_dir().unwrap());
// std::fs::create_dir_all(&app_data_path_buf).unwrap();
let app_data_path = app_data_path_buf.to_str().unwrap();

let file_path = format!("{}/creds.txt", app_data_path);
let file = File::open(file_path);
match file {
Ok(mut file) => {
Expand All @@ -53,6 +59,7 @@ fn main() {
}
Err(e) => {
println!("Error: {:?}", e);
creds_window::open_window(&app.app_handle());
}
}

Expand All @@ -65,19 +72,7 @@ fn main() {
std::process::exit(0);
}
"add" => {
let creds_window = tauri::WindowBuilder::new(
app_handle,
"addcreds", /* the unique window label */
tauri::WindowUrl::App("index.html".into()),
)
.title("fortinet-connect")
.center()
.build()
.ok();

if let Some(creds_window) = creds_window {
creds_window.show().ok();
}
creds_window::open_window(app_handle);
}

_ => {}
Expand Down

0 comments on commit 13a3929

Please sign in to comment.