Skip to main content

git_proc/
config.rs

1/// Create a new `git config` command builder for getting/setting a key.
2#[must_use]
3pub fn new(key: &str) -> Config<'_> {
4    Config::new(key)
5}
6
7/// Create a `git config --list` command builder that lists all configuration.
8#[must_use]
9pub fn list() -> List<'static> {
10    List::new()
11}
12
13/// Builder for `git config` command.
14///
15/// See `git config --help` for full documentation.
16#[derive(Debug)]
17pub struct Config<'a> {
18    base: crate::RepoBase<'a>,
19    key: &'a str,
20    value: Option<&'a str>,
21}
22
23crate::impl_repo_base!(Config);
24
25impl<'a> Config<'a> {
26    #[must_use]
27    fn new(key: &'a str) -> Self {
28        Self {
29            base: crate::RepoBase::default(),
30            key,
31            value: None,
32        }
33    }
34
35    /// Set the value for the configuration key.
36    #[must_use]
37    pub fn value(mut self, value: &'a str) -> Self {
38        self.value = Some(value);
39        self
40    }
41
42    /// Execute the command and return the exit status.
43    pub async fn status(self) -> Result<(), crate::Error> {
44        Ok(crate::Build::build(self)?.status().await?)
45    }
46}
47
48impl crate::Build for Config<'_> {
49    fn build(self) -> Result<cmd_proc::Command, crate::EnvError> {
50        Ok(self
51            .base
52            .command()?
53            .argument("config")
54            .argument(self.key)
55            .optional_argument(self.value))
56    }
57}
58
59#[cfg(feature = "test-utils")]
60impl Config<'_> {
61    /// Compare the built command with another command using debug representation.
62    pub fn test_eq(&self, other: &cmd_proc::Command) {
63        let command = crate::Build::build(Self {
64            base: self.base,
65            key: self.key,
66            value: self.value,
67        })
68        .unwrap();
69        command.test_eq(other);
70    }
71}
72
73/// Builder for `git config --list`.
74///
75/// See `git config --help` for full documentation.
76#[derive(Debug)]
77pub struct List<'a> {
78    base: crate::RepoBase<'a>,
79}
80
81crate::impl_repo_base!(List);
82
83impl<'a> List<'a> {
84    #[must_use]
85    fn new() -> Self {
86        Self {
87            base: crate::RepoBase::default(),
88        }
89    }
90}
91
92impl Default for List<'_> {
93    fn default() -> Self {
94        Self::new()
95    }
96}
97
98impl crate::Build for List<'_> {
99    fn build(self) -> Result<cmd_proc::Command, crate::EnvError> {
100        Ok(self.base.command()?.argument("config").argument("--list"))
101    }
102}
103
104#[cfg(feature = "test-utils")]
105impl List<'_> {
106    /// Compare the built command with another command using debug representation.
107    pub fn test_eq(&self, other: &cmd_proc::Command) {
108        let command = crate::Build::build(Self { base: self.base }).unwrap();
109        command.test_eq(other);
110    }
111}
112
113#[cfg(all(test, feature = "test-utils"))]
114mod tests {
115    use super::*;
116
117    #[test]
118    fn test_config_list_command() {
119        let expected = cmd_proc::Command::new("git")
120            .argument("config")
121            .argument("--list");
122
123        list().test_eq(&expected);
124    }
125}