Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Dev #52

Merged
merged 2 commits into from
Oct 17, 2023
Merged

Dev #52

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
345 changes: 138 additions & 207 deletions src/app.rs

Large diffs are not rendered by default.

83 changes: 38 additions & 45 deletions src/database/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ pub struct SavedKey {
key: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SavedUrl {
id: i32,
url: String,
}

#[derive(Debug)]
pub struct DB {
pub conn: Connection,
Expand All @@ -36,7 +30,7 @@ impl DB {
let dbpath = dir.join("CuTE.db");

let conn = Connection::open_with_flags(
&dbpath,
dbpath,
OpenFlags::SQLITE_OPEN_READ_WRITE
| OpenFlags::SQLITE_OPEN_CREATE
| OpenFlags::SQLITE_OPEN_URI
Expand All @@ -62,19 +56,16 @@ impl DB {
}

pub fn add_command(&self, command: &str, json_str: String) -> Result<(), rusqlite::Error> {
if self.command_exists(command).unwrap() {
return Ok(());
}
self.conn.execute(
"INSERT INTO commands (command, curl_json) VALUES (?1, ?2)",
params![command, &json_str],
)?;
let mut stmt = self
.conn
.prepare("INSERT INTO commands (command, curl_json) VALUES (?1, ?2)")?;
let _ = stmt.execute(params![command, &json_str])?;
Ok(())
}

pub fn delete_command(&self, command: &str) -> Result<(), rusqlite::Error> {
self.conn
.execute("DELETE FROM commands WHERE command = ?1", params![command])?;
pub fn delete_command(&self, id: i32) -> Result<(), rusqlite::Error> {
let mut stmt = self.conn.prepare("DELETE FROM commands WHERE id = ?")?;
stmt.execute(params![id])?;
Ok(())
}

Expand All @@ -91,11 +82,7 @@ impl DB {
.conn
.prepare("SELECT COUNT(*) FROM commands WHERE command = ?")?;
let count: i64 = stmt.query_row([&command], |row| row.get(0))?;
if count.is_positive() {
Ok(true)
} else {
Ok(false)
}
Ok(count > 0)
}

pub fn get_commands(&self) -> Result<Vec<SavedCommand>> {
Expand All @@ -116,18 +103,18 @@ impl DB {
Ok(commands)
}

pub fn delete_key(&self, key: &str) -> Result<()> {
self.conn
.execute("DELETE FROM keys WHERE key = ?1", params![key])?;
pub fn delete_key(&self, id: i32) -> Result<()> {
let mut stmt = self.conn.prepare("DELETE FROM keys WHERE id = ?1")?;
stmt.execute(params![id])?;
Ok(())
}

pub fn add_key(&self, key: &str) -> Result<()> {
if !self.key_exists(key).unwrap() {
if self.key_exists(key).unwrap() {
return Ok(());
}
self.conn
.execute("INSERT INTO keys (key) VALUES (?1)", params![key])?;
let mut stmt = self.conn.prepare("INSERT INTO keys (key) VALUES (?1)")?;
let _ = stmt.execute(params![key])?;
Ok(())
}

Expand All @@ -147,24 +134,12 @@ impl DB {
}
}

impl Display for SavedUrl {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.url)
}
}

impl Display for SavedCommand {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.command)
}
}

impl Display for SavedKey {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, " ID: {} | Key: {}", self.id, self.key)
}
}

// TODO: we need to be getting the api key from the command and offering
// to store it separately + link the two. (also encrypt the key?)
// do we use OS keyring or maybe an ENV VAR?
Expand All @@ -175,16 +150,26 @@ impl SavedCommand {
Ok(serde_json::to_string(&self).expect("Failed to serialize"))
}

pub fn get_id(&self) -> i32 {
self.id
}

pub fn from_json(json: &str) -> Result<Self> {
Ok(serde_json::from_str(json).expect("Failed to deserialize"))
}

pub fn get_curl_json(&self) -> String {
self.curl_json.clone()
pub fn get_curl_json(&self) -> &str {
&self.curl_json
}

pub fn get_command(&self) -> String {
self.command.clone()
pub fn get_command(&self) -> &str {
&self.command
}
}

impl Display for SavedKey {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, " ID: {} | Key: {}", self.id, self.key)
}
}

Expand All @@ -195,17 +180,25 @@ impl SavedKey {
key: key.to_string(),
}
}

pub fn get_id(&self) -> i32 {
self.id
}

pub fn is_key(&self, key: &str) -> bool {
self.key == key
}

pub fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(&self).expect("Failed to serialize"))
}

pub fn from_json(json: &str) -> Result<Self> {
Ok(serde_json::from_str(json).expect("Failed to deserialize"))
}
//TODO: implement encryption

//TODO: implement actual encryption
//
// pub fn encrypt(&self, key: &str) -> Result<String> {
// let mut encrypted = encrypt(key, self.key.as_str())?;
// encrypted.push_str("\n");
Expand Down
2 changes: 1 addition & 1 deletion src/display/inputopt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt::Display;

use crate::request::cmdtype::CmdType;
use crate::request::command::CmdType;
use crate::screens::auth::AuthType;

#[derive(Debug, Clone, PartialEq)]
Expand Down
8 changes: 8 additions & 0 deletions src/display/menuopts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ pub const DISPLAY_OPT_USERAGENT: &str = " Specify User-Agent: ";
pub const DISPLAY_OPT_PROXY_TUNNEL: &str = " Enable HTTP Proxy-Tunnel 󱠾 ";
pub const DISPLAY_OPT_URL: &str = " Request URL: ";
lazy_static! {
pub static ref ALERT_MENU_OPTIONS_CMD: [&'static str; 4] = [
"Execute  ",
"Delete  ",
"Copy to Clipboard 󰅎 ",
"Cancel  ",
];
pub static ref ALERT_MENU_OPTIONS_KEY: [&'static str; 3] =
["Delete", "Copy to Clipboard", "Cancel"];
pub static ref MAIN_MENU_OPTIONS: [&'static str; 4] = [
"Build and send an HTTP request 󰖟 ",
"Download a remote file or directory 󰧩 ",
Expand Down
5 changes: 1 addition & 4 deletions src/events/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
}
KeyCode::Esc => {
app.go_back_screen(); // Escape Should Bring You Back
if app.input.value().len() > 0 {
if !app.input.value().is_empty() {
app.input.reset(); // If we leave the page, we should clear the input buffer
}
}
Expand Down Expand Up @@ -54,9 +54,6 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
KeyCode::Char('b') => {
app.go_back_screen();
}
KeyCode::Char('x') => {
app.delete_item(app.cursor);
}
_ => {}
}
}
Expand Down
16 changes: 0 additions & 16 deletions src/request/cmdtype.rs

This file was deleted.

Loading