entrust_core/
git.rs

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
use anyhow::anyhow;
use std::path::Path;
use std::process::{Command, Stdio};

pub fn init(store: &Path) -> anyhow::Result<()> {
    run_command(
        git().args(["init", "--initial-branch", "main"]),
        store,
        true,
    )?;
    run_command(git().args(["add", "*"]), store, true)?;
    run_command(
        git().args(["commit", "--message", "initialize rp password store repo"]),
        store,
        true,
    )?;
    Ok(())
}

pub fn add(store: &Path, key: &str) -> anyhow::Result<()> {
    if has_repository(store) {
        run_command(
            git().arg("add").arg(store.join(key).as_os_str()),
            store,
            true,
        )?;
        run_command(
            git().args(["commit", "--message", &format!("add {key}")]),
            store,
            true,
        )?;
    }
    Ok(())
}

pub fn edit(store: &Path, key: &str) -> anyhow::Result<()> {
    if has_repository(store) && is_file_tracked(store, key) {
        run_command(
            git().arg("add").arg(store.join(key).as_os_str()),
            store,
            true,
        )?;
        run_command(
            git().args(["commit", "--message", &format!("edit {}", key)]),
            store,
            true,
        )?;
    }
    Ok(())
}

pub fn r#move(store: &Path, from_key: &str, to_key: &str) -> anyhow::Result<bool> {
    if has_repository(store) && is_file_tracked(store, from_key) {
        run_command(
            git()
                .arg("mv")
                .arg(store.join(from_key).as_os_str())
                .arg(store.join(to_key).as_os_str()),
            store,
            true,
        )?;
        run_command(
            git().args([
                "commit",
                "--message",
                &format!("move {} to {}", from_key, to_key),
            ]),
            store,
            true,
        )?;
        Ok(true)
    } else {
        Ok(false)
    }
}

pub fn remove(store: &Path, key: &str) -> anyhow::Result<()> {
    if has_repository(store) && is_file_tracked(store, key) {
        run_command(
            git().arg("rm").arg(store.join(key).as_os_str()),
            store,
            true,
        )?;
        run_command(
            Command::new("git").args(["commit", "--message", &format!("remove {}", key)]),
            store,
            true,
        )?;
    }
    Ok(())
}

fn run_command(command: &mut Command, store: &Path, inherit_io: bool) -> anyhow::Result<()> {
    let stdio = || match inherit_io {
        true => Stdio::inherit(),
        false => Stdio::null(),
    };
    let result = command
        .current_dir(store)
        .stdin(stdio())
        .stdout(stdio())
        .stderr(stdio())
        .status();
    match result {
        Ok(status) if status.success() => Ok(()),
        Ok(status) => Err(anyhow!(
            "git failed with status {}",
            status
                .code()
                .map(|c| c.to_string())
                .unwrap_or("unknown".to_string())
        )),
        Err(err) => Err(err.into()),
    }
}

fn has_repository(store: &Path) -> bool {
    store.join(".git").is_dir()
}

fn is_file_tracked(store: &Path, key: &str) -> bool {
    run_command(
        git()
            .arg("ls-files")
            .arg("--error-unmatch")
            .arg(store.join(key).as_os_str()),
        store,
        false,
    )
    .is_ok()
}

fn git() -> Command {
    Command::new("git")
}