Encapsulate fallback in OmConfig

This commit is contained in:
shivaraj-bh 2024-11-26 13:43:10 +05:30
parent be6de96c4d
commit 1a4916efe4
6 changed files with 29 additions and 26 deletions

View File

@ -2,7 +2,7 @@
use clap::Subcommand;
use colored::Colorize;
use nix_rs::command::NixCmd;
use omnix_common::config::{OmConfig, OmConfigError};
use omnix_common::config::OmConfig;
use tracing::instrument;
use crate::flake_ref::FlakeRef;
@ -39,10 +39,8 @@ impl Command {
pub async fn run(self, nixcmd: &NixCmd, verbose: bool) -> anyhow::Result<()> {
tracing::info!("{}", "\n👟 Reading om.ci config from flake".bold());
let url = self.get_flake_ref().to_flake_url().await?;
let cfg = match OmConfig::from_yaml(nixcmd, &url).await {
Err(OmConfigError::ReadYaml(_)) => OmConfig::from_flake(nixcmd, &url).await,
other => other,
}?;
let cfg = OmConfig::get(nixcmd, &url).await?;
tracing::debug!("OmConfig: {cfg:?}");
match self {
Command::Run(cmd) => cmd.run(nixcmd, verbose, cfg).await,

View File

@ -15,7 +15,7 @@ mod tests {
"github:srid/haskell-flake/76214cf8b0d77ed763d1f093ddce16febaf07365#default.dev"
.to_string(),
);
let cfg = OmConfig::from_flake(&NixCmd::default(), url).await.unwrap();
let cfg = OmConfig::get(&NixCmd::default(), url).await.unwrap();
let (config, attrs) = cfg.get_sub_config_under::<SubflakesConfig>("ci").unwrap();
assert_eq!(attrs, &["dev"]);
// assert_eq!(cfg.selected_subconfig, Some("dev".to_string()));

View File

@ -1,6 +1,6 @@
use clap::Parser;
use nix_rs::{command::NixCmd, flake::url::FlakeUrl};
use omnix_common::config::{OmConfig, OmConfigError};
use omnix_common::config::OmConfig;
/// Prepare to develop on a flake project
#[derive(Parser, Debug)]
@ -28,11 +28,7 @@ impl DevelopCommand {
pub async fn run(&self) -> anyhow::Result<()> {
let flake = self.flake_shell.without_attr();
let nix_cmd = NixCmd::get().await;
let om_config = match OmConfig::from_yaml(nix_cmd, &flake).await {
Err(OmConfigError::ReadYaml(_)) => OmConfig::from_flake(nix_cmd, &flake).await,
other => other,
}?;
let om_config = OmConfig::get(NixCmd::get().await, &flake).await?;
tracing::info!("⌨️ Preparing to develop project: {:}", &flake);
let prj = omnix_develop::core::Project::new(flake, om_config).await?;

View File

@ -1,6 +1,6 @@
//! Manage omnix configuration in flake.nix
use std::collections::BTreeMap;
use std::{collections::BTreeMap, path::PathBuf};
use nix_rs::{
command::NixCmd,
@ -24,8 +24,16 @@ pub struct OmConfig {
}
impl OmConfig {
/// Fetch the `om` configuration from `om.yaml` if present, falling back to `om` config in flake output
pub async fn get(cmd: &NixCmd, flake_url: &FlakeUrl) -> Result<Self, OmConfigError> {
match Self::from_yaml(cmd, flake_url).await {
Err(OmConfigError::YamlNotFound(_)) => Self::from_flake(cmd, flake_url).await,
other => other,
}
}
/// Read the configuration from `om.yaml` in flake root
pub async fn from_yaml(cmd: &NixCmd, flake_url: &FlakeUrl) -> Result<Self, OmConfigError> {
async fn from_yaml(cmd: &NixCmd, flake_url: &FlakeUrl) -> Result<Self, OmConfigError> {
let path = if let Some(local_path) = flake_url.without_attr().as_local_path() {
local_path.to_path_buf()
} else {
@ -33,6 +41,10 @@ impl OmConfig {
}
.join("om.yaml");
if !path.exists() {
return Err(OmConfigError::YamlNotFound(path));
}
let yaml_str = std::fs::read_to_string(path)?;
let config: OmConfigTree = serde_yaml::from_str(&yaml_str)?;
Ok(OmConfig {
@ -43,7 +55,7 @@ impl OmConfig {
}
/// Read the configuration from `om` flake output
pub async fn from_flake(cmd: &NixCmd, flake_url: &FlakeUrl) -> Result<Self, OmConfigError> {
async fn from_flake(cmd: &NixCmd, flake_url: &FlakeUrl) -> Result<Self, OmConfigError> {
Ok(OmConfig {
flake_url: flake_url.without_attr(),
reference: flake_url.get_attr().as_list(),
@ -127,6 +139,10 @@ pub enum OmConfigError {
#[error("Failed to decode (json error): {0}")]
DecodeErrorJson(#[from] serde_json::Error),
/// Yaml config not found
#[error("{0} YAML config does not exist")]
YamlNotFound(PathBuf),
/// Failed to parse yaml
#[error("Failed to parse yaml: {0}")]
ParseYaml(#[from] serde_yaml::Error),

View File

@ -109,11 +109,7 @@ pub async fn run_all_checks_with(flake_url: Option<FlakeUrl>) -> anyhow::Result<
let health: NixHealth = match flake_url.as_ref() {
Some(flake_url) => {
let nix_cmd = NixCmd::get().await;
let om_config = match OmConfig::from_yaml(nix_cmd, flake_url).await {
Err(OmConfigError::ReadYaml(_)) => OmConfig::from_flake(nix_cmd, flake_url).await,
other => other,
}?;
let om_config = OmConfig::get(NixCmd::get().await, flake_url).await?;
NixHealth::from_om_config(&om_config)
}
None => Ok(NixHealth::default()),

View File

@ -2,7 +2,7 @@ use std::fmt::{self, Display, Formatter};
use colored::Colorize;
use nix_rs::{command::NixCmd, flake::url::FlakeUrl};
use omnix_common::config::{OmConfig, OmConfigError};
use omnix_common::config::OmConfig;
use crate::template::Template;
@ -33,11 +33,8 @@ impl<'a> Display for FlakeTemplate<'a> {
/// Load templates from the given flake
pub async fn load_templates<'a>(url: &FlakeUrl) -> anyhow::Result<Vec<FlakeTemplate>> {
let nix_cmd = NixCmd::get().await;
let om_config = match OmConfig::from_yaml(nix_cmd, url).await {
Err(OmConfigError::ReadYaml(_)) => OmConfig::from_flake(nix_cmd, url).await,
other => other,
}?;
let om_config = OmConfig::get(NixCmd::get().await, url).await?;
let templates = om_config
.config
.get::<Template>("templates")?