summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 2a0d3b08dd0ac97364bf3eeb03a60a5b2ec1e63a (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
129
130
131
132
133
134
135
136
137
138
139
140
141
mod parser;
pub mod user_info;
pub mod vcs;

use crate::vcs::git;
use comfy_table::Table;
use humansize::{format_size, BINARY};
use parser::*;
use rustyline::{error::ReadlineError, DefaultEditor};
use std::{io, ops::ControlFlow};
use thiserror::Error;

pub fn run_command(user_input: &str) -> Result<ControlFlow<(), ()>, ShackleError> {
    match user_input.parse::<ShackleCommand>() {
        Err(parse_error) => {
            println!("{}", parse_error);
        }
        Ok(ShackleCommand::Exit) => {
            return Ok(ControlFlow::Break(()));
        }
        Ok(ShackleCommand::List(ListArgs { verbose })) => {
            let mut table = Table::new();
            if !verbose {
                table.set_header(vec!["path", "description"]);
                let listing = git::list()?;
                for meta in listing {
                    table.add_row(vec![meta.path.display().to_string(), meta.description]);
                }
            } else {
                table.set_header(vec!["path", "description", "size"]);
                let listing = git::list_verbose()?;
                for meta in listing {
                    table.add_row(vec![
                        meta.path.display().to_string(),
                        meta.description,
                        format_size(meta.size, BINARY),
                    ]);
                }
            }

            println!("{table}");
        }
        Ok(ShackleCommand::SetDescription(SetDescriptionArgs {
            directory,
            description,
        })) => {
            git::set_description(&directory, &description)?;
            println!("Successfully updated description");
        }
        Ok(ShackleCommand::SetBranch(SetBranchArgs { directory, branch })) => {
            git::set_branch(&directory, &branch)?;
            println!("Successfully updated branch");
        }
        Ok(ShackleCommand::Init(InitArgs {
            repo_name,
            group,
            description,
            branch,
        })) => {
            let init_result = git::init(&repo_name, &group, &description, &branch)?;
            println!("Successfully created \"{}\"", init_result.path.display());
        }
        Ok(ShackleCommand::Delete(DeleteArgs { directory })) => {
            if confirm_risky_action(format!(
                "Are you sure you want to delete \"{}\"?",
                directory.display()
            ))? {
                git::delete(&directory)?;
                println!("Successfully deleted \"{}\"", directory.display());
            } else {
                println!("Action cancelled");
            }
        }
        Ok(ShackleCommand::Housekeeping(HousekeepingArgs { directory })) => match directory {
            Some(directory) => {
                git::housekeeping(&directory)?;
                println!(
                    "Successfully did housekeeping on \"{}\"",
                    directory.display()
                );
            }
            None => {
                let list = git::list()?;
                for repo in list {
                    git::housekeeping(&repo.path)?;
                    println!(
                        "Successfully did housekeeping on \"{}\"",
                        repo.path.display()
                    );
                }
            }
        },
        Ok(ShackleCommand::GitUploadPack(upload_pack_args)) => {
            git::upload_pack(&upload_pack_args)?;
        }
        Ok(ShackleCommand::GitReceivePack(receive_pack_args)) => {
            git::receive_pack(&receive_pack_args)?;
        }
    }
    Ok(ControlFlow::Continue(()))
}

fn confirm_risky_action(prompt: String) -> Result<bool, ShackleError> {
    let mut rl = DefaultEditor::new()?;
    loop {
        let user_input = rl
            .readline(&format!("{} (yes/no) ", prompt))
            .map(|user_input| user_input.to_lowercase())?;

        match user_input.trim() {
            "" => {}
            "yes" => {
                return Ok(true);
            }
            "no" => {
                return Ok(false);
            }
            _ => {
                println!("Please answer 'yes' or 'no'.");
            }
        }
    }
}

#[derive(Error, Debug)]
pub enum ShackleError {
    #[error(transparent)]
    IoError(#[from] io::Error),
    #[error(transparent)]
    NixError(#[from] nix::errno::Errno),
    #[error(transparent)]
    GitError(#[from] git2::Error),
    #[error(transparent)]
    ReadlineError(#[from] ReadlineError),
    #[error("Could not get the current user name")]
    UserReadError,
    #[error("Path is not accessible")]
    InvalidDirectory,
    #[error("Unknown group")]
    InvalidGroup,
}