gix_config/file/init/
from_env.rs

1use std::convert::TryFrom;
2
3use bstr::ByteSlice;
4
5use crate::{file, file::init, parse::section, path::interpolate, File, KeyRef};
6
7/// Represents the errors that may occur when calling [`File::from_env()`].
8#[derive(Debug, thiserror::Error)]
9#[allow(missing_docs)]
10pub enum Error {
11    #[error("Configuration {kind} at index {index} contained illformed UTF-8")]
12    IllformedUtf8 { index: usize, kind: &'static str },
13    #[error("GIT_CONFIG_COUNT was not a positive integer: {}", .input)]
14    InvalidConfigCount { input: String },
15    #[error("GIT_CONFIG_KEY_{} was not set", .key_id)]
16    InvalidKeyId { key_id: usize },
17    #[error("GIT_CONFIG_KEY_{} was set to an invalid value: {}", .key_id, .key_val)]
18    InvalidKeyValue { key_id: usize, key_val: String },
19    #[error("GIT_CONFIG_VALUE_{} was not set", .value_id)]
20    InvalidValueId { value_id: usize },
21    #[error(transparent)]
22    PathInterpolationError(#[from] interpolate::Error),
23    #[error(transparent)]
24    Includes(#[from] init::includes::Error),
25    #[error(transparent)]
26    Section(#[from] section::header::Error),
27    #[error(transparent)]
28    ValueName(#[from] section::value_name::Error),
29}
30
31/// Instantiation from environment variables
32impl File<'static> {
33    /// Generates a config from `GIT_CONFIG_*` environment variables or returns `Ok(None)` if no configuration was found.
34    /// See [`git-config`'s documentation] for more information on the environment variables in question.
35    ///
36    /// With `options` configured, it's possible to resolve `include.path` or `includeIf.<condition>.path` directives as well.
37    ///
38    /// [`git-config`'s documentation]: https://git-scm.com/docs/git-config#Documentation/git-config.txt-GITCONFIGCOUNT
39    pub fn from_env(options: init::Options<'_>) -> Result<Option<File<'static>>, Error> {
40        use std::env;
41        let count: usize = match env::var("GIT_CONFIG_COUNT") {
42            Ok(v) => v.parse().map_err(|_| Error::InvalidConfigCount { input: v })?,
43            Err(_) => return Ok(None),
44        };
45
46        if count == 0 {
47            return Ok(None);
48        }
49
50        let meta = file::Metadata {
51            path: None,
52            source: crate::Source::Env,
53            level: 0,
54            trust: gix_sec::Trust::Full,
55        };
56        let mut config = File::new(meta);
57        for i in 0..count {
58            let key = gix_path::os_string_into_bstring(
59                env::var_os(format!("GIT_CONFIG_KEY_{i}")).ok_or(Error::InvalidKeyId { key_id: i })?,
60            )
61            .map_err(|_| Error::IllformedUtf8 { index: i, kind: "key" })?;
62            let value = env::var_os(format!("GIT_CONFIG_VALUE_{i}")).ok_or(Error::InvalidValueId { value_id: i })?;
63            let key = KeyRef::parse_unvalidated(key.as_ref()).ok_or_else(|| Error::InvalidKeyValue {
64                key_id: i,
65                key_val: key.to_string(),
66            })?;
67
68            config
69                .section_mut_or_create_new(key.section_name, key.subsection_name)?
70                .push(
71                    section::ValueName::try_from(key.value_name.to_owned())?,
72                    Some(
73                        gix_path::os_str_into_bstr(&value)
74                            .map_err(|_| Error::IllformedUtf8 {
75                                index: i,
76                                kind: "value",
77                            })?
78                            .as_bytes()
79                            .into(),
80                    ),
81                );
82        }
83
84        let mut buf = Vec::new();
85        init::includes::resolve(&mut config, &mut buf, options)?;
86        Ok(Some(config))
87    }
88}