summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 9984c8ccd7c8fd816c6bdea8e02a5a05b6d42587 (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
use clap::Parser;
use rustyline::{error::ReadlineError, DefaultEditor};
use shackle_shell::{run_command, ShackleError};

/// Shackle Shell - A replacement for git-shell with repo management commands built in.
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
    /// Run a single shell command and exit
    #[arg(short, long)]
    command: Option<String>,
}

fn main() {
    if let Err(e) = main_inner() {
        eprintln!("{}", e);
        std::process::exit(1);
    }
}

fn main_inner() -> Result<(), ShackleError> {
    let args = Args::parse();
    match args.command {
        Some(user_input) => {
            run_command(&user_input)?;
        }
        None => {
            run_interactive_loop()?;
        }
    }

    Ok(())
}

fn run_interactive_loop() -> Result<(), ShackleError> {
    let mut rl = DefaultEditor::new()?;
    loop {
        let readline = rl.readline("> ");
        match readline.as_ref().map(|user_input| user_input.trim()) {
            Ok("") => {}
            Ok(user_input) => {
                rl.add_history_entry(user_input)?;
                match run_command(user_input) {
                    Ok(control_flow) => {
                        if control_flow.is_break() {
                            break;
                        }
                    }
                    Err(e) => {
                        println!("{}", e);
                    }
                }
            }
            Err(ReadlineError::Interrupted) => {
                println!("Interrupted");
                break;
            }
            Err(ReadlineError::Eof) => {
                break;
            }
            Err(err) => {
                println!("Error: {}", err);
                break;
            }
        }
    }
    Ok(())
}