atuin/src/main.rs

97 lines
2.3 KiB
Rust
Raw Normal View History

2021-02-14 06:28:01 -07:00
#![feature(proc_macro_hygiene)]
#![feature(decl_macro)]
#![warn(clippy::pedantic)]
use std::path::PathBuf;
use directories::ProjectDirs;
use eyre::{eyre, Result};
use structopt::StructOpt;
2021-02-13 13:21:49 -07:00
use uuid::Uuid;
#[macro_use]
extern crate log;
2021-02-14 06:28:01 -07:00
#[macro_use]
extern crate rocket;
use command::{history::HistoryCmd, import::ImportCmd, server::ServerCmd};
2021-02-13 16:20:04 -07:00
use local::database::SqliteDatabase;
use local::history::History;
2021-02-13 12:37:00 -07:00
mod command;
mod local;
2021-02-14 06:28:01 -07:00
mod server;
2021-02-13 12:37:00 -07:00
#[derive(StructOpt)]
#[structopt(
author = "Ellie Huxtable <e@elm.sh>",
version = "0.1.0",
about = "Keep your shell history in sync"
)]
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")]
db: Option<PathBuf>,
#[structopt(subcommand)]
2021-02-13 05:58:40 -07:00
atuin: AtuinCmd,
}
#[derive(StructOpt)]
2021-02-13 05:58:40 -07:00
enum AtuinCmd {
#[structopt(
about="manipulate shell history",
aliases=&["h", "hi", "his", "hist", "histo", "histor"],
)]
History(HistoryCmd),
#[structopt(about = "import shell history from file")]
2021-02-13 12:37:00 -07:00
Import(ImportCmd),
2021-02-13 13:21:49 -07:00
#[structopt(about = "start an atuin server")]
2021-02-14 06:28:01 -07:00
Server(ServerCmd),
2021-02-13 13:21:49 -07:00
#[structopt(about = "generates a UUID")]
Uuid,
}
2021-02-13 05:58:40 -07:00
impl Atuin {
fn run(self) -> Result<()> {
let db_path = match self.db {
Some(db_path) => {
let path = db_path
.to_str()
.ok_or(eyre!("path {:?} was not valid UTF-8", db_path))?;
let path = shellexpand::full(path)?;
PathBuf::from(path.as_ref())
}
None => {
2021-02-13 05:58:40 -07:00
let project_dirs = ProjectDirs::from("com", "elliehuxtable", "atuin").ok_or(
eyre!("could not determine db file location\nspecify one using the --db flag"),
)?;
let root = project_dirs.data_dir();
root.join("history.db")
}
};
2021-02-13 12:37:00 -07:00
let mut db = SqliteDatabase::new(db_path)?;
2021-02-13 05:58:40 -07:00
match self.atuin {
2021-02-13 13:21:49 -07:00
AtuinCmd::History(history) => history.run(&mut db),
2021-02-13 12:37:00 -07:00
AtuinCmd::Import(import) => import.run(&mut db),
2021-02-14 06:28:01 -07:00
AtuinCmd::Server(server) => server.run(),
2021-02-13 13:21:49 -07:00
AtuinCmd::Uuid => {
println!("{}", Uuid::new_v4().to_simple().to_string());
Ok(())
}
}
}
}
fn main() -> Result<()> {
pretty_env_logger::init();
2021-02-13 05:58:40 -07:00
Atuin::from_args().run()
}