-
Notifications
You must be signed in to change notification settings - Fork 240
Validate snfoundry.toml #3869
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
Open
franciszekjob
wants to merge
11
commits into
master
Choose a base branch
from
3811-fix-parsing-snfoundry-toml
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+94
−45
Open
Validate snfoundry.toml #3869
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7c30c85
Add snfoundry.toml validation
franciszekjob 070e167
Formatting
franciszekjob 4bab61a
Apply code review suggestion
franciszekjob 9919b98
Fix test
franciszekjob 689ce36
Formatting
franciszekjob 606a9c8
Refactor config names
franciszekjob 43e9bca
Apply code review suggestion
franciszekjob d03e7c3
Formatting
franciszekjob 06c902d
Merge branch 'master' of https://github.com/foundry-rs/starknet-found…
franciszekjob 3bd4586
Fix errors
franciszekjob fdde1f7
Formatting
franciszekjob File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,14 @@ | ||
| use anyhow::{Context, Result, anyhow}; | ||
| use camino::Utf8PathBuf; | ||
| use scarb_metadata::{Metadata, PackageId}; | ||
| use serde::de::DeserializeOwned; | ||
| use serde::{Deserialize, Serialize}; | ||
| use serde_json::{Map, Number}; | ||
| use std::collections::HashMap; | ||
| use std::fs::File; | ||
| use std::{env, fs}; | ||
| use tempfile::{TempDir, tempdir}; | ||
| use toml::Value; | ||
|
|
||
| pub const CONFIG_FILENAME: &str = "snfoundry.toml"; | ||
|
|
||
| /// Defined in snfoundry.toml | ||
|
|
@@ -30,6 +33,18 @@ pub trait PackageConfig { | |
| Self: Sized; | ||
| } | ||
|
|
||
| #[derive(Debug, Serialize, Deserialize)] | ||
| struct ConfigSchema<T> { | ||
| #[serde(flatten)] | ||
| pub tools: HashMap<String, ToolProfiles<T>>, | ||
| } | ||
|
|
||
| #[derive(Debug, Serialize, Deserialize)] | ||
| struct ToolProfiles<T> { | ||
| #[serde(flatten)] | ||
| pub profiles: HashMap<String, T>, | ||
| } | ||
|
|
||
| fn get_with_ownership(config: serde_json::Value, key: &str) -> Option<serde_json::Value> { | ||
| match config { | ||
| serde_json::Value::Object(mut map) => map.remove(key), | ||
|
|
@@ -64,31 +79,40 @@ pub fn resolve_config_file() -> Utf8PathBuf { | |
| }) | ||
| } | ||
|
|
||
| pub fn load_config<T: Config + Default>( | ||
| path: Option<&Utf8PathBuf>, | ||
| profile: Option<&str>, | ||
| ) -> Result<T> { | ||
| let config_path = path | ||
| pub fn load_config<T>(path: Option<&Utf8PathBuf>, profile: Option<&str>) -> Result<T> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe rename this something it suggests more it's loading form snfoundry_toml? |
||
| where | ||
| T: Config + Default + Serialize + DeserializeOwned + Clone, | ||
| { | ||
| let path = path | ||
| .as_ref() | ||
| .and_then(|p| search_config_upwards_relative_to(p).ok()) | ||
| .or_else(|| find_config_file().ok()); | ||
|
|
||
| match config_path { | ||
| Some(path) => { | ||
| let raw_config_toml = fs::read_to_string(path) | ||
| .context("Failed to read snfoundry.toml config file")? | ||
| .parse::<Value>() | ||
| .context("Failed to parse snfoundry.toml config file")?; | ||
| let Some(config_path) = path else { | ||
| return Ok(T::default()); | ||
| }; | ||
|
|
||
| let raw_config_json = serde_json::to_value(raw_config_toml) | ||
| .context("Conversion from TOML value to JSON value should not fail.")?; | ||
| let raw = fs::read_to_string(config_path).context("Failed to read snfoundry.toml")?; | ||
| let toml_value: toml::Value = | ||
| toml::from_str(&raw).context("Failed to parse snfoundry.toml config file")?; | ||
| let json_value = serde_json::to_value(toml_value)?; | ||
| let resolved_json = resolve_env_variables(json_value)?; | ||
| let parsed: ConfigSchema<T> = serde_json::from_value(resolved_json) | ||
| .context("Failed to deserialize resolved config into ConfigSchema")?; | ||
| let tool_name = T::tool_name(); | ||
|
|
||
| let profile = get_profile(raw_config_json, T::tool_name(), profile)?; | ||
| T::from_raw(resolve_env_variables(profile)?) | ||
| } | ||
| None => Ok(T::default()), | ||
| } | ||
| let Some(tool_profiles) = parsed.tools.get(tool_name) else { | ||
| return Ok(T::default()); | ||
| }; | ||
|
|
||
| let profile_name = profile.unwrap_or("default"); | ||
| let Some(profile_config) = tool_profiles.profiles.get(profile_name) else { | ||
| return Err(anyhow!("Profile [{profile_name}] not found in config")); | ||
| }; | ||
|
|
||
| Ok(profile_config.clone()) | ||
| } | ||
|
|
||
| /// Loads config for a specific package from the `Scarb.toml` file | ||
| /// # Arguments | ||
| /// * `metadata` - Scarb metadata object | ||
|
|
@@ -252,7 +276,7 @@ mod tests { | |
| ); | ||
| } | ||
|
|
||
| #[derive(Debug, Default, Serialize, Deserialize)] | ||
| #[derive(Debug, Default, Serialize, Deserialize, Clone)] | ||
| pub struct StubConfig { | ||
| #[serde(default)] | ||
| pub url: String, | ||
|
|
@@ -305,7 +329,7 @@ mod tests { | |
| assert_eq!(config.url, String::new()); | ||
| } | ||
|
|
||
| #[derive(Debug, Default, Serialize, Deserialize)] | ||
| #[derive(Debug, Default, Serialize, Deserialize, Clone)] | ||
| pub struct StubComplexConfig { | ||
| #[serde(default)] | ||
| pub url: String, | ||
|
|
@@ -315,7 +339,7 @@ mod tests { | |
| pub nested: StubComplexConfigNested, | ||
| } | ||
|
|
||
| #[derive(Debug, Default, Serialize, Deserialize)] | ||
| #[derive(Debug, Default, Serialize, Deserialize, Clone)] | ||
| pub struct StubComplexConfigNested { | ||
| #[serde( | ||
| default, | ||
|
|
@@ -353,11 +377,13 @@ mod tests { | |
| #[test] | ||
| #[expect(clippy::float_cmp)] | ||
| fn resolve_env_vars() { | ||
| let tempdir = | ||
| copy_config_to_tempdir("tests/data/stubtool_snfoundry.toml", Some("childdir1")) | ||
| .unwrap(); | ||
| let tempdir = copy_config_to_tempdir( | ||
| "tests/data/stubtool_complex_snfoundry.toml", | ||
| Some("childdir1"), | ||
| ) | ||
| .unwrap(); | ||
| fs::copy( | ||
| "tests/data/stubtool_snfoundry.toml", | ||
| "tests/data/stubtool_complex_snfoundry.toml", | ||
| tempdir.path().join("childdir1").join(CONFIG_FILENAME), | ||
| ) | ||
| .expect("Failed to copy config file to temp dir"); | ||
|
|
||
8 changes: 8 additions & 0 deletions
8
crates/configuration/tests/data/stubtool_complex_snfoundry.toml
franciszekjob marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| [stubtool.with-envs] | ||
| url = "$VALUE_STRING123132" | ||
| account = "$VALUE_INT123132" | ||
|
|
||
| [stubtool.with-envs.nested] | ||
| list-example = [ "$VALUE_BOOL1231321", "$VALUE_BOOL1231322" ] | ||
| url-nested = "$VALUE_FLOAT123132" | ||
| url-alt = "${VALUE_STRING123142}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.