Skip to main content

git_proc/
config.rs

1use std::path::Path;
2
3use crate::CommandError;
4
5/// Create a new `git config` command builder for getting/setting a key.
6#[must_use]
7pub fn new(key: &str) -> Config<'_> {
8    Config::new(key)
9}
10
11/// Builder for `git config` command.
12///
13/// See `git config --help` for full documentation.
14#[derive(Debug)]
15pub struct Config<'a> {
16    repo_path: Option<&'a Path>,
17    key: &'a str,
18    value: Option<&'a str>,
19}
20
21crate::impl_repo_path!(Config);
22
23impl<'a> Config<'a> {
24    #[must_use]
25    fn new(key: &'a str) -> Self {
26        Self {
27            repo_path: None,
28            key,
29            value: None,
30        }
31    }
32
33    /// Set the value for the configuration key.
34    #[must_use]
35    pub fn value(mut self, value: &'a str) -> Self {
36        self.value = Some(value);
37        self
38    }
39
40    /// Execute the command and return the exit status.
41    pub async fn status(self) -> Result<(), CommandError> {
42        crate::Build::build(self).status().await
43    }
44}
45
46impl crate::Build for Config<'_> {
47    fn build(self) -> cmd_proc::Command {
48        crate::base_command(self.repo_path)
49            .argument("config")
50            .argument(self.key)
51            .optional_argument(self.value)
52    }
53}
54
55#[cfg(feature = "test-utils")]
56impl Config<'_> {
57    /// Compare the built command with another command using debug representation.
58    pub fn test_eq(&self, other: &cmd_proc::Command) {
59        let command = crate::Build::build(Self {
60            repo_path: self.repo_path,
61            key: self.key,
62            value: self.value,
63        });
64        command.test_eq(other);
65    }
66}