From 7436e4ff651b64d4019a59d04c30c414ae220403 Mon Sep 17 00:00:00 2001 From: Conrad Ludgate Date: Fri, 22 Apr 2022 21:14:23 +0100 Subject: feature-flags (#328) * use feature flags * fmt * fix features * update ci * fmt Co-authored-by: Ellie Huxtable --- src/command/client/history.rs | 11 +++++- src/command/client/login.rs | 75 ------------------------------------- src/command/client/logout.rs | 15 -------- src/command/client/register.rs | 50 ------------------------- src/command/client/sync.rs | 59 ++++++++++++++++++++++++++--- src/command/client/sync/login.rs | 75 +++++++++++++++++++++++++++++++++++++ src/command/client/sync/logout.rs | 15 ++++++++ src/command/client/sync/register.rs | 50 +++++++++++++++++++++++++ 8 files changed, 203 insertions(+), 147 deletions(-) delete mode 100644 src/command/client/login.rs delete mode 100644 src/command/client/logout.rs delete mode 100644 src/command/client/register.rs create mode 100644 src/command/client/sync/login.rs create mode 100644 src/command/client/sync/logout.rs create mode 100644 src/command/client/sync/register.rs (limited to 'src/command/client') diff --git a/src/command/client/history.rs b/src/command/client/history.rs index 994cbfd5..4f96c444 100644 --- a/src/command/client/history.rs +++ b/src/command/client/history.rs @@ -9,6 +9,8 @@ use tabwriter::TabWriter; use atuin_client::database::{current_context, Database}; use atuin_client::history::History; use atuin_client::settings::Settings; + +#[cfg(feature = "sync")] use atuin_client::sync; #[derive(Subcommand)] @@ -143,8 +145,13 @@ impl Cmd { db.update(&h).await?; if settings.should_sync()? { - debug!("running periodic background sync"); - sync::sync(settings, false, db).await?; + #[cfg(feature = "sync")] + { + debug!("running periodic background sync"); + sync::sync(settings, false, db).await?; + } + #[cfg(not(feature = "sync"))] + debug!("not compiled with sync support"); } else { debug!("sync disabled! not syncing"); } diff --git a/src/command/client/login.rs b/src/command/client/login.rs deleted file mode 100644 index efc9c590..00000000 --- a/src/command/client/login.rs +++ /dev/null @@ -1,75 +0,0 @@ -use std::io; - -use atuin_common::api::LoginRequest; -use clap::AppSettings; -use clap::Parser; -use eyre::Result; -use tokio::{fs::File, io::AsyncWriteExt}; - -use atuin_client::api_client; -use atuin_client::settings::Settings; - -#[derive(Parser)] -#[clap(setting(AppSettings::DeriveDisplayOrder))] -pub struct Cmd { - #[clap(long, short)] - pub username: Option, - - #[clap(long, short)] - pub password: Option, - - /// The encryption key for your account - #[clap(long, short)] - pub key: Option, -} - -fn get_input() -> Result { - let mut input = String::new(); - io::stdin().read_line(&mut input)?; - Ok(input.trim_end_matches(&['\r', '\n'][..]).to_string()) -} - -impl Cmd { - pub async fn run(&self, settings: &Settings) -> Result<()> { - let session_path = atuin_common::utils::data_dir().join("session"); - - if session_path.exists() { - println!( - "You are already logged in! Please run 'atuin logout' if you wish to login again" - ); - - return Ok(()); - } - - let username = or_user_input(&self.username, "username"); - let password = or_user_input(&self.password, "password"); - let key = or_user_input(&self.key, "encryption key"); - - let session = api_client::login( - settings.sync_address.as_str(), - LoginRequest { username, password }, - ) - .await?; - - let session_path = settings.session_path.as_str(); - let mut file = File::create(session_path).await?; - file.write_all(session.session.as_bytes()).await?; - - let key_path = settings.key_path.as_str(); - let mut file = File::create(key_path).await?; - file.write_all(key.as_bytes()).await?; - - println!("Logged in!"); - - Ok(()) - } -} - -pub(super) fn or_user_input(value: &'_ Option, name: &'static str) -> String { - value.clone().unwrap_or_else(|| read_user_input(name)) -} - -fn read_user_input(name: &'static str) -> String { - eprint!("Please enter {}: ", name); - get_input().expect("Failed to read from input") -} diff --git a/src/command/client/logout.rs b/src/command/client/logout.rs deleted file mode 100644 index a7e9541d..00000000 --- a/src/command/client/logout.rs +++ /dev/null @@ -1,15 +0,0 @@ -use eyre::{Context, Result}; -use fs_err::remove_file; - -pub fn run() -> Result<()> { - let session_path = atuin_common::utils::data_dir().join("session"); - - if session_path.exists() { - remove_file(session_path.as_path()).context("Failed to remove session file")?; - println!("You have logged out!"); - } else { - println!("You are not logged in"); - } - - Ok(()) -} diff --git a/src/command/client/register.rs b/src/command/client/register.rs deleted file mode 100644 index 2c60a2e9..00000000 --- a/src/command/client/register.rs +++ /dev/null @@ -1,50 +0,0 @@ -use clap::AppSettings; -use clap::Parser; -use eyre::Result; -use tokio::{fs::File, io::AsyncWriteExt}; - -use atuin_client::api_client; -use atuin_client::settings::Settings; - -#[derive(Parser)] -#[clap(setting(AppSettings::DeriveDisplayOrder))] -pub struct Cmd { - #[clap(long, short)] - pub username: Option, - - #[clap(long, short)] - pub email: Option, - - #[clap(long, short)] - pub password: Option, -} - -impl Cmd { - pub async fn run(self, settings: &Settings) -> Result<()> { - run(settings, &self.username, &self.email, &self.password).await - } -} - -pub async fn run( - settings: &Settings, - username: &Option, - email: &Option, - password: &Option, -) -> Result<()> { - use super::login::or_user_input; - let username = or_user_input(username, "username"); - let email = or_user_input(email, "email"); - let password = or_user_input(password, "password"); - - let session = - api_client::register(settings.sync_address.as_str(), &username, &email, &password).await?; - - let path = settings.session_path.as_str(); - let mut file = File::create(path).await?; - file.write_all(session.session.as_bytes()).await?; - - // Create a new key, and save it to disk - let _key = atuin_client::encryption::new_key(settings)?; - - Ok(()) -} diff --git a/src/command/client/sync.rs b/src/command/client/sync.rs index f8bfd5e2..8e80310a 100644 --- a/src/command/client/sync.rs +++ b/src/command/client/sync.rs @@ -1,15 +1,64 @@ -use eyre::Result; - use atuin_client::database::Database; +use clap::Subcommand; +use eyre::{Result, WrapErr}; + use atuin_client::settings::Settings; -use atuin_client::sync; -pub async fn run( +mod login; +mod logout; +mod register; + +#[derive(Subcommand)] +#[clap(infer_subcommands = true)] +pub enum Cmd { + /// Sync with the configured server + Sync { + /// Force re-download everything + #[clap(long, short)] + force: bool, + }, + + /// Login to the configured server + Login(login::Cmd), + + /// Log out + Logout, + + /// Register with the configured server + Register(register::Cmd), + + /// Print the encryption key for transfer to another machine + Key, +} + +impl Cmd { + pub async fn run( + self, + settings: Settings, + db: &mut (impl Database + Send + Sync), + ) -> Result<()> { + match self { + Self::Sync { force } => run(&settings, force, db).await, + Self::Login(l) => l.run(&settings).await, + Self::Logout => logout::run(), + Self::Register(r) => r.run(&settings).await, + Self::Key => { + use atuin_client::encryption::{encode_key, load_key}; + let key = load_key(&settings).wrap_err("could not load encryption key")?; + let encode = encode_key(key).wrap_err("could not encode encryption key")?; + println!("{}", encode); + Ok(()) + } + } + } +} + +async fn run( settings: &Settings, force: bool, db: &mut (impl Database + Send + Sync), ) -> Result<()> { - sync::sync(settings, force, db).await?; + atuin_client::sync::sync(settings, force, db).await?; println!( "Sync complete! {} items in database, force: {}", db.history_count().await?, diff --git a/src/command/client/sync/login.rs b/src/command/client/sync/login.rs new file mode 100644 index 00000000..efc9c590 --- /dev/null +++ b/src/command/client/sync/login.rs @@ -0,0 +1,75 @@ +use std::io; + +use atuin_common::api::LoginRequest; +use clap::AppSettings; +use clap::Parser; +use eyre::Result; +use tokio::{fs::File, io::AsyncWriteExt}; + +use atuin_client::api_client; +use atuin_client::settings::Settings; + +#[derive(Parser)] +#[clap(setting(AppSettings::DeriveDisplayOrder))] +pub struct Cmd { + #[clap(long, short)] + pub username: Option, + + #[clap(long, short)] + pub password: Option, + + /// The encryption key for your account + #[clap(long, short)] + pub key: Option, +} + +fn get_input() -> Result { + let mut input = String::new(); + io::stdin().read_line(&mut input)?; + Ok(input.trim_end_matches(&['\r', '\n'][..]).to_string()) +} + +impl Cmd { + pub async fn run(&self, settings: &Settings) -> Result<()> { + let session_path = atuin_common::utils::data_dir().join("session"); + + if session_path.exists() { + println!( + "You are already logged in! Please run 'atuin logout' if you wish to login again" + ); + + return Ok(()); + } + + let username = or_user_input(&self.username, "username"); + let password = or_user_input(&self.password, "password"); + let key = or_user_input(&self.key, "encryption key"); + + let session = api_client::login( + settings.sync_address.as_str(), + LoginRequest { username, password }, + ) + .await?; + + let session_path = settings.session_path.as_str(); + let mut file = File::create(session_path).await?; + file.write_all(session.session.as_bytes()).await?; + + let key_path = settings.key_path.as_str(); + let mut file = File::create(key_path).await?; + file.write_all(key.as_bytes()).await?; + + println!("Logged in!"); + + Ok(()) + } +} + +pub(super) fn or_user_input(value: &'_ Option, name: &'static str) -> String { + value.clone().unwrap_or_else(|| read_user_input(name)) +} + +fn read_user_input(name: &'static str) -> String { + eprint!("Please enter {}: ", name); + get_input().expect("Failed to read from input") +} diff --git a/src/command/client/sync/logout.rs b/src/command/client/sync/logout.rs new file mode 100644 index 00000000..a7e9541d --- /dev/null +++ b/src/command/client/sync/logout.rs @@ -0,0 +1,15 @@ +use eyre::{Context, Result}; +use fs_err::remove_file; + +pub fn run() -> Result<()> { + let session_path = atuin_common::utils::data_dir().join("session"); + + if session_path.exists() { + remove_file(session_path.as_path()).context("Failed to remove session file")?; + println!("You have logged out!"); + } else { + println!("You are not logged in"); + } + + Ok(()) +} diff --git a/src/command/client/sync/register.rs b/src/command/client/sync/register.rs new file mode 100644 index 00000000..2c60a2e9 --- /dev/null +++ b/src/command/client/sync/register.rs @@ -0,0 +1,50 @@ +use clap::AppSettings; +use clap::Parser; +use eyre::Result; +use tokio::{fs::File, io::AsyncWriteExt}; + +use atuin_client::api_client; +use atuin_client::settings::Settings; + +#[derive(Parser)] +#[clap(setting(AppSettings::DeriveDisplayOrder))] +pub struct Cmd { + #[clap(long, short)] + pub username: Option, + + #[clap(long, short)] + pub email: Option, + + #[clap(long, short)] + pub password: Option, +} + +impl Cmd { + pub async fn run(self, settings: &Settings) -> Result<()> { + run(settings, &self.username, &self.email, &self.password).await + } +} + +pub async fn run( + settings: &Settings, + username: &Option, + email: &Option, + password: &Option, +) -> Result<()> { + use super::login::or_user_input; + let username = or_user_input(username, "username"); + let email = or_user_input(email, "email"); + let password = or_user_input(password, "password"); + + let session = + api_client::register(settings.sync_address.as_str(), &username, &email, &password).await?; + + let path = settings.session_path.as_str(); + let mut file = File::create(path).await?; + file.write_all(session.session.as_bytes()).await?; + + // Create a new key, and save it to disk + let _key = atuin_client::encryption::new_key(settings)?; + + Ok(()) +} -- cgit v1.3.1