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

View File

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

View File

@ -139,6 +139,6 @@ mod test {
};
// 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::hash::{Hash, Hasher};
use chrono::Utc;
use atuin_common::utils::uuid_v4;
// 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 id: String,
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)]
mod test {
use chrono::{TimeZone, Utc};
use std::io::Cursor;
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]
fn parse_complex() {
@ -201,21 +184,24 @@ ERROR
- ~/.local/share/fish/fish_history
"#;
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();
assert_eq!(
history,
vec![
fishtory!(1639162832, "history --help"),
fishtory!(1639162851, "cat ~/.bash_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"),
]
);
// simple wrapper for fish history entry
macro_rules! fishtory {
($timestamp:expr, $command:expr) => {
let h = fish.next().expect("missing entry in history").unwrap();
assert_eq!(h.command.as_str(), $command);
assert_eq!(h.timestamp.timestamp(), $timestamp);
};
}
fishtory!(1639162832, "history --help");
fishtory!(1639162851, "cat ~/.bash_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 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::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));
if most_common_command.is_none() {
return Err(eyre!("No commands found"));
bail!("No commands found");
}
let table = vec![

View File

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