Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions src/command/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
use clap::CommandFactory;
use clap::Subcommand;
use clap_complete::Shell;
use clap_complete::{generate, generate_to};
use eyre::{Result, WrapErr};

use atuin_client::database::Sqlite;
use atuin_client::settings::Settings;
use atuin_common::utils::uuid_v4;

mod event;
mod history;
mod import;
mod init;
mod login;
mod logout;
mod register;
mod search;
mod stats;
mod sync;
use std::path::PathBuf;

#[derive(Subcommand)]
#[clap(infer_subcommands = true)]
pub enum Cmd {
/// Manipulate shell history
#[clap(subcommand)]
History(history::Cmd),

/// Import shell history from file
#[clap(subcommand)]
Import(import::Cmd),

/// Calculate statistics for your history
#[clap(subcommand)]
Stats(stats::Cmd),

/// Output shell setup
#[clap(subcommand)]
Init(init::Cmd),

/// Generate a UUID
Uuid,

/// Interactive history search
Search(search::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,

/// Generate shell completions
GenCompletions {
/// Set the shell for generating completions
#[clap(long, short)]
shell: Shell,

/// Set the output directory
#[clap(long, short)]
out_dir: Option<String>,
},
}

impl Cmd {
pub async fn run(self) -> Result<()> {
pretty_env_logger::init();

let settings = Settings::new().wrap_err("could not load client settings")?;

let db_path = PathBuf::from(settings.db_path.as_str());
let mut db = Sqlite::new(db_path).await?;

match self {
Self::History(history) => history.run(&settings, &mut db).await,
Self::Import(import) => import.run(&mut db).await,
Self::Stats(stats) => stats.run(&mut db, &settings).await,
Self::Init(init) => {
init.run();
Ok(())
}
Self::Search(search) => search.run(&mut db, &settings).await,
Self::Sync { force } => sync::run(&settings, force, &mut 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(())
}
Self::Uuid => {
println!("{}", uuid_v4());
Ok(())
}
Self::GenCompletions { shell, out_dir } => {
let mut cli = crate::Atuin::command();

match out_dir {
Some(out_dir) => {
generate_to(shell, &mut cli, env!("CARGO_PKG_NAME"), &out_dir)?;
}
None => {
generate(
shell,
&mut cli,
env!("CARGO_PKG_NAME"),
&mut std::io::stdout(),
);
}
}

Ok(())
}
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions src/command/init.rs → src/command/client/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ pub enum Cmd {
}

fn init_zsh() {
let full = include_str!("../shell/atuin.zsh");
let full = include_str!("../../shell/atuin.zsh");
println!("{}", full);
}

fn init_bash() {
let full = include_str!("../shell/atuin.bash");
let full = include_str!("../../shell/atuin.bash");
println!("{}", full);
}

fn init_fish() {
let full = include_str!("../shell/atuin.fish");
let full = include_str!("../../shell/atuin.fish");
println!("{}", full);
}

Expand Down
File renamed without changes.
7 changes: 5 additions & 2 deletions src/command/logout.rs → src/command/client/logout.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use eyre::{Context, Result};
use fs_err::remove_file;

pub fn run() {
pub fn run() -> Result<()> {
let session_path = atuin_common::utils::data_dir().join("session");

if session_path.exists() {
remove_file(session_path.as_path()).expect("Failed to remove session file");
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(())
}
6 changes: 6 additions & 0 deletions src/command/register.rs → src/command/client/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ pub struct Cmd {
pub password: Option<String>,
}

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<String>,
Expand Down
69 changes: 67 additions & 2 deletions src/command/search.rs → src/command/client/search.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use chrono::Utc;
use clap::Parser;
use eyre::Result;
use std::{io::stdout, ops::Sub, time::Duration};

use termion::{event::Key, input::MouseTerminal, raw::IntoRawMode, screen::AlternateScreen};
use tui::{
backend::{Backend, TermionBackend},
Expand All @@ -19,10 +19,75 @@ use atuin_client::{
settings::{SearchMode, Settings},
};

use crate::command::event::{Event, Events};
use super::event::{Event, Events};

const VERSION: &str = env!("CARGO_PKG_VERSION");

#[derive(Parser)]
pub struct Cmd {
/// Filter search result by directory
#[clap(long, short)]
cwd: Option<String>,

/// Exclude directory from results
#[clap(long = "exclude-cwd")]
exclude_cwd: Option<String>,

/// Filter search result by exit code
#[clap(long, short)]
exit: Option<i64>,

/// Exclude results with this exit code
#[clap(long = "exclude-exit")]
exclude_exit: Option<i64>,

/// Only include results added before this date
#[clap(long, short)]
before: Option<String>,

/// Only include results after this date
#[clap(long)]
after: Option<String>,

/// Open interactive search UI
#[clap(long, short)]
interactive: bool,

/// Use human-readable formatting for time
#[clap(long)]
human: bool,

query: Vec<String>,

/// Show only the text of the command
#[clap(long)]
cmd_only: bool,
}

impl Cmd {
pub async fn run(
self,
db: &mut (impl Database + Send + Sync),
settings: &Settings,
) -> Result<()> {
run(
settings,
self.cwd,
self.exit,
self.interactive,
self.human,
self.exclude_exit,
self.exclude_cwd,
self.before,
self.after,
self.cmd_only,
&self.query,
db,
)
.await
}
}

struct State {
input: String,

Expand Down
File renamed without changes.
File renamed without changes.
Loading