summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 56cd7299d87a308c2488e423b9c356d257ee1e4a (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
mod git;
mod parser;

use clap::Parser;
use parser::*;
use rustyline::{error::ReadlineError, DefaultEditor};
use std::{io, ops::ControlFlow, process::Command};
use thiserror::Error;

/// 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() -> Result<(), ShackleError> {
    let args = Args::parse();
    match args.command {
        Some(user_input) => {
            run_command(user_input)?;
        }
        None => {
            run_interactive_loop()?;
        }
    }

    Ok(())
}

fn run_command(user_input: String) -> Result<ControlFlow<(), ()>, ShackleError> {
    match user_input.parse::<ShackleCommand>() {
        Err(parse_error) => {
            println!("{}", parse_error);
        }
        Ok(ShackleCommand::Whitespace) => {}
        Ok(ShackleCommand::Exit) => {
            return Ok(ControlFlow::Break(()));
        }
        Ok(ShackleCommand::GitInit(GitInitArgs { repo_name })) => {
            git::init(&repo_name)?;
            println!("Successfully created \"{}.git\"", repo_name);
        }
        Ok(ShackleCommand::GitUploadPack(upload_pack_args)) => {
            let mut command = Command::new("git-upload-pack");

            if upload_pack_args.strict {
                command.arg("strict");
            }
            if upload_pack_args.no_strict {
                command.arg("no-strict");
            }
            if let Some(timeout) = upload_pack_args.timeout {
                command.args(["timeout", &timeout.to_string()]);
            }
            if upload_pack_args.stateless_rpc {
                command.arg("stateless-rpc");
            }
            if upload_pack_args.advertise_refs {
                command.arg("advertise-refs");
            }

            command.arg(&upload_pack_args.directory);

            command.spawn()?.wait()?;
        }
        Ok(ShackleCommand::GitReceivePack(receive_pack_args)) => {
            let mut command = Command::new("git-receive-pack");

            if receive_pack_args.http_backend_info_refs {
                command.arg("--http-backend-info-refs");
            }

            command.arg(&receive_pack_args.directory);

            command.spawn()?.wait()?;
        }
    }
    Ok(ControlFlow::Continue(()))
}

fn run_interactive_loop() -> Result<(), ShackleError> {
    let mut rl = DefaultEditor::new()?;
    loop {
        let readline = rl.readline("> ");
        match readline {
            Ok(user_input) => {
                rl.add_history_entry(user_input.as_str())?;
                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(())
}

pub enum FlowControl {}

#[derive(Error, Debug)]
pub enum ShackleError {
    #[error(transparent)]
    IoError(#[from] io::Error),
    #[error(transparent)]
    GitError(#[from] git2::Error),
    #[error(transparent)]
    ReadlineError(#[from] ReadlineError),
}