2021-04-20 14:53:07 -06:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2021-11-17 04:50:34 -07:00
|
|
|
use eyre::{Result, WrapErr};
|
2021-02-14 10:18:02 -07:00
|
|
|
use structopt::StructOpt;
|
2021-02-14 11:00:41 -07:00
|
|
|
|
2021-04-20 14:53:07 -06:00
|
|
|
use atuin_client::database::Sqlite;
|
|
|
|
use atuin_client::settings::Settings as ClientSettings;
|
|
|
|
use atuin_common::utils::uuid_v4;
|
|
|
|
use atuin_server::settings::Settings as ServerSettings;
|
2021-02-14 10:18:02 -07:00
|
|
|
|
2021-03-19 18:50:31 -06:00
|
|
|
mod event;
|
2021-02-14 10:18:02 -07:00
|
|
|
mod history;
|
|
|
|
mod import;
|
2021-02-15 16:33:30 -07:00
|
|
|
mod init;
|
2021-04-13 12:14:07 -06:00
|
|
|
mod login;
|
2021-05-09 13:11:17 -06:00
|
|
|
mod logout;
|
2021-04-13 12:14:07 -06:00
|
|
|
mod register;
|
2021-03-19 18:50:31 -06:00
|
|
|
mod search;
|
2021-02-14 10:18:02 -07:00
|
|
|
mod server;
|
2021-02-14 15:12:35 -07:00
|
|
|
mod stats;
|
2021-04-13 12:14:07 -06:00
|
|
|
mod sync;
|
2021-02-14 10:18:02 -07:00
|
|
|
|
|
|
|
#[derive(StructOpt)]
|
|
|
|
pub enum AtuinCmd {
|
|
|
|
#[structopt(
|
|
|
|
about="manipulate shell history",
|
|
|
|
aliases=&["h", "hi", "his", "hist", "histo", "histor"],
|
|
|
|
)]
|
|
|
|
History(history::Cmd),
|
|
|
|
|
|
|
|
#[structopt(about = "import shell history from file")]
|
|
|
|
Import(import::Cmd),
|
|
|
|
|
|
|
|
#[structopt(about = "start an atuin server")]
|
|
|
|
Server(server::Cmd),
|
|
|
|
|
2021-02-14 15:12:35 -07:00
|
|
|
#[structopt(about = "calculate statistics for your history")]
|
|
|
|
Stats(stats::Cmd),
|
|
|
|
|
2021-02-15 16:33:30 -07:00
|
|
|
#[structopt(about = "output shell setup")]
|
2021-04-26 04:50:31 -06:00
|
|
|
Init(init::Cmd),
|
2021-02-15 16:33:30 -07:00
|
|
|
|
2021-02-14 10:18:02 -07:00
|
|
|
#[structopt(about = "generates a UUID")]
|
|
|
|
Uuid,
|
2021-03-19 18:50:31 -06:00
|
|
|
|
|
|
|
#[structopt(about = "interactive history search")]
|
2021-04-21 11:13:51 -06:00
|
|
|
Search {
|
|
|
|
#[structopt(long, short, about = "filter search result by directory")]
|
|
|
|
cwd: Option<String>,
|
|
|
|
|
2021-04-25 11:21:52 -06:00
|
|
|
#[structopt(long = "exclude-cwd", about = "exclude directory from results")]
|
|
|
|
exclude_cwd: Option<String>,
|
|
|
|
|
2021-04-21 11:13:51 -06:00
|
|
|
#[structopt(long, short, about = "filter search result by exit code")]
|
|
|
|
exit: Option<i64>,
|
|
|
|
|
2021-04-25 11:21:52 -06:00
|
|
|
#[structopt(long = "exclude-exit", about = "exclude results with this exit code")]
|
|
|
|
exclude_exit: Option<i64>,
|
|
|
|
|
|
|
|
#[structopt(long, short, about = "only include results added before this date")]
|
|
|
|
before: Option<String>,
|
|
|
|
|
|
|
|
#[structopt(long, about = "only include results after this date")]
|
|
|
|
after: Option<String>,
|
|
|
|
|
2021-04-21 11:13:51 -06:00
|
|
|
#[structopt(long, short, about = "open interactive search UI")]
|
|
|
|
interactive: bool,
|
|
|
|
|
2021-04-25 11:21:52 -06:00
|
|
|
#[structopt(long, short, about = "use human-readable formatting for time")]
|
|
|
|
human: bool,
|
|
|
|
|
2021-04-21 11:13:51 -06:00
|
|
|
query: Vec<String>,
|
2021-05-09 12:01:21 -06:00
|
|
|
|
|
|
|
#[structopt(long, about = "Show only the text of the command")]
|
|
|
|
cmd_only: bool,
|
2021-04-21 11:13:51 -06:00
|
|
|
},
|
2021-04-13 12:14:07 -06:00
|
|
|
|
|
|
|
#[structopt(about = "sync with the configured server")]
|
|
|
|
Sync {
|
|
|
|
#[structopt(long, short, about = "force re-download everything")]
|
|
|
|
force: bool,
|
|
|
|
},
|
|
|
|
|
|
|
|
#[structopt(about = "login to the configured server")]
|
|
|
|
Login(login::Cmd),
|
|
|
|
|
2021-05-09 13:11:17 -06:00
|
|
|
#[structopt(about = "log out")]
|
|
|
|
Logout,
|
|
|
|
|
2021-04-13 12:14:07 -06:00
|
|
|
#[structopt(about = "register with the configured server")]
|
|
|
|
Register(register::Cmd),
|
|
|
|
|
|
|
|
#[structopt(about = "print the encryption key for transfer to another machine")]
|
|
|
|
Key,
|
2021-02-14 10:18:02 -07:00
|
|
|
}
|
2021-02-14 11:00:41 -07:00
|
|
|
|
|
|
|
impl AtuinCmd {
|
2021-04-20 14:53:07 -06:00
|
|
|
pub async fn run(self) -> Result<()> {
|
2021-11-17 04:50:34 -07:00
|
|
|
let client_settings = ClientSettings::new().wrap_err("could not load client settings")?;
|
|
|
|
let server_settings = ServerSettings::new().wrap_err("could not load server settings")?;
|
2021-04-20 14:53:07 -06:00
|
|
|
|
|
|
|
let db_path = PathBuf::from(client_settings.db_path.as_str());
|
|
|
|
|
2021-04-25 11:21:52 -06:00
|
|
|
let mut db = Sqlite::new(db_path).await?;
|
2021-04-20 14:53:07 -06:00
|
|
|
|
2021-02-14 11:00:41 -07:00
|
|
|
match self {
|
2021-04-20 14:53:07 -06:00
|
|
|
Self::History(history) => history.run(&client_settings, &mut db).await,
|
2021-04-25 11:21:52 -06:00
|
|
|
Self::Import(import) => import.run(&mut db).await,
|
2021-04-20 14:53:07 -06:00
|
|
|
Self::Server(server) => server.run(&server_settings).await,
|
2021-04-25 11:21:52 -06:00
|
|
|
Self::Stats(stats) => stats.run(&mut db, &client_settings).await,
|
2021-09-24 10:03:37 -06:00
|
|
|
Self::Init(init) => {
|
|
|
|
init.run();
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-04-21 11:13:51 -06:00
|
|
|
Self::Search {
|
|
|
|
cwd,
|
|
|
|
exit,
|
|
|
|
interactive,
|
2021-04-25 11:21:52 -06:00
|
|
|
human,
|
|
|
|
exclude_exit,
|
|
|
|
exclude_cwd,
|
|
|
|
before,
|
|
|
|
after,
|
2021-04-21 11:13:51 -06:00
|
|
|
query,
|
2021-05-09 12:01:21 -06:00
|
|
|
cmd_only,
|
2021-04-25 11:21:52 -06:00
|
|
|
} => {
|
|
|
|
search::run(
|
2021-05-09 01:33:56 -06:00
|
|
|
&client_settings,
|
2021-04-25 11:21:52 -06:00
|
|
|
cwd,
|
|
|
|
exit,
|
|
|
|
interactive,
|
|
|
|
human,
|
|
|
|
exclude_exit,
|
|
|
|
exclude_cwd,
|
|
|
|
before,
|
|
|
|
after,
|
2021-05-09 12:01:21 -06:00
|
|
|
cmd_only,
|
2021-04-25 11:21:52 -06:00
|
|
|
&query,
|
|
|
|
&mut db,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
2021-02-14 11:00:41 -07:00
|
|
|
|
2021-04-20 14:53:07 -06:00
|
|
|
Self::Sync { force } => sync::run(&client_settings, force, &mut db).await,
|
2021-12-08 06:37:49 -07:00
|
|
|
Self::Login(l) => l.run(&client_settings).await,
|
2021-05-09 13:11:17 -06:00
|
|
|
Self::Logout => {
|
|
|
|
logout::run();
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-11-13 15:40:24 -07:00
|
|
|
Self::Register(r) => {
|
2021-12-08 06:37:49 -07:00
|
|
|
register::run(&client_settings, &r.username, &r.email, &r.password).await
|
2021-11-13 15:40:24 -07:00
|
|
|
}
|
2021-04-13 12:14:07 -06:00
|
|
|
Self::Key => {
|
2021-11-17 04:50:34 -07:00
|
|
|
use atuin_client::encryption::{encode_key, load_key};
|
|
|
|
let key = load_key(&client_settings).wrap_err("could not load encryption key")?;
|
|
|
|
let encode = encode_key(key).wrap_err("could not encode encryption key")?;
|
|
|
|
println!("{}", encode);
|
2021-04-13 12:14:07 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-02-14 11:00:41 -07:00
|
|
|
Self::Uuid => {
|
2021-02-14 11:40:51 -07:00
|
|
|
println!("{}", uuid_v4());
|
2021-02-14 11:00:41 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|