-
Notifications
You must be signed in to change notification settings - Fork 253
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
add autorust.toml with tags_allow to limit tags #747
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,4 @@ once_cell = "1.7" | |
syn = { version = "1.0", features = ["parsing"] } | ||
camino = "1.0" | ||
askama = "0.11" | ||
toml = "0.5" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use crate::config_parser::Tag; | ||
use camino::{Utf8Path, Utf8PathBuf}; | ||
use serde::Deserialize; | ||
use std::{collections::HashSet, fs}; | ||
|
||
#[derive(Deserialize, Debug, Default)] | ||
pub struct PackageConfig { | ||
pub tags_allow: Vec<String>, | ||
} | ||
impl<'a> PackageConfig { | ||
pub fn tags(&self, tags: Vec<&'a Tag>) -> Vec<&'a Tag> { | ||
if self.tags_allow.is_empty() { | ||
tags | ||
} else { | ||
let tags_allow: HashSet<&str> = self.tags_allow.iter().map(String::as_str).collect(); | ||
tags.into_iter().filter(|tag| tags_allow.contains(tag.name())).collect() | ||
} | ||
} | ||
} | ||
|
||
pub type Result<T, E = Error> = std::result::Result<T, E>; | ||
#[derive(Debug, thiserror::Error)] | ||
pub enum Error { | ||
#[error("Failed to deserialize autorust.toml")] | ||
Deserialize(#[from] toml::de::Error), | ||
#[error(transparent)] | ||
Io(#[from] crate::io::Error), | ||
} | ||
|
||
/// Deserializes the autorust.toml into a PackageConfig | ||
/// If the file does not exist, then returns a default instance | ||
pub fn read(path: &Utf8Path) -> Result<PackageConfig> { | ||
if path.exists() { | ||
let bytes = fs::read(path).map_err(|source| crate::io::Error::ReadFile { | ||
source, | ||
file: Utf8PathBuf::from(path), | ||
})?; | ||
Ok(toml::from_slice(&bytes)?) | ||
} else { | ||
Ok(PackageConfig::default()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_tags_allow() -> Result<(), Error> { | ||
let s = r#"tags_allow = ["package-2021-08", "package-2021-05"]"#; | ||
let config: PackageConfig = toml::from_str(s)?; | ||
assert_eq!(2, config.tags_allow.len()); | ||
assert_eq!("package-2021-08", config.tags_allow[0]); | ||
assert_eq!("package-2021-05", config.tags_allow[1]); | ||
Ok(()) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
tags_allow = ["package-2021-08", "package-2021-05"] | ||
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. This is limiting the tags for azure_mgmt_network to two of the most recent stable tags. 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. So no root section is necessary? Should we have one to avoid future issues, or likely not a problem? FWIW, our autorest tooling makes heavy use (required, really) of configuration so I definitely could see adding more in the future here...assuming we don't eventually adopt autorest instead. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good to cover the case where no autorest.toml exists and the passed tags are returned back. That'll happen far more frequently.