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
21impl<'a> Config<'a> {
22    #[must_use]
23    fn new(key: &'a str) -> Self {
24        Self {
25            repo_path: None,
26            key,
27            value: None,
28        }
29    }
30
31    /// Set the repository path (`-C <path>`).
32    #[must_use]
33    pub fn repo_path(mut self, path: &'a Path) -> Self {
34        self.repo_path = Some(path);
35        self
36    }
37
38    /// Set the value for the configuration key.
39    #[must_use]
40    pub fn value(mut self, value: &'a str) -> Self {
41        self.value = Some(value);
42        self
43    }
44
45    /// Execute the command and return the exit status.
46    pub fn status(self) -> Result<(), CommandError> {
47        self.build().status()
48    }
49
50    /// Execute the command and return stdout as a string (for getting values).
51    #[must_use]
52    pub fn stdout(self) -> cmd_proc::Capture {
53        self.build().stdout()
54    }
55
56    fn build(self) -> cmd_proc::Command {
57        crate::base_command(self.repo_path)
58            .argument("config")
59            .argument(self.key)
60            .optional_argument(self.value)
61    }
62}
63
64#[cfg(feature = "test-utils")]
65impl Config<'_> {
66    /// Compare the built command with another command using debug representation.
67    pub fn test_eq(&self, other: &cmd_proc::Command) {
68        let command = Self {
69            repo_path: self.repo_path,
70            key: self.key,
71            value: self.value,
72        }
73        .build();
74        command.test_eq(other);
75    }
76}