summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 022703b9ed852445b0ef8f9b0d265503c5600bc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#![feature(proc_macro_hygiene)]
#![feature(decl_macro)]
#![warn(clippy::pedantic, clippy::nursery)]

use std::path::PathBuf;

use directories::ProjectDirs;
use eyre::{eyre, Result};
use structopt::StructOpt;
use uuid::Uuid;

#[macro_use]
extern crate log;

#[macro_use]
extern crate rocket;

use command::{history, import, server};
use local::database::Sqlite;
use local::history::History;

mod command;
mod local;
mod remote;

#[derive(StructOpt)]
#[structopt(
    author = "Ellie Huxtable <e@elm.sh>",
    version = "0.1.0",
    about = "Keep your shell history in sync"
)]
struct Atuin {
    #[structopt(long, parse(from_os_str), help = "db file path")]
    db: Option<PathBuf>,

    #[structopt(subcommand)]
    atuin: AtuinCmd,
}

#[derive(StructOpt)]
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),

    #[structopt(about = "generates a UUID")]
    Uuid,
}

impl Atuin {
    fn run(self) -> Result<()> {
        let db_path = if let Some(db_path) = self.db {
            let path = db_path
                .to_str()
                .ok_or_else(|| eyre!("path {:?} was not valid UTF-8", db_path))?;
            let path = shellexpand::full(path)?;
            PathBuf::from(path.as_ref())
        } else {
            let project_dirs =
                ProjectDirs::from("com", "elliehuxtable", "atuin").ok_or_else(|| {
                    eyre!("could not determine db file location\nspecify one using the --db flag")
                })?;
            let root = project_dirs.data_dir();
            root.join("history.db")
        };

        let mut db = Sqlite::new(db_path)?;

        match self.atuin {
            AtuinCmd::History(history) => history.run(&mut db),
            AtuinCmd::Import(import) => import.run(&mut db),
            AtuinCmd::Server(server) => server.run(),

            AtuinCmd::Uuid => {
                println!("{}", Uuid::new_v4().to_simple().to_string());
                Ok(())
            }
        }
    }
}

fn main() -> Result<()> {
    pretty_env_logger::init();

    Atuin::from_args().run()
}