Skip to content

Commit

Permalink
Added collection loading and saving
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-cpsn committed Feb 22, 2024
1 parent 5fc9f21 commit 24450c6
Show file tree
Hide file tree
Showing 28 changed files with 575 additions and 385 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target

atac.log
atac.log
collection.json
110 changes: 109 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ throbber-widgets-tui = "0.4.0"
regex = "1.10.3"
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.114"
clap = { version = "4.5.0", features = ["derive", "color"] }
lazy_static = "1.4.0"
15 changes: 8 additions & 7 deletions src/app/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use ratatui::backend::Backend;
use ratatui::Terminal;
use tui_textarea::TextArea;
use crate::app::app_states::AppState;
use crate::app::request_ui::param_tabs::RequestParamsTabs;
use crate::app::request_ui::result_tabs::RequestResultTabs;
use crate::app::request_ui::views::RequestView;
use crate::app::ui::param_tabs::RequestParamsTabs;
use crate::app::ui::result_tabs::RequestResultTabs;
use crate::app::ui::views::RequestView;
use crate::app::startup::args::ARGS;
use crate::request::request::{Request};
use crate::utils::stateful_list::StatefulList;
use crate::utils::stateful_scrollbar::StatefulScrollbar;
Expand Down Expand Up @@ -42,7 +43,7 @@ pub struct App<'a> {
}

impl App<'_> {
pub fn new<'a>(requests: Vec<Request>) -> App<'a> {
pub fn new<'a>() -> App<'a> {
App {
should_quit: false,
state: AppState::Normal,
Expand All @@ -51,12 +52,12 @@ impl App<'_> {
.write(true)
.create(true)
.truncate(true)
.open("atac.log")
.expect("Could not open file"),
.open(&ARGS.log_file)
.expect(&format!("Could not open log file \"{}\"", ARGS.log_file)),

collection: StatefulList {
state: Default::default(),
items: requests,
items: vec![],
selected: None,
last_selected: None,
},
Expand Down
2 changes: 1 addition & 1 deletion src/app/app_logic/change_app_state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::app::app::App;
use crate::app::app_states::AppState;
use crate::app::request_ui::param_tabs::RequestParamsTabs;
use crate::app::ui::param_tabs::RequestParamsTabs;
use crate::request::body::ContentType;

impl App<'_> {
Expand Down
2 changes: 1 addition & 1 deletion src/app/app_logic/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::app::app_states::AppState;
use crate::request::auth::Auth;
use crate::request::request::{Request};

impl<'a> App<'a> {
impl App<'_> {
pub fn reset_inputs(&mut self) {
self.url_text_input.reset_input();
self.request_param_table.param_selection_text_input.reset_input();
Expand Down
13 changes: 10 additions & 3 deletions src/app/app_logic/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ impl App<'_> {

self.collection.items[selected_request_index].url = final_url;

self.save_collection_to_file();
self.select_request_state();
}

Expand All @@ -83,6 +84,8 @@ impl App<'_> {
let next_method = next_method(&self.collection.items[selected_request_index].method);

self.collection.items[selected_request_index].method = next_method;

self.save_collection_to_file();
}

/* PARAMS */
Expand Down Expand Up @@ -117,6 +120,7 @@ impl App<'_> {

selected_request.params[row].enabled = !selected_request.params[row].enabled;

self.save_collection_to_file();
self.update_inputs();
}

Expand All @@ -132,6 +136,7 @@ impl App<'_> {
(_, _) => {}
};

self.save_collection_to_file();
self.select_request_state();
}

Expand All @@ -143,6 +148,7 @@ impl App<'_> {

self.collection.items[selected_request_index].auth = next_auth(&selected_request.auth);

self.save_collection_to_file();
self.load_request_auth_param_tab();
}

Expand Down Expand Up @@ -177,6 +183,7 @@ impl App<'_> {
_ => {}
}

self.save_collection_to_file();
self.select_request_state();
}

Expand All @@ -193,7 +200,7 @@ impl App<'_> {
_ => {}
}

self.update_inputs();
self.save_collection_to_file();
self.select_request_state();
}

Expand All @@ -210,7 +217,7 @@ impl App<'_> {
_ => {}
}

self.update_inputs();
self.save_collection_to_file();
self.select_request_state();
}

Expand Down Expand Up @@ -241,7 +248,7 @@ impl App<'_> {

self.collection.items[selected_request_index].body = new_body;

self.update_inputs();
self.save_collection_to_file();
self.select_request_state();
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/app_states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ratatui::text::Line;
use strum::Display;
use crate::app::app::App;
use crate::app::app_states::AppState::*;
use crate::app::request_ui::param_tabs::RequestParamsTabs;
use crate::app::ui::param_tabs::RequestParamsTabs;
use crate::request::auth::Auth;
use crate::request::body::ContentType;

Expand Down
13 changes: 3 additions & 10 deletions src/app/events.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crossterm::event;
use crossterm::event::{Event, KeyCode, KeyEventKind, KeyModifiers};
use std::io::{Result, Write};
use std::io::{Result};
use tui_textarea::CursorMove;
use crate::app::app::{App};
use crate::app::app_states::AppState;
use crate::app::request_ui::param_tabs::RequestParamsTabs;
use crate::app::ui::param_tabs::RequestParamsTabs;

impl App<'_> {
/// Handle events
Expand Down Expand Up @@ -221,14 +221,7 @@ impl App<'_> {
};

if !miss_input {
self.log_file
.write_fmt(format_args!(
"{:25}{:25}{:40}\n",
format!("{:?}", key.modifiers),
format!("{:?}", key.code),
previous_app_state.to_string(),
))
.expect("Could not write to log file");
self.write_to_log_file(format!("{:?}", key.modifiers), format!("{:?}", key.code), previous_app_state.to_string());
}
}
}
Expand Down
56 changes: 56 additions & 0 deletions src/app/files/collection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::fs;
use std::fs::OpenOptions;
use std::io::{Read, Write};
use crate::app::app::App;
use crate::app::startup::args::ARGS;
use crate::request::request::Request;

impl App<'_> {
/// Set the app request to the requests found in the collection file
pub fn set_collection_from_file(&mut self) {
println!("Parsing collection file...");

let mut file_content= String::new();

let mut collection_file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(&ARGS.collection_file)
.expect(&format!("Could not open collection file \"{}\"", ARGS.collection_file));

collection_file.read_to_string(&mut file_content).expect(&format!("Could not read collection file \"{}\"", ARGS.collection_file));

let requests: Vec<Request>;

if file_content.len() == 0 {
requests = vec![];
}
else {
requests = serde_json::from_str(&file_content).expect("Could not parse collection");
}

self.collection.items = requests;

println!("Collection file parsed!");
}

/// Save app collection in the collection file through a temporary file
pub fn save_collection_to_file(&mut self) {
let temps_file_path = format!("{}_", ARGS.collection_file);

let mut temp_file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(&temps_file_path)
.expect("Could not open temp file");

let collection_json = serde_json::to_string_pretty(&self.collection.items).expect("Could not serialize collection");

temp_file.write_all(collection_json.as_bytes()).expect("Could not write to temp file");
temp_file.flush().unwrap();

fs::rename(temps_file_path, &ARGS.collection_file).expect("Could not move temp file to collection file");
}
}
Loading

0 comments on commit 24450c6

Please sign in to comment.