summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: d6b2725a6a29e6732ad91bf8fd1588a82437966e (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// will uncomment this once it is closer to release
// #![warn(clippy::cargo)]
#![warn(clippy::pedantic)]
#![warn(clippy::nursery)]
#![warn(clippy::as_conversions)]
#![warn(clippy::get_unwrap)]
#![allow(clippy::cognitive_complexity)]
#![allow(clippy::missing_const_for_fn)]
#![allow(clippy::option_option)]
#![allow(clippy::similar_names)]
#![allow(clippy::struct_excessive_bools)]
#![allow(clippy::too_many_arguments)]
#![allow(clippy::too_many_lines)]
#![allow(clippy::type_complexity)]
// this isn't super relevant in a binary - if it's actually a problem, we'll
// just get a compilation failure
#![allow(clippy::future_not_send)]

mod config;
mod dirs;
mod env;
mod format;
mod info;
mod parse;
mod prelude;
mod runner;
mod shell;

use prelude::*;

use clap::Parser as _;

#[derive(clap::Parser)]
#[clap(about = "NoteBook SHell")]
struct Opt {
    #[clap(short = 'c')]
    command: Option<String>,

    #[clap(long)]
    status_fd: Option<std::os::unix::io::RawFd>,
}

#[tokio::main]
async fn async_main(opt: Opt) -> Result<i32> {
    if let Some(command) = opt.command {
        let mut shell_write = opt.status_fd.and_then(|fd| {
            nix::sys::stat::fstat(fd).ok().map(|_| {
                // Safety: we don't create File instances for or read/write
                // data on this fd anywhere else
                unsafe { tokio::fs::File::from_raw_fd(fd) }
            })
        });

        return runner::main(command, &mut shell_write).await;
    }

    #[cfg(nbsh_tokio_console)]
    console_subscriber::init();

    shell::main().await
}

fn main() {
    match async_main(Opt::parse()) {
        Ok(code) => {
            std::process::exit(code);
        }
        Err(e) => {
            eprintln!("nbsh: {}", e);
            std::process::exit(1);
        }
    };
}