Compare commits

...

4 commits

Author SHA1 Message Date
a1484939ab config(server): add token field to Forge
All checks were successful
ci/woodpecker/push/docker Pipeline was successful
ci/woodpecker/push/release Pipeline was successful
ci/woodpecker/push/todo-check Pipeline was successful
ci/woodpecker/push/build Pipeline was successful
2024-04-08 12:09:29 +01:00
aaaa975911 refactor(config): compare whole server config in one go 2024-04-08 12:09:29 +01:00
be01d106a4 feat(config): use accesors for hostname and user 2024-04-08 12:09:29 +01:00
3c4a0d52b1 feat(config): Add ApiToken newtype 2024-04-08 11:50:10 +01:00
3 changed files with 50 additions and 26 deletions

View file

@ -30,7 +30,7 @@ actix-rt = "2.9"
[dev-dependencies]
# Testing
assert2 = "0.3"
# assert2 = "0.3"
test-log = "0.2"
anyhow = "1.0"

View file

@ -2,7 +2,7 @@
forge_type = "ForgeJo"
hostname = "git.example.net"
user = "git-next" # the user to perform actions as
# API token for user?
token = "API-Token"
# path to private SSH key for user?
[forge.default.repos]

View file

@ -47,6 +47,7 @@ pub struct Forge {
forge_type: ForgeType,
hostname: String,
user: String,
token: String,
// API Token
// Private SSH Key Path
repos: HashMap<String, Repo>,
@ -64,6 +65,10 @@ impl Forge {
User(self.user.clone())
}
pub fn token(&self) -> ApiToken {
ApiToken(self.token.clone())
}
pub fn repos(&self) -> impl Iterator<Item = (RepoName, &Repo)> {
self.repos
.iter()
@ -77,13 +82,6 @@ pub struct Repo {
branch: String,
}
impl Repo {
#[cfg(test)]
pub fn new(repo: &str, branch: &str) -> Self {
Self {
repo: repo.to_string(),
branch: branch.to_string(),
}
}
#[allow(dead_code)]
pub fn repo(&self) -> RepoPath {
RepoPath(self.repo.clone())
@ -127,11 +125,19 @@ impl Display for User {
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ApiToken(pub String);
impl Display for ApiToken {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ForgeDetails {
pub name: ForgeName,
pub forge_type: ForgeType,
pub hostname: Hostname,
pub user: User,
pub token: ApiToken,
// API Token
// Private SSH Key Path
}
@ -140,8 +146,9 @@ impl From<(&ForgeName, &Forge)> for ForgeDetails {
Self {
name: forge.0.clone(),
forge_type: forge.1.forge_type.clone(),
hostname: Hostname(forge.1.hostname.clone()),
user: User(forge.1.user.clone()),
hostname: forge.1.hostname(),
user: forge.1.user(),
token: forge.1.token(),
}
}
}
@ -182,8 +189,9 @@ impl RepoDetails {
forge: ForgeDetails {
name: forge_name.clone(),
forge_type: forge.forge_type.clone(),
hostname: Hostname(forge.hostname.clone()),
user: User(forge.user.clone()),
hostname: forge.hostname(),
user: forge.user(),
token: forge.token(),
},
}
}
@ -220,7 +228,6 @@ impl Display for ForgeType {
#[cfg(test)]
mod tests {
use assert2::let_assert;
use crate::filesystem::FileSystem;
@ -236,6 +243,7 @@ mod tests {
forge_type = "ForgeJo"
hostname = "git.example.net"
user = "Bob"
token = "API-Token"
[forge.default.repos]
hello = { repo = "user/hello", branch = "main" }
@ -244,18 +252,34 @@ mod tests {
)
.map_err(OneOf::new)?;
let config = ServerConfig::load(&fs)?;
let_assert!(Some(default) = config.forge.get("default"));
assert_eq!(default.forge_type, ForgeType::ForgeJo);
assert_eq!(default.hostname, "git.example.net".to_string());
assert_eq!(default.user, "Bob".to_string());
assert_eq!(
default.repos.get("hello"),
Some(Repo::new("user/hello", "main").as_ref())
);
assert_eq!(
default.repos.get("world"),
Some(Repo::new("user/world", "master").as_ref())
);
let expected = ServerConfig {
forge: HashMap::from([(
"default".to_string(),
Forge {
forge_type: ForgeType::ForgeJo,
hostname: "git.example.net".to_string(),
user: "Bob".to_string(),
token: "API-Token".to_string(),
repos: HashMap::from([
(
"hello".to_string(),
Repo {
repo: "user/hello".to_string(),
branch: "main".to_string(),
},
),
(
"world".to_string(),
Repo {
repo: "user/world".to_string(),
branch: "master".to_string(),
},
),
]),
},
)]),
};
assert_eq!(config, expected);
Ok(())
}