aboutsummaryrefslogtreecommitdiffstats
path: root/crates/client/src/command/mod.rs
blob: a2e750347eafa4641fb8a7b3db2f03d92e016cd7 (plain) (blame)
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
use clap::Subcommand;
use eyre::Result;

#[cfg(not(windows))]
use rustix::{fs::Mode, process::umask};

mod client;
mod contributors;
mod gen_completions;

#[derive(Subcommand)]
#[command(infer_subcommands = true)]
pub(crate) enum AtuinCmd {
    #[command(flatten)]
    Client(client::Cmd),

    /// Generate a UUID
    Uuid,

    Contributors,

    /// Generate shell completions
    GenCompletions(gen_completions::Cmd),
}

impl AtuinCmd {
    pub(crate) fn run(self) -> Result<()> {
        #[cfg(not(windows))]
        {
            // set umask before we potentially open/create files
            // or in other words, 077. Do not allow any access to any other user
            let mode = Mode::RWXG | Mode::RWXO;
            umask(mode);
        }

        match self {
            Self::Client(client) => client.run(),

            Self::Contributors => {
                contributors::run();
                Ok(())
            }
            Self::Uuid => {
                println!("{}", turtle_common::utils::uuid_v7().as_simple());
                Ok(())
            }
            Self::GenCompletions(gen_completions) => gen_completions.run(),
        }
    }
}

pub(crate) fn current_session() -> Result<String> {
    std::env::var("ATUIN_SESSION").map_err(|_| {
        eyre::eyre!("Failed to find $ATUIN_SESSION in the environment. Check that you have correctly set up your shell.")
    })
}