2021-02-14 08:15:26 -07:00
|
|
|
#![warn(clippy::pedantic, clippy::nursery)]
|
2021-03-10 14:24:08 -07:00
|
|
|
#![allow(clippy::use_self)] // not 100% reliable
|
2021-02-14 06:28:01 -07:00
|
|
|
|
2020-10-05 10:20:48 -06:00
|
|
|
use std::path::PathBuf;
|
2020-10-04 17:59:28 -06:00
|
|
|
|
2020-10-05 10:20:48 -06:00
|
|
|
use eyre::{eyre, Result};
|
2021-04-20 10:07:11 -06:00
|
|
|
use fern::colors::{Color, ColoredLevelConfig};
|
2021-04-14 11:40:50 -06:00
|
|
|
use human_panic::setup_panic;
|
2021-04-13 12:14:07 -06:00
|
|
|
use structopt::{clap::AppSettings, StructOpt};
|
2020-10-04 17:59:28 -06:00
|
|
|
|
2020-10-05 10:20:48 -06:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
2020-10-04 17:59:28 -06:00
|
|
|
|
2021-03-10 14:24:08 -07:00
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
|
|
|
|
2021-02-14 10:18:02 -07:00
|
|
|
use command::AtuinCmd;
|
2021-02-14 08:15:26 -07:00
|
|
|
use local::database::Sqlite;
|
2021-03-10 14:24:08 -07:00
|
|
|
use settings::Settings;
|
2020-10-04 17:59:28 -06:00
|
|
|
|
2021-04-13 12:14:07 -06:00
|
|
|
mod api;
|
2021-02-13 12:37:00 -07:00
|
|
|
mod command;
|
|
|
|
mod local;
|
2021-04-20 10:07:11 -06:00
|
|
|
mod server;
|
2021-03-10 14:24:08 -07:00
|
|
|
mod settings;
|
2021-04-13 12:14:07 -06:00
|
|
|
mod utils;
|
2021-02-13 12:37:00 -07:00
|
|
|
|
2020-10-05 04:52:03 -06:00
|
|
|
#[derive(StructOpt)]
|
|
|
|
#[structopt(
|
2020-10-05 10:20:48 -06:00
|
|
|
author = "Ellie Huxtable <e@elm.sh>",
|
2021-04-13 12:14:07 -06:00
|
|
|
version = "0.5.0",
|
|
|
|
about = "Magical shell history",
|
|
|
|
global_settings(&[AppSettings::ColoredHelp, AppSettings::DeriveDisplayOrder])
|
2020-10-05 04:52:03 -06:00
|
|
|
)]
|
2021-02-13 05:58:40 -07:00
|
|
|
struct Atuin {
|
2020-10-05 10:34:28 -06:00
|
|
|
#[structopt(long, parse(from_os_str), help = "db file path")]
|
2020-10-05 10:20:48 -06:00
|
|
|
db: Option<PathBuf>,
|
|
|
|
|
|
|
|
#[structopt(subcommand)]
|
2021-02-13 05:58:40 -07:00
|
|
|
atuin: AtuinCmd,
|
2020-10-05 10:20:48 -06:00
|
|
|
}
|
|
|
|
|
2021-02-13 05:58:40 -07:00
|
|
|
impl Atuin {
|
2021-04-20 10:07:11 -06:00
|
|
|
async fn run(self, settings: &Settings) -> Result<()> {
|
2021-02-14 08:15:26 -07:00
|
|
|
let db_path = if let Some(db_path) = self.db {
|
|
|
|
let path = db_path
|
|
|
|
.to_str()
|
|
|
|
.ok_or_else(|| eyre!("path {:?} was not valid UTF-8", db_path))?;
|
|
|
|
let path = shellexpand::full(path)?;
|
|
|
|
PathBuf::from(path.as_ref())
|
|
|
|
} else {
|
2021-04-09 05:40:21 -06:00
|
|
|
PathBuf::from(settings.local.db_path.as_str())
|
2020-10-05 10:20:48 -06:00
|
|
|
};
|
|
|
|
|
2021-02-14 08:15:26 -07:00
|
|
|
let mut db = Sqlite::new(db_path)?;
|
2020-10-05 10:20:48 -06:00
|
|
|
|
2021-04-20 10:07:11 -06:00
|
|
|
self.atuin.run(&mut db, settings).await
|
2020-10-05 04:52:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 10:07:11 -06:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<()> {
|
|
|
|
let colors = ColoredLevelConfig::new()
|
|
|
|
.warn(Color::Yellow)
|
|
|
|
.error(Color::Red);
|
2021-04-13 12:14:07 -06:00
|
|
|
|
2021-03-21 14:04:39 -06:00
|
|
|
fern::Dispatch::new()
|
2021-04-20 10:07:11 -06:00
|
|
|
.format(move |out, message, record| {
|
2021-03-21 14:04:39 -06:00
|
|
|
out.finish(format_args!(
|
|
|
|
"{} [{}] {}",
|
2021-04-20 10:07:11 -06:00
|
|
|
chrono::Local::now().to_rfc3339(),
|
|
|
|
colors.color(record.level()),
|
2021-03-21 14:04:39 -06:00
|
|
|
message
|
|
|
|
))
|
|
|
|
})
|
|
|
|
.level(log::LevelFilter::Info)
|
2021-04-20 10:07:11 -06:00
|
|
|
.level_for("sqlx", log::LevelFilter::Warn)
|
2021-03-21 14:04:39 -06:00
|
|
|
.chain(std::io::stdout())
|
|
|
|
.apply()?;
|
2020-10-05 04:52:03 -06:00
|
|
|
|
2021-04-20 10:07:11 -06:00
|
|
|
let settings = Settings::new()?;
|
|
|
|
setup_panic!();
|
|
|
|
|
|
|
|
Atuin::from_args().run(&settings).await
|
2020-10-04 17:59:28 -06:00
|
|
|
}
|