Skip to content

Commit

Permalink
Merge pull request #3 from maheshrayas/support-csv
Browse files Browse the repository at this point in the history
Output the deprecated list to csv
  • Loading branch information
maheshrayas authored Apr 17, 2022
2 parents 62f195c + 94a411b commit 640d9ca
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 32 deletions.
55 changes: 51 additions & 4 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ reqwest = { version = "0.11", features = ["json"] }
clap = { version = "3.1.8", features = ["derive"] }
comfy-table = "5.0.1"
openssl = { version = "0.10", features = ["vendored"] }
csv="1.1.6"
6 changes: 2 additions & 4 deletions src/cluster.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::{Deprecated, JsonDetails, TableDetails};
use crate::utils::{ClusterOP, Deprecated, JsonDetails, TableDetails};
use anyhow::Result;
use kube::{
api::{Api, DynamicObject, ResourceExt},
Expand All @@ -10,9 +10,7 @@ use log::info;
use std::sync::Arc;
use tokio::task::spawn;

pub(crate) async fn get_cluster_resources(
version: &str,
) -> Result<Vec<tokio::task::JoinHandle<Result<Vec<TableDetails>>>>> {
pub(crate) async fn get_cluster_resources(version: &str) -> Result<ClusterOP> {
let client = Client::try_default().await?;
//let current_config = kube::config::Config::infer().await?;
let current_config = kube::config::Kubeconfig::read().unwrap();
Expand Down
Empty file added src/csv.rs
Empty file.
40 changes: 16 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use anyhow::Result;
use comfy_table::Table;
use utils::{DeprecatedResult, Output};
mod cluster;
mod utils;
use crate::cluster::get_cluster_resources;
use crate::utils::{init_logger, TableDetails};
use crate::utils::{init_logger, ClusterOP};
use clap::Parser;

#[derive(Parser)]
#[clap(author, version, about, long_about = None)]
struct Sunset {
#[clap(long = "target-version", short = 't')]
target_version: Option<String>,
/// Output format table, junit, csv
#[clap(long = "output", short = 'o', arg_enum,default_value_t = Output::Table)]
output: Output,
#[clap(long, short)]
kubeconfig: Option<String>,
#[clap(short, long, parse(from_occurrences))]
Expand All @@ -36,29 +38,19 @@ async fn main() -> anyhow::Result<()> {

init_logger();

let mut table = Table::new();
table.set_header(vec![
"Kind",
"Namespace",
"Name",
"DeprecatedApiVersion",
"SupportedApiVersion",
]);
let join_handle: Vec<tokio::task::JoinHandle<Result<Vec<TableDetails>>>> =
get_cluster_resources(version).await?;
let join_handle: ClusterOP = get_cluster_resources(version).await?;
let d = DeprecatedResult::new(join_handle);

for task in join_handle {
let result = task.await?.unwrap();
for r in result {
table.add_row(vec![
r.kind,
r.namespace,
r.name,
r.deprecated_api_version,
r.supported_api_version,
]);
match cli.output {
Output::Csv => {
d.generate_csv().await?;
}
Output::Junit => {
println!("Junit");
}
Output::Table => {
d.generate_table().await?;
}
}
println!("{table}");
Ok(())
}
77 changes: 77 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,78 @@
use anyhow::Result;
use clap::ArgEnum;
use comfy_table::Table;
use env_logger::{Builder, Env};
use log::info;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::io::Write;
use tokio::task::JoinHandle;

pub(crate) type ClusterOP = Vec<JoinHandle<Result<Vec<TableDetails>>>>;

pub(crate) struct DeprecatedResult {
pub(crate) output: ClusterOP,
}

impl DeprecatedResult {
pub(crate) fn new(d: ClusterOP) -> Self {
Self { output: d }
}

pub(crate) async fn generate_table(self) -> Result<()> {
let mut table = Table::new();
table.set_header(vec![
"Kind",
"Namespace",
"Name",
"DeprecatedApiVersion",
"SupportedApiVersion",
]);
for task in self.output {
let result = task.await?.unwrap();
for r in result {
table.add_row(vec![
r.kind,
r.namespace,
r.name,
r.deprecated_api_version,
r.supported_api_version,
]);
}
}
println!("{table}");
Ok(())
}

pub(crate) async fn generate_csv(self) -> Result<()> {
let mut wtr = csv::Writer::from_path("./deprecated-list.csv")?;
wtr.write_record([
"Kind",
"Namespace",
"Name",
"DeprecatedApiVersion",
"SupportedApiVersion",
])?;
for task in self.output {
let result = task.await?.unwrap();
for r in result {
wtr.write_record([
r.kind,
r.namespace,
r.name,
r.deprecated_api_version,
r.supported_api_version,
])?;
}
}
wtr.flush()?;
info!(
"deprecated-list.csv written at location {}",
std::env::current_dir()?.as_os_str().to_str().unwrap()
);
Ok(())
}
}
#[derive(Serialize, Deserialize, Default, Debug)]
pub(crate) struct JsonDetails {
#[serde(rename = "apiVersion")]
Expand Down Expand Up @@ -50,3 +120,10 @@ pub(crate) fn init_logger() {
})
.init();
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ArgEnum)]
pub(crate) enum Output {
Table,
Junit,
Csv,
}

0 comments on commit 640d9ca

Please sign in to comment.