Skip to content

Commit

Permalink
refactor: make ksnotify config serializable (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitagry authored Mar 19, 2024
1 parent 358e502 commit 14efcda
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 24 deletions.
3 changes: 2 additions & 1 deletion src/ci.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use anyhow::{Context, Result};
use log::info;
use serde::{Deserialize, Serialize};
use std::env;
use strum_macros::EnumString;

#[derive(Debug, PartialEq, Eq, Clone, Copy, EnumString)]
#[derive(Debug, PartialEq, Eq, Clone, Copy, EnumString, Serialize, Deserialize)]
pub enum CIKind {
/// ksnotify is running on GitLab CI.
#[strum(serialize = "gitlab")]
Expand Down
26 changes: 4 additions & 22 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{ci, notifier};

use anyhow::Result;
use log::info;
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use std::env;
use std::fs;
use std::path::PathBuf;
Expand All @@ -18,7 +18,7 @@ enum NotifierKind {
Slack,
}

#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
pub ci: ci::CIKind,
pub notifier: notifier::NotifierKind,
Expand Down Expand Up @@ -52,27 +52,9 @@ impl Config {
fn from_file(path: PathBuf) -> Result<Self> {
info!("cli arguments are not set, use configuration file");
let config_string = fs::read_to_string(path)?;
let doc: HashMap<String, String> = serde_yaml::from_str(&config_string)?;
let ci = ci::CIKind::from_str(doc.get("ci").expect("failed to load the CI type"))?;
let notifier = notifier::NotifierKind::from_str(
doc.get("notifier")
.expect("failed to load the Notifier type"),
)?;
let suppress_skaffold = doc
.get("suppress_skaffold")
.expect("failed to load the suppress_skaffold flag")
.parse::<bool>()?;
let patch = doc
.get("patch")
.expect("failed to load the patch flag")
.parse::<bool>()?;
let config: Config = serde_yaml::from_str(&config_string)?;

Ok(Self {
ci,
notifier,
suppress_skaffold,
patch,
})
Ok(config)
}

fn from_env() -> Result<Self> {
Expand Down
3 changes: 2 additions & 1 deletion src/notifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ pub mod gitlab;
use crate::template;

use anyhow::Result;
use serde::{Deserialize, Serialize};
use strum_macros::EnumString;

#[derive(Debug, PartialEq, Eq, Clone, Copy, EnumString)]
#[derive(Debug, PartialEq, Eq, Clone, Copy, EnumString, Serialize, Deserialize)]
pub enum NotifierKind {
#[strum(serialize = "gitlab")]
GitLab,
Expand Down

0 comments on commit 14efcda

Please sign in to comment.