-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.rs
212 lines (196 loc) · 7.34 KB
/
config.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use crate::{Github, Gitlab, Source, SourceMap};
use figment::{
providers::{Format, Serialized, Toml},
Figment,
};
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
env,
path::{Path, PathBuf},
};
/// The main configuration.
#[derive(Debug, Deserialize, Serialize, PartialEq)]
pub struct Config {
allowed_signers: Option<PathBuf>,
pub users: Option<Vec<User>>,
local: Option<Vec<String>>,
sources: Vec<SourceConfiguration>,
}
impl Default for Config {
/// The default configuration containing common sources as well as the location of the allowed
/// signers file if it is configured within Git.
fn default() -> Self {
Config {
allowed_signers: git_allowed_signers(),
users: None,
local: None,
sources: vec![
SourceConfiguration {
name: "github".to_string(),
provider: SourceType::Github,
url: "https://api.github.com".to_string(),
},
SourceConfiguration {
name: "gitlab".to_string(),
provider: SourceType::Gitlab,
url: "https://gitlab.com".to_string(),
},
],
}
}
}
impl Config {
/// Get the configured sources.
#[must_use]
pub fn get_sources(&self) -> SourceMap {
self.sources
.iter()
.map(|source_config| {
let name = source_config.name.clone();
let source: Box<dyn Source> = match source_config.provider {
SourceType::Github => Box::new(Github::new(source_config.url.parse().unwrap())),
SourceType::Gitlab => Box::new(Gitlab::new(source_config.url.parse().unwrap())),
};
(name, source)
})
.collect()
}
/// Load the configuration from a TOML file, using defaults for values that were not provided.
pub fn load(path: &Path) -> figment::Result<Self> {
Figment::from(Serialized::defaults(Config::default()))
.admerge(Toml::file(path))
.extract()
}
/// Load the configuration from a figment provider without using any defaults.
#[cfg(test)]
fn _load_from_provider(provider: impl figment::Provider) -> figment::Result<Self> {
Figment::from(provider).extract()
}
/// Save the configuration.
fn save(&self) -> Result<(), Box<dyn std::error::Error>> {
todo!("Save the configuration while preserving formatting.");
}
}
/// The path to the allowed signers file as configured within Git. If an error occurs while
/// retrieving the path, `None` is returned.
fn git_allowed_signers() -> Option<PathBuf> {
let file = gix_config::File::from_globals().ok()?;
let path = file
.path("gpg", Some("ssh".into()), "allowedsignersfile")?
.interpolate(gix_config::path::interpolate::Context {
home_dir: env::var("HOME")
.ok()
.map(std::convert::Into::<PathBuf>::into)
.as_deref(),
..Default::default()
})
.ok()?;
Some(path.into())
}
/// The type of source.
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, clap::ValueEnum)]
#[serde(rename_all = "lowercase")]
pub enum SourceType {
Github,
Gitlab,
}
#[derive(Debug, Deserialize, Serialize, PartialEq)]
pub struct User {
pub name: String,
pub principals: Vec<String>,
pub sources: Vec<String>,
}
/// The representation of a [`Source`] in configuration.
#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct SourceConfiguration {
name: String,
provider: SourceType,
url: String,
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
/// The example configuration is rendered correctly.
#[test]
fn example_config() {
let toml = indoc! {r#"
users = [
{ name = "torvalds", principals = ["torvalds@linux-foundation.org"], sources = ["github"] },
{ name = "gvanrossum", principals = ["guido@python.org"], sources = ["github", "gitlab"] },
{ name = "graydon", principals = ["graydon@pobox.com"], sources = ["github"] },
{ name = "cwoods", principals = ["cwoods@acme.corp"], sources = ["acme-corp"] },
{ name = "rdavis", principals = ["rdavis@acme.corp"], sources = ["acme-corp"] },
{ name = "pbrock", principals = ["pbrock@acme.corp"], sources = ["acme-corp"] }
]
local = [
"jdoe@example.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJHDGMF+tZQL3dcr1arPst+YP8v33Is0kAJVvyTKrxMw"
]
[[sources]]
name = "acme-corp"
provider = "gitlab"
url = "https://git.acme.corp"
"#};
let expected = Config {
allowed_signers: None,
users: Some(vec![
User {
name: "torvalds".to_string(),
principals: vec!["torvalds@linux-foundation.org".to_string()],
sources: vec!["github".to_string()],
},
User {
name: "gvanrossum".to_string(),
principals: vec!["guido@python.org".to_string()],
sources: vec!["github".to_string(), "gitlab".to_string()],
},
User {
name: "graydon".to_string(),
principals: vec!["graydon@pobox.com".to_string()],
sources: vec!["github".to_string()],
},
User {
name: "cwoods".to_string(),
principals: vec!["cwoods@acme.corp".to_string()],
sources: vec!["acme-corp".to_string()],
},
User {
name: "rdavis".to_string(),
principals: vec!["rdavis@acme.corp".to_string()],
sources: vec!["acme-corp".to_string()],
},
User {
name: "pbrock".to_string(),
principals: vec!["pbrock@acme.corp".to_string()],
sources: vec!["acme-corp".to_string()],
},
]),
local: Some(vec!["jdoe@example.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJHDGMF+tZQL3dcr1arPst+YP8v33Is0kAJVvyTKrxMw".parse().unwrap()]),
sources: vec![
SourceConfiguration {
name: "acme-corp".to_string(),
provider: SourceType::Gitlab,
url: "https://git.acme.corp".to_string(),
}
]
};
let config = Config::_load_from_provider(Toml::string(toml)).unwrap();
assert_eq!(config, expected);
}
/// The default configuration contains the default GitHub and GitLab sources.
#[test]
fn default_configuration_contains_default_sources() {
let default_sources = Config::default().sources;
assert!(default_sources.contains(&SourceConfiguration {
name: "github".to_string(),
provider: SourceType::Github,
url: "https://api.github.com".to_string(),
}));
assert!(default_sources.contains(&SourceConfiguration {
name: "gitlab".to_string(),
provider: SourceType::Gitlab,
url: "https://gitlab.com".to_string(),
}));
}
}