Skip to content

Commit

Permalink
Restructure account commands to account subcommand (atuinsh#984)
Browse files Browse the repository at this point in the history
* Stop running triggers on history delete

* Move to account management dir

* Alter trigger function to only run for inserts

* wip

* Add atuin account subcommands, and re-org delete

* Clarify docs

* Delete silly dupe migration

* Um where did this come from

* Oops, insert only plz
  • Loading branch information
ellie authored May 17, 2023
1 parent 7d5a82d commit ca26383
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 13 deletions.
2 changes: 1 addition & 1 deletion atuin-client/src/api_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ impl<'a> Client<'a> {
}

pub async fn delete(&self) -> Result<()> {
let url = format!("{}/register", self.sync_addr);
let url = format!("{}/account", self.sync_addr);
let url = Url::parse(url.as_str())?;

let resp = self.client.delete(url).send().await?;
Expand Down
30 changes: 30 additions & 0 deletions atuin-server/migrations/20230515221038_trigger-delete-only.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- We do not need to run the trigger on deletes, as the only time we are deleting history is when the user
-- has already been deleted
-- This actually slows down deleting all the history a good bit!

create or replace function user_history_count()
returns trigger as
$func$
begin
if (TG_OP='INSERT') then
update total_history_count_user set total = total + 1 where user_id = new.user_id;

if not found then
insert into total_history_count_user(user_id, total)
values (
new.user_id,
(select count(1) from history where user_id = new.user_id)
);
end if;
end if;

return NEW; -- this is actually ignored for an after trigger, but oh well
end;
$func$
language plpgsql volatile -- pldfplplpflh
cost 100; -- default value

create or replace trigger tg_user_history_count
after insert on history
for each row
execute procedure user_history_count();
10 changes: 10 additions & 0 deletions atuin/src/command/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use env_logger::Builder;
#[cfg(feature = "sync")]
mod sync;

#[cfg(feature = "sync")]
mod account;

mod history;
mod import;
mod search;
Expand All @@ -34,6 +37,9 @@ pub enum Cmd {
#[cfg(feature = "sync")]
#[command(flatten)]
Sync(sync::Cmd),

#[cfg(feature = "sync")]
Account(account::Cmd),
}

impl Cmd {
Expand All @@ -54,8 +60,12 @@ impl Cmd {
Self::Import(import) => import.run(&mut db).await,
Self::Stats(stats) => stats.run(&mut db, &settings).await,
Self::Search(search) => search.run(db, &mut settings).await,

#[cfg(feature = "sync")]
Self::Sync(sync) => sync.run(settings, &mut db).await,

#[cfg(feature = "sync")]
Self::Account(account) => account.run(settings).await,
}
}
}
41 changes: 41 additions & 0 deletions atuin/src/command/client/account.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use clap::{Args, Subcommand};
use eyre::Result;

use atuin_client::settings::Settings;

pub mod delete;
pub mod login;
pub mod logout;
pub mod register;

#[derive(Args)]
pub struct Cmd {
#[command(subcommand)]
command: Commands,
}

#[derive(Subcommand)]
pub enum Commands {
/// Login to the configured server
Login(login::Cmd),

// Register a new account
Register(register::Cmd),

/// Log out
Logout,

// Delete your account, and all synced data
Delete,
}

impl Cmd {
pub async fn run(self, settings: Settings) -> Result<()> {
match self.command {
Commands::Login(l) => l.run(&settings).await,
Commands::Register(r) => r.run(&settings).await,
Commands::Logout => logout::run(&settings),
Commands::Delete => delete::run(&settings).await,
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 5 additions & 11 deletions atuin/src/command/client/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ use eyre::{Result, WrapErr};

use atuin_client::{database::Database, settings::Settings};

mod delete;
mod login;
mod logout;
mod register;
mod status;

use crate::command::client::account;

#[derive(Subcommand)]
#[command(infer_subcommands = true)]
pub enum Cmd {
Expand All @@ -20,16 +18,13 @@ pub enum Cmd {
},

/// Login to the configured server
Login(login::Cmd),
Login(account::login::Cmd),

/// Log out
Logout,

/// Register with the configured server
Register(register::Cmd),

/// Unregister with the configured server
Unregister,
Register(account::register::Cmd),

/// Print the encryption key for transfer to another machine
Key {
Expand All @@ -46,9 +41,8 @@ impl Cmd {
match self {
Self::Sync { force } => run(&settings, force, db).await,
Self::Login(l) => l.run(&settings).await,
Self::Logout => logout::run(&settings),
Self::Logout => account::logout::run(&settings),
Self::Register(r) => r.run(&settings).await,
Self::Unregister => delete::run(&settings).await,
Self::Status => status::run(&settings, db).await,
Self::Key { base64 } => {
use atuin_client::encryption::{encode_key, load_key};
Expand Down
1 change: 1 addition & 0 deletions atuin/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl AtuinCmd {
match self {
#[cfg(feature = "client")]
Self::Client(client) => client.run(),

#[cfg(feature = "server")]
Self::Server(server) => server.run(),
Self::Contributors => {
Expand Down
4 changes: 3 additions & 1 deletion docs/docs/commands/sync.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ here!
You can delete your sync account with

```
atuin unregister
atuin account delete
```

This will remove your account and all synchronized history from the server. Local data will not be touched!

## Key

As all your data is encrypted, Atuin generates a key for you. It's stored in the
Expand Down

0 comments on commit ca26383

Please sign in to comment.