Skip to content

Commit

Permalink
conditionally render status line (#211)
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Hoß <seb@xn--ho-hia.de>
  • Loading branch information
sebhoss authored Mar 16, 2024
1 parent 561adae commit 358958e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::PathBuf;
use anyhow::{anyhow, Context, Result};
use clap::{CommandFactory, Parser, Subcommand};
use k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1::{
CustomResourceDefinition, CustomResourceDefinitionVersion, CustomResourceSubresources,
CustomResourceDefinition, CustomResourceDefinitionVersion,
};
use kopium::{analyze, Config, Container};
use kube::{api, core::Version, Api, Client, ResourceExt};
Expand Down Expand Up @@ -222,8 +222,8 @@ impl Kopium {
if scope == "Namespaced" {
println!(r#"#[kube(namespaced)]"#);
}
if let Some(CustomResourceSubresources { status: Some(_), .. }) =
version.subresources
if version.subresources.as_ref().is_some_and(|c| c.status.is_some())
&& self.has_status_resource(&structs)
{
println!(r#"#[kube(status = "{}Status")]"#, kind);
}
Expand Down Expand Up @@ -327,6 +327,12 @@ impl Kopium {
println!("#[derive({})]", derives.join(", "));
}

fn has_status_resource(&self, results: &[Container]) -> bool {
results
.iter()
.any(|o| o.is_status_container() && !o.members.is_empty())
}

fn print_prelude(&self, results: &[Container]) {
if !self.hide_kube {
println!("use kube::CustomResource;");
Expand Down
4 changes: 4 additions & 0 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ impl Container {
self.level == 1 && self.name.ends_with("Spec")
}

pub fn is_status_container(&self) -> bool {
self.level == 1 && self.name.ends_with("Status")
}

pub fn contains_conditions(&self) -> bool {
self.members.iter().any(|m| m.type_.contains("Vec<Condition>"))
}
Expand Down

0 comments on commit 358958e

Please sign in to comment.