Make CDH great (...not again) #3
@@ -1,10 +1,13 @@
 | 
			
		||||
use clap::Arg;
 | 
			
		||||
use log::{debug, info, error};
 | 
			
		||||
use serde_json::from_str;
 | 
			
		||||
use crate::types::{self, HelmRepo};
 | 
			
		||||
use log::{debug, error, info};
 | 
			
		||||
use serde_json::from_str;
 | 
			
		||||
 | 
			
		||||
use super::Connector;
 | 
			
		||||
use std::{borrow::Borrow, io::{Result, Error, ErrorKind}, process::Command};
 | 
			
		||||
use std::{
 | 
			
		||||
    borrow::Borrow,
 | 
			
		||||
    io::{Error, ErrorKind, Result},
 | 
			
		||||
    process::Command,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
pub(crate) struct Argo;
 | 
			
		||||
 | 
			
		||||
@@ -104,12 +107,10 @@ impl Connector for Argo {
 | 
			
		||||
                String::from_utf8_lossy(&output.stderr),
 | 
			
		||||
            ))
 | 
			
		||||
        }
 | 
			
		||||
    
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
impl Argo{
 | 
			
		||||
pub(crate) fn init() -> Argo {
 | 
			
		||||
impl Argo {
 | 
			
		||||
    pub(crate) fn init() -> Argo {
 | 
			
		||||
        Argo
 | 
			
		||||
}
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -4,7 +4,7 @@ use serde_json::from_str;
 | 
			
		||||
use crate::types;
 | 
			
		||||
 | 
			
		||||
use super::Connector;
 | 
			
		||||
use std::{borrow::Borrow, fmt::format, io::Result, process::Command};
 | 
			
		||||
use std::{borrow::Borrow, io::Result, process::Command};
 | 
			
		||||
 | 
			
		||||
pub(crate) struct Helmfile {
 | 
			
		||||
    path: String,
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										122
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										122
									
								
								src/main.rs
									
									
									
									
									
								
							@@ -9,14 +9,13 @@ use serde::{Deserialize, Serialize};
 | 
			
		||||
use serde_json::from_str;
 | 
			
		||||
use std::{
 | 
			
		||||
    borrow::Borrow,
 | 
			
		||||
    fmt::{self, format},
 | 
			
		||||
    io::{Error, ErrorKind, Result},
 | 
			
		||||
    io::Result,
 | 
			
		||||
    process::{exit, Command},
 | 
			
		||||
};
 | 
			
		||||
use tabled::Tabled;
 | 
			
		||||
use version_compare::{Cmp, Version};
 | 
			
		||||
 | 
			
		||||
use crate::types::HelmChart;
 | 
			
		||||
use crate::types::{HelmChart, Status};
 | 
			
		||||
 | 
			
		||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
 | 
			
		||||
enum Kinds {
 | 
			
		||||
@@ -58,23 +57,6 @@ struct LocalCharts {
 | 
			
		||||
    version: Option<String>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Three possible statuses of versions comparison
 | 
			
		||||
#[derive(Clone, Serialize)]
 | 
			
		||||
enum Status {
 | 
			
		||||
    Uptodate,
 | 
			
		||||
    Outdated,
 | 
			
		||||
    Missing,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl fmt::Display for Status {
 | 
			
		||||
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 | 
			
		||||
        match *self {
 | 
			
		||||
            Status::Uptodate => write!(f, "Up-to-date"),
 | 
			
		||||
            Status::Outdated => write!(f, "Outdated"),
 | 
			
		||||
            Status::Missing => write!(f, "Missing"),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
#[derive(Clone, Tabled, Serialize)]
 | 
			
		||||
struct ExecResult {
 | 
			
		||||
    name: String,
 | 
			
		||||
@@ -122,7 +104,7 @@ fn main() {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    charts.iter().for_each(|a| {
 | 
			
		||||
        let err = check_chart(&mut result, a);
 | 
			
		||||
        check_chart(&mut result, a).unwrap();
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    // Parse the helmfile
 | 
			
		||||
@@ -269,104 +251,6 @@ fn handle_result(result: &Vec<ExecResult>, outdated_fail: bool) -> Result<bool>
 | 
			
		||||
    Ok(failed)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Downloading repos from repositories
 | 
			
		||||
fn repo_sync() -> Result<()> {
 | 
			
		||||
    info!("syncing helm repos");
 | 
			
		||||
    let cmd: String = "argocd app list -o json | jq '[ .[] | {name: .spec.source.chart, url: .spec.source.repoURL} ]'".to_string();
 | 
			
		||||
    let output = Command::new("bash")
 | 
			
		||||
        .arg("-c")
 | 
			
		||||
        .arg(cmd)
 | 
			
		||||
        .output()
 | 
			
		||||
        .expect("helmfile is failed");
 | 
			
		||||
    info!("{:?}", output.clone());
 | 
			
		||||
    if output.status.success() {
 | 
			
		||||
        let repos: Vec<Repo> = serde_json::from_slice(&output.stdout).unwrap();
 | 
			
		||||
        info!("adding repositories");
 | 
			
		||||
        for repo in repos.iter() {
 | 
			
		||||
            let name = repo.name.clone();
 | 
			
		||||
            if name.is_some() {
 | 
			
		||||
                info!(
 | 
			
		||||
                    "syncing {} with the origin {}",
 | 
			
		||||
                    name.clone().unwrap(),
 | 
			
		||||
                    repo.url
 | 
			
		||||
                );
 | 
			
		||||
                let cmd = format!(
 | 
			
		||||
                    "helm repo add {} {}",
 | 
			
		||||
                    name.clone().unwrap(),
 | 
			
		||||
                    repo.url.clone()
 | 
			
		||||
                );
 | 
			
		||||
                debug!("running {}", cmd);
 | 
			
		||||
                let output = Command::new("bash")
 | 
			
		||||
                    .arg("-c")
 | 
			
		||||
                    .arg(cmd)
 | 
			
		||||
                    .output()
 | 
			
		||||
                    .expect("helm repo sync is failed");
 | 
			
		||||
                match output.status.success() {
 | 
			
		||||
                    true => {
 | 
			
		||||
                        info!(
 | 
			
		||||
                            "{} with the origin {} is synced successfully",
 | 
			
		||||
                            name.unwrap(),
 | 
			
		||||
                            repo.url
 | 
			
		||||
                        );
 | 
			
		||||
                    }
 | 
			
		||||
                    false => {
 | 
			
		||||
                        error!(
 | 
			
		||||
                            "{} with the origin {} can't be synced",
 | 
			
		||||
                            name.unwrap(),
 | 
			
		||||
                            repo.url
 | 
			
		||||
                        )
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        let cmd = "helm repo update";
 | 
			
		||||
        let output = Command::new("bash")
 | 
			
		||||
            .arg("-c")
 | 
			
		||||
            .arg(cmd)
 | 
			
		||||
            .output()
 | 
			
		||||
            .expect("helm repo sync is failed");
 | 
			
		||||
        match output.status.success() {
 | 
			
		||||
            true => {
 | 
			
		||||
                info!("repositories are updated successfully");
 | 
			
		||||
            }
 | 
			
		||||
            false => {
 | 
			
		||||
                error!(
 | 
			
		||||
                    "repositories can't be updated, {}",
 | 
			
		||||
                    String::from_utf8_lossy(&output.stderr)
 | 
			
		||||
                );
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Ok(())
 | 
			
		||||
    } else {
 | 
			
		||||
        Err(Error::new(
 | 
			
		||||
            ErrorKind::Other,
 | 
			
		||||
            String::from_utf8_lossy(&output.stderr),
 | 
			
		||||
        ))
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Run helmfile list and write the result into struct
 | 
			
		||||
fn parse_argo_apps() -> Result<Vec<LocalCharts>> {
 | 
			
		||||
    let cmd: String = "argocd app list -o json | jq '[.[] | {chart: .spec.source.chart, version: .spec.source.targetRevision}]'".to_string();
 | 
			
		||||
 | 
			
		||||
    debug!("executing '${}'", cmd);
 | 
			
		||||
    let output = Command::new("bash")
 | 
			
		||||
        .arg("-c")
 | 
			
		||||
        .arg(cmd)
 | 
			
		||||
        .output()
 | 
			
		||||
        .expect("helmfile is failed");
 | 
			
		||||
    let helm_stdout = String::from_utf8_lossy(&output.stdout);
 | 
			
		||||
 | 
			
		||||
    match from_str::<Vec<LocalCharts>>(Borrow::borrow(&helm_stdout)) {
 | 
			
		||||
        Ok(mut charts) => {
 | 
			
		||||
            charts.dedup();
 | 
			
		||||
            Ok(charts)
 | 
			
		||||
        }
 | 
			
		||||
        Err(err) => Err(err.into()),
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Takes two version and returns the newer one.
 | 
			
		||||
fn get_newer_version(v1: String, v2: String) -> String {
 | 
			
		||||
    match Version::from(&v1.replace('v', ""))
 | 
			
		||||
 
 | 
			
		||||
@@ -16,7 +16,7 @@ pub(crate) struct HelmRepo {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Clone, Serialize)]
 | 
			
		||||
enum Status {
 | 
			
		||||
pub(crate) enum Status {
 | 
			
		||||
    Uptodate,
 | 
			
		||||
    Outdated,
 | 
			
		||||
    Missing,
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user