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

Commit

Permalink
Merge pull request #97 from PThorpe92/p_89
Browse files Browse the repository at this point in the history
feat!: add labels to API keys. breaking changes for v0.1.0
  • Loading branch information
PThorpe92 authored Nov 11, 2023
2 parents 0523ddf + 4d8a585 commit fb1cdd1
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 30 deletions.
14 changes: 9 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ impl<'a> App<'a> {
.unwrap();
command.easy_from_opts();
match command.execute(None) {
Ok(_) => self.set_response(command.get_response().clone()),
Err(e) => self.set_response(e.to_string()),
Ok(_) => self.set_response(&command.get_response()),
Err(e) => self.set_response(&e),
};
self.goto_screen(Screen::Response(self.response.clone().unwrap()));
}
Expand All @@ -227,6 +227,10 @@ impl<'a> App<'a> {
}
}

pub fn set_key_label(&self, key: i32, label: &str) -> Result<(), String> {
self.db.set_key_label(key, label).map_err(|e| e.to_string())
}

pub fn copy_to_clipboard(&self, opt: &str) -> Result<(), String> {
if let Ok(mut clipboard) = Clipboard::new() {
if let Err(e) = clipboard.set_text(opt) {
Expand Down Expand Up @@ -342,10 +346,10 @@ impl<'a> App<'a> {
}
}

pub fn set_response(&mut self, response: String) {
self.response = Some(response.clone());
pub fn set_response(&mut self, response: &str) {
self.response = Some(response.to_string());
if self.command.is_some() {
self.command.as_mut().unwrap().set_response(&response);
self.command.as_mut().unwrap().set_response(response);
}
}

Expand Down
28 changes: 24 additions & 4 deletions src/database/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct SavedCommand {
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SavedKey {
id: i32,
label: Option<String>,
key: String,
}

Expand Down Expand Up @@ -53,7 +54,6 @@ impl DB {
| OpenFlags::SQLITE_OPEN_NO_MUTEX,
);

// We Need To Handle Some Errors Here Related To Opening SQLite3 Database Files
let conn = match conn_result {
Ok(connection) => connection,
Err(e) => {
Expand All @@ -71,7 +71,7 @@ impl DB {
)?;

conn.execute(
"CREATE TABLE IF NOT EXISTS keys (id INTEGER PRIMARY KEY, key TEXT);",
"CREATE TABLE IF NOT EXISTS keys (id INTEGER PRIMARY KEY, key TEXT, label TEXT);",
params![],
)?;

Expand Down Expand Up @@ -148,12 +148,21 @@ impl DB {
Ok(())
}

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

pub fn get_keys(&self) -> Result<Vec<SavedKey>> {
let mut stmt = self.conn.prepare("SELECT id, key FROM keys")?;
let mut stmt = self.conn.prepare("SELECT id, key, label FROM keys")?;
let rows = stmt.query_map(params![], |row| {
Ok(SavedKey {
id: row.get(0)?,
key: row.get(1)?,
label: row.get(2)?,
})
})?;
let mut keys = Vec::new();
Expand Down Expand Up @@ -196,7 +205,11 @@ impl SavedCommand {

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

Expand All @@ -205,9 +218,16 @@ impl SavedKey {
SavedKey {
id: 0,
key: key.to_string(),
label: None,
}
}

pub fn get_label(&self) -> &str {
match &self.label {
Some(label) => label,
None => "",
}
}
pub fn get_id(&self) -> i32 {
self.id
}
Expand Down
2 changes: 2 additions & 0 deletions src/display/inputopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub enum InputOpt {
FtpAccount,
CaPath,
CaCert,
KeyLabel(i32),
}

impl Display for InputOpt {
Expand All @@ -48,6 +49,7 @@ impl Display for InputOpt {
InputOpt::CaCert => write!(f, "| Ca Cert"),
InputOpt::VerifyPeer => write!(f, "| Verify Peer DNS-Over-HTTPS"),
InputOpt::FtpAccount => write!(f, "| FTP Account"),
InputOpt::KeyLabel(_) => write!(f, "| Key Label"),
}
}
}
2 changes: 1 addition & 1 deletion src/display/menuopts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ lazy_static! {
"Cancel  ",
];
pub static ref KEY_MENU_OPTIONS: [&'static str; 4] = [
"Add a new key  ",
"Add a Label  ",
"Delete  ",
"Copy to Clipboard 󰅎 ",
"Cancel  ",
Expand Down
10 changes: 0 additions & 10 deletions src/events/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
app.input.reset(); // If we leave the page, we should clear the input buffer
}
}
// Other handlers you could add here.
KeyCode::Up => {
app.move_cursor_up();
}
Expand Down Expand Up @@ -85,15 +84,6 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {

InputMode::Editing => match key_event.kind {
KeyEventKind::Press => match key_event.code {
KeyCode::Char('v') | KeyCode::Char('V') => {
if key_event.modifiers == KeyModifiers::CONTROL {
let mut new_str = app.input.value().to_string();
let value = app.get_from_clipboard();
new_str.push_str(&value);
app.input.reset();
app.input = tui_input::Input::from(new_str);
}
}
KeyCode::Enter => {
app.messages.push(app.input.value().to_string());
app.input.reset();
Expand Down
2 changes: 1 addition & 1 deletion src/request/curl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ mod tests {
"url": "https://example.com",
"opts": [],
"resp": "This is a response",
"upload_file": "file.txt",
"upload_file": "",
"outfile": "output.txt",
}
);
Expand Down
4 changes: 4 additions & 0 deletions src/screens/input/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ pub fn parse_input(message: String, opt: InputOpt, app: &mut App) {
app.goto_screen(Screen::Downloads(String::from(PARSE_INT_ERROR)));
}
}
InputOpt::KeyLabel(id) => match app.set_key_label(id, &message) {
Ok(_) => app.goto_screen(Screen::SavedKeys),
Err(e) => app.goto_screen(Screen::Error(e)),
},
InputOpt::Auth(auth) => {
parse_auth(auth, app, &message);
}
Expand Down
10 changes: 5 additions & 5 deletions src/screens/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,16 @@ pub fn handle_screen_defaults<B: Backend>(app: &mut App, frame: &mut Frame<'_, B
Screen::SavedKeys => {
items = Some(
app.get_saved_keys()
.unwrap()
.unwrap_or_default()
.into_iter()
.map(|x| format!("{:?}", x))
.map(|x| x.to_string())
.collect::<Vec<String>>(),
);
}
Screen::SavedCommands => {
items = Some(
app.get_saved_commands()
.unwrap()
.unwrap_or_default()
.into_iter()
.map(|x| format!("{:?}", x))
.collect::<Vec<String>>(),
Expand Down Expand Up @@ -148,7 +148,7 @@ pub fn handle_screen<B: Backend>(app: &mut App, frame: &mut Frame<'_, B>, screen
Screen::Method => handle_method_select_screen(app, frame),
Screen::ViewBody => {
let area = default_rect(frame.size());
let response = app.response.clone().unwrap();
let response = app.response.clone().unwrap_or_default();
let paragraph = Paragraph::new(Text::from(response.as_str()))
.style(app.config.get_style())
.alignment(Alignment::Center);
Expand Down Expand Up @@ -182,7 +182,7 @@ pub fn handle_screen<B: Backend>(app: &mut App, frame: &mut Frame<'_, B>, screen
}
// RESPONSE SCREEN ******************************************************
Screen::Response(resp) => {
app.set_response(resp.clone());
app.set_response(&resp);
handle_response_screen(app, frame, resp.to_string());
}
Screen::SavedCommands => {
Expand Down
2 changes: 1 addition & 1 deletion src/screens/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn handle_request_menu_screen<B: Backend>(
match app.execute_command() {
Ok(()) => {
let response = app.command.as_mut().unwrap().get_response();
app.response = Some(response.clone());
app.set_response(&response);
app.goto_screen(Screen::Response(response));
}
Err(e) => {
Expand Down
4 changes: 2 additions & 2 deletions src/screens/saved_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ pub fn handle_key_menu<B: Backend>(app: &mut App, frame: &mut Frame<'_, B>, cmd:
frame.render_widget(alert_text_chunk, alert_box);
frame.render_stateful_widget(list, options_box, &mut list_state);
match app.selected {
// Add a new key
// Add/Edit Label
Some(0) => {
app.goto_screen(Screen::InputMenu(
crate::display::inputopt::InputOpt::ApiKey,
crate::display::inputopt::InputOpt::KeyLabel(selected.get_id()),
));
}
// delete item
Expand Down
2 changes: 1 addition & 1 deletion src/screens/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl<'a> Screen {
len = items.as_ref().unwrap().len();
}
return items
.unwrap_or(vec!["No Saved Commands".to_string()])
.unwrap_or(vec!["No Saved Keys".to_string()])
.iter()
.map(|c| ListItem::new(format!("{}{}", c, determine_line_size(len))))
.collect();
Expand Down

0 comments on commit fb1cdd1

Please sign in to comment.