Skip to content

Commit

Permalink
Merge pull request #139 from DeterminateSystems/sort-allowed-refs
Browse files Browse the repository at this point in the history
  • Loading branch information
lucperkins authored Oct 22, 2024
2 parents 2b2a7c8 + 9353a40 commit 9d532f9
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 64 deletions.
6 changes: 3 additions & 3 deletions allowed-refs.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[
"nixpkgs-24.05-darwin",
"nixos-24.05",
"nixpkgs-unstable",
"nixos-24.05-small",
"nixos-unstable",
"nixos-unstable-small",
"nixos-unstable"
"nixpkgs-24.05-darwin",
"nixpkgs-unstable"
]
16 changes: 8 additions & 8 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions src/allowed_refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ struct Metric {
current: String,
}

pub(crate) fn check(allowed_refs: Vec<String>) -> Result<bool, FlakeCheckerError> {
Ok(get()? == allowed_refs)
pub(crate) fn check_allowed_refs(allowed_refs: Vec<String>) -> Result<bool, FlakeCheckerError> {
Ok(fetch_allowed_refs()? == allowed_refs)
}

pub(crate) fn get() -> Result<Vec<String>, FlakeCheckerError> {
let officially_supported: Vec<String> = reqwest::blocking::get(ALLOWED_REFS_URL)?
pub(crate) fn fetch_allowed_refs() -> Result<Vec<String>, FlakeCheckerError> {
let mut officially_supported: Vec<String> = reqwest::blocking::get(ALLOWED_REFS_URL)?
.json::<Response>()?
.data
.result
Expand All @@ -39,5 +39,7 @@ pub(crate) fn get() -> Result<Vec<String>, FlakeCheckerError> {
.map(|res| res.metric.channel.clone())
.collect();

officially_supported.sort();

Ok(officially_supported)
}
117 changes: 68 additions & 49 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#[cfg(feature = "allowed-refs")]
mod allowed_refs;
mod condition;
mod error;
mod flake;
mod issue;
mod summary;
mod telemetry;

#[cfg(feature = "allowed-refs")]
mod allowed_refs;

use error::FlakeCheckerError;
use flake::{check_flake_lock, FlakeCheckConfig};
use summary::Summary;
Expand All @@ -20,6 +21,7 @@ use parse_flake_lock::FlakeLock;
use crate::condition::evaluate_condition;

/// A flake.lock checker for Nix projects.
#[cfg(not(feature = "allowed-refs"))]
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
Expand Down Expand Up @@ -92,18 +94,9 @@ struct Cli {
/// The Common Expression Language (CEL) policy to apply to each Nixpkgs input.
#[arg(long, short, env = "NIX_FLAKE_CHECKER_CONDITION")]
condition: Option<String>,

#[cfg(feature = "allowed-refs")]
// Check to make sure that Flake Checker is aware of the current supported branches.
#[arg(long, hide = true)]
check_allowed_refs: bool,

#[cfg(feature = "allowed-refs")]
// Check to make sure that Flake Checker is aware of the current supported branches.
#[arg(long, hide = true)]
get_allowed_refs: bool,
}

#[cfg(not(feature = "allowed-refs"))]
fn main() -> Result<ExitCode, FlakeCheckerError> {
let allowed_refs: Vec<String> =
serde_json::from_str(include_str!("../allowed-refs.json")).unwrap();
Expand All @@ -119,44 +112,7 @@ fn main() -> Result<ExitCode, FlakeCheckerError> {
nixpkgs_keys,
markdown_summary,
condition,
#[cfg(feature = "allowed-refs")]
check_allowed_refs,
#[cfg(feature = "allowed-refs")]
get_allowed_refs,
} = Cli::parse();
#[cfg(feature = "allowed-refs")]
if get_allowed_refs {
match allowed_refs::get() {
Ok(refs) => {
let json_refs = serde_json::to_string(&refs)?;
println!("{json_refs}");
return Ok(ExitCode::SUCCESS);
}
Err(e) => {
println!("Error fetching allowed refs: {}", e);
return Ok(ExitCode::FAILURE);
}
}
}

#[cfg(feature = "allowed-refs")]
if check_allowed_refs {
match allowed_refs::check(allowed_refs) {
Ok(equals) => {
if equals {
println!("The allowed reference sets are up to date.");
return Ok(ExitCode::SUCCESS);
} else {
println!("The allowed reference sets are NOT up to date. Make sure to update.");
return Ok(ExitCode::FAILURE);
}
}
Err(e) => {
println!("Error checking allowed refs: {}", e);
return Ok(ExitCode::FAILURE);
}
}
}

if !flake_lock_path.exists() {
if ignore_missing_flake_lock {
Expand Down Expand Up @@ -211,3 +167,66 @@ fn main() -> Result<ExitCode, FlakeCheckerError> {

Ok(ExitCode::SUCCESS)
}

#[cfg(feature = "allowed-refs")]
#[derive(Parser)]
struct Cli {
// Check to make sure that Flake Checker is aware of the current supported branches.
#[arg(long, hide = true)]
check_allowed_refs: bool,

// Check to make sure that Flake Checker is aware of the current supported branches.
#[arg(long, hide = true)]
get_allowed_refs: bool,
}

#[cfg(feature = "allowed-refs")]
fn main() -> Result<ExitCode, FlakeCheckerError> {
let Cli {
check_allowed_refs,
get_allowed_refs,
} = Cli::parse();

if !get_allowed_refs && !check_allowed_refs {
panic!("You must select either --get-allowed-refs or --check-allowed-refs");
}

if get_allowed_refs {
match allowed_refs::fetch_allowed_refs() {
Ok(refs) => {
let json_refs = serde_json::to_string(&refs)?;
println!("{json_refs}");
return Ok(ExitCode::SUCCESS);
}
Err(e) => {
println!("Error fetching allowed refs: {}", e);
return Ok(ExitCode::FAILURE);
}
}
}

if check_allowed_refs {
let mut allowed_refs: Vec<String> =
serde_json::from_str(include_str!("../allowed-refs.json")).unwrap();

allowed_refs.sort();

match allowed_refs::check_allowed_refs(allowed_refs) {
Ok(equals) => {
if equals {
println!("The allowed reference sets are up to date.");
return Ok(ExitCode::SUCCESS);
} else {
println!("The allowed reference sets are NOT up to date. Make sure to update.");
return Ok(ExitCode::FAILURE);
}
}
Err(e) => {
println!("Error checking allowed refs: {}", e);
return Ok(ExitCode::FAILURE);
}
}
}

Ok(ExitCode::SUCCESS)
}

0 comments on commit 9d532f9

Please sign in to comment.