Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement get_sources on config #23

Merged
merged 2 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/cli/update.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! The update subcommand used to get the latest allowed signers and write them to the output file.
use crate::{AllowedSignersEntry, Config, Source, SshPublicKey};
use std::collections::{HashMap, HashSet};
use crate::{AllowedSignersEntry, Config, SourceMap, SshPublicKey};
use std::collections::HashSet;

pub(super) fn update(config: Config) {
let sources = config.get_sources();
Expand All @@ -16,6 +16,6 @@ pub(super) fn update(config: Config) {
}
}

fn get_public_keys(user: (), sources: &HashMap<String, Box<dyn Source>>) -> Vec<SshPublicKey> {
fn get_public_keys(user: (), sources: &SourceMap) -> Vec<SshPublicKey> {
todo!("Retrieve public keys for a user from all sources.");
}
16 changes: 13 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Source;
use crate::{Github, Gitlab, Source, SourceMap};
use figment::{
providers::{Format, Serialized, Toml},
Figment,
Expand Down Expand Up @@ -46,8 +46,18 @@ impl Default for Config {
impl Config {
/// Get the configured sources.
#[must_use]
pub fn get_sources(&self) -> HashMap<String, Box<dyn Source>> {
todo!()
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.
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
pub use config::Config;
pub use core::*;
pub use signer::{AllowedSignersEntry, AllowedSignersFile};
pub use source::Source;
pub use source::{Github, Gitlab, Source, SourceMap};

pub mod cli;
mod config;
Expand Down
4 changes: 4 additions & 0 deletions src/source/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::SshPublicKey;
use async_trait::async_trait;
use std::collections::HashMap;

/// A source implements a way to get public keys from a Git provider.
#[async_trait]
Expand All @@ -11,3 +12,6 @@ pub trait Source {
client: &reqwest::Client,
) -> Result<Vec<SshPublicKey>, reqwest::Error>;
}

/// A `HashMap` containing named sources.
pub type SourceMap = HashMap<String, Box<dyn Source>>;
4 changes: 3 additions & 1 deletion src/source/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pub use main::Source;
pub use github::Github;
pub use gitlab::Gitlab;
pub use main::{Source, SourceMap};

mod github;
mod gitlab;
Expand Down