A few minor tweaks (#314)

* use bail macro

replace client database errors

remove dead code

* fix test
This commit is contained in:
Conrad Ludgate 2022-04-21 08:05:57 +01:00 committed by GitHub
parent ed4e07d2e6
commit 48747e3b7c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 30 additions and 65 deletions

View file

@ -1,7 +1,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use chrono::Utc; use chrono::Utc;
use eyre::{eyre, Result}; use eyre::{bail, Result};
use reqwest::header::{HeaderMap, AUTHORIZATION, USER_AGENT}; use reqwest::header::{HeaderMap, AUTHORIZATION, USER_AGENT};
use reqwest::{StatusCode, Url}; use reqwest::{StatusCode, Url};
use sodiumoxide::crypto::secretbox; use sodiumoxide::crypto::secretbox;
@ -41,7 +41,7 @@ pub async fn register(
let resp = reqwest::blocking::get(url)?; let resp = reqwest::blocking::get(url)?;
if resp.status().is_success() { if resp.status().is_success() {
return Err(eyre!("username already in use")); bail!("username already in use");
} }
let url = format!("{}/register", address); let url = format!("{}/register", address);
@ -54,7 +54,7 @@ pub async fn register(
.await?; .await?;
if !resp.status().is_success() { if !resp.status().is_success() {
return Err(eyre!("failed to register user")); bail!("failed to register user");
} }
let session = resp.json::<RegisterResponse>().await?; let session = resp.json::<RegisterResponse>().await?;
@ -73,7 +73,7 @@ pub async fn login(address: &str, req: LoginRequest) -> Result<LoginResponse> {
.await?; .await?;
if resp.status() != reqwest::StatusCode::OK { if resp.status() != reqwest::StatusCode::OK {
return Err(eyre!("invalid login details")); bail!("invalid login details");
} }
let session = resp.json::<LoginResponse>().await?; let session = resp.json::<LoginResponse>().await?;
@ -102,7 +102,7 @@ impl<'a> Client<'a> {
let resp = self.client.get(url).send().await?; let resp = self.client.get(url).send().await?;
if resp.status() != StatusCode::OK { if resp.status() != StatusCode::OK {
return Err(eyre!("failed to get count (are you logged in?)")); bail!("failed to get count (are you logged in?)");
} }
let count = resp.json::<CountResponse>().await?; let count = resp.json::<CountResponse>().await?;

View file

@ -5,15 +5,14 @@ use async_trait::async_trait;
use chrono::prelude::*; use chrono::prelude::*;
use chrono::Utc; use chrono::Utc;
use eyre::Result;
use itertools::Itertools; use itertools::Itertools;
use regex::Regex; use regex::Regex;
use fs_err as fs; use fs_err as fs;
use sqlx::sqlite::{ use sqlx::{
SqliteConnectOptions, SqliteJournalMode, SqlitePool, SqlitePoolOptions, SqliteRow, sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePool, SqlitePoolOptions, SqliteRow},
Result, Row,
}; };
use sqlx::Row;
use super::history::History; use super::history::History;
use super::ordering; use super::ordering;

View file

@ -139,6 +139,6 @@ mod test {
}; };
// this should err // this should err
decrypt(&e2, &key1).expect_err("expected an error decrypting with invalid key"); let _ = decrypt(&e2, &key1).expect_err("expected an error decrypting with invalid key");
} }
} }

View file

@ -1,12 +1,11 @@
use std::env; use std::env;
use std::hash::{Hash, Hasher};
use chrono::Utc; use chrono::Utc;
use atuin_common::utils::uuid_v4; use atuin_common::utils::uuid_v4;
// Any new fields MUST be Optional<>! // Any new fields MUST be Optional<>!
#[derive(Debug, Clone, Serialize, Deserialize, Ord, PartialOrd, sqlx::FromRow)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, sqlx::FromRow)]
pub struct History { pub struct History {
pub id: String, pub id: String,
pub timestamp: chrono::DateTime<Utc>, pub timestamp: chrono::DateTime<Utc>,
@ -46,21 +45,3 @@ impl History {
} }
} }
} }
impl PartialEq for History {
// for the sakes of listing unique history only, we do not care about
// anything else
// obviously this does not refer to the *same* item of history, but when
// we only render the command, it looks the same
fn eq(&self, other: &Self) -> bool {
self.command == other.command
}
}
impl Eq for History {}
impl Hash for History {
fn hash<H: Hasher>(&self, state: &mut H) {
self.command.hash(state);
}
}

View file

@ -144,26 +144,9 @@ impl<R: Read> Iterator for Fish<R> {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use chrono::{TimeZone, Utc};
use std::io::Cursor; use std::io::Cursor;
use super::Fish; use super::Fish;
use crate::history::History;
// simple wrapper for fish history entry
macro_rules! fishtory {
($timestamp:literal, $command:literal) => {
History::new(
Utc.timestamp($timestamp, 0),
$command.into(),
"unknown".into(),
-1,
-1,
None,
None,
)
};
}
#[test] #[test]
fn parse_complex() { fn parse_complex() {
@ -201,21 +184,24 @@ ERROR
- ~/.local/share/fish/fish_history - ~/.local/share/fish/fish_history
"#; "#;
let cursor = Cursor::new(input); let cursor = Cursor::new(input);
let fish = Fish::new(cursor).unwrap(); let mut fish = Fish::new(cursor).unwrap();
let history = fish.collect::<Result<Vec<_>, _>>().unwrap(); // simple wrapper for fish history entry
assert_eq!( macro_rules! fishtory {
history, ($timestamp:expr, $command:expr) => {
vec![ let h = fish.next().expect("missing entry in history").unwrap();
fishtory!(1639162832, "history --help"), assert_eq!(h.command.as_str(), $command);
fishtory!(1639162851, "cat ~/.bash_history"), assert_eq!(h.timestamp.timestamp(), $timestamp);
fishtory!(1639162890, "ls ~/.local/share/fish/fish_history"), };
fishtory!(1639162893, "cat ~/.local/share/fish/fish_history"), }
fishtory!(1639162933, "echo \"foo\" \\\n'bar' baz"),
fishtory!(1639162939, "cat ~/.local/share/fish/fish_history"), fishtory!(1639162832, "history --help");
fishtory!(1639163063, r#"echo "\"" \\ "\\""#), fishtory!(1639162851, "cat ~/.bash_history");
fishtory!(1639163066, "cat ~/.local/share/fish/fish_history"), fishtory!(1639162890, "ls ~/.local/share/fish/fish_history");
] fishtory!(1639162893, "cat ~/.local/share/fish/fish_history");
); fishtory!(1639162933, "echo \"foo\" \\\n'bar' baz");
fishtory!(1639162939, "cat ~/.local/share/fish/fish_history");
fishtory!(1639163063, r#"echo "\"" \\ "\\""#);
fishtory!(1639163066, "cat ~/.local/share/fish/fish_history");
} }
} }

View file

@ -6,7 +6,7 @@ use chrono_english::parse_date_string;
use clap::Parser; use clap::Parser;
use cli_table::{format::Justify, print_stdout, Cell, Style, Table}; use cli_table::{format::Justify, print_stdout, Cell, Style, Table};
use eyre::{eyre, Result}; use eyre::{bail, Result};
use atuin_client::database::Database; use atuin_client::database::Database;
use atuin_client::history::History; use atuin_client::history::History;
@ -32,7 +32,7 @@ fn compute_stats(history: &[History]) -> Result<()> {
let most_common_command = commands.iter().max_by(|a, b| a.1.cmp(b.1)); let most_common_command = commands.iter().max_by(|a, b| a.1.cmp(b.1));
if most_common_command.is_none() { if most_common_command.is_none() {
return Err(eyre!("No commands found")); bail!("No commands found");
} }
let table = vec![ let table = vec![

View file

@ -9,7 +9,6 @@ use eyre::Result;
extern crate log; extern crate log;
use command::AtuinCmd; use command::AtuinCmd;
mod command; mod command;
const VERSION: &str = env!("CARGO_PKG_VERSION"); const VERSION: &str = env!("CARGO_PKG_VERSION");