Some fixes and updates
Signed-off-by: Nikolai Rodionov <nikolai.rodionov@onpier.de>
This commit is contained in:
		@@ -4,10 +4,17 @@ repositories:
 | 
			
		||||
      url: https://grafana.github.io/helm-charts
 | 
			
		||||
 | 
			
		||||
charts:
 | 
			
		||||
  - name: k8s-monitoring
 | 
			
		||||
  - name: mimir-distributed
 | 
			
		||||
    repository: grafana
 | 
			
		||||
    patches:
 | 
			
		||||
      - name: vendor-deps
 | 
			
		||||
    mirrors:
 | 
			
		||||
      - custom-command
 | 
			
		||||
patches:
 | 
			
		||||
  - name: vendor-deps
 | 
			
		||||
    custom_command:
 | 
			
		||||
      commands:
 | 
			
		||||
        - yq -i '.dependencies[].repository = "file://charts"' Chart.yaml
 | 
			
		||||
mirrors:
 | 
			
		||||
  - name: custom-commands
 | 
			
		||||
    custom_command:
 | 
			
		||||
 
 | 
			
		||||
@@ -90,7 +90,7 @@ impl Git {
 | 
			
		||||
            let _ = self.exec(cmd, git_opts.workdir.clone())?;
 | 
			
		||||
        }
 | 
			
		||||
        let cmd = format!(
 | 
			
		||||
            "{} diff --staged --quiet || {} -C {} commit -m \"{}\"",
 | 
			
		||||
            "{} diff --cached --quiet || {} -C {} commit -m \"{}\"",
 | 
			
		||||
            git_opts.bin, git_opts.bin, self.repo_path, opts.message
 | 
			
		||||
        );
 | 
			
		||||
        match self.exec(cmd, git_opts.workdir.clone()) {
 | 
			
		||||
 
 | 
			
		||||
@@ -20,6 +20,10 @@ pub(crate) fn latest() -> String {
 | 
			
		||||
    "latest".to_string()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub(crate) fn def_false() -> bool {
 | 
			
		||||
    false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Chart {
 | 
			
		||||
    pub fn find_repo(
 | 
			
		||||
        &self,
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										41
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										41
									
								
								src/main.rs
									
									
									
									
									
								
							@@ -1,6 +1,7 @@
 | 
			
		||||
use clap::Parser;
 | 
			
		||||
use cli::{check_prerequisites, get_full_path_dir};
 | 
			
		||||
use config::{read_config, Config, ConfigImpl};
 | 
			
		||||
use config::{read_config, ChartExtended, Config, ConfigImpl};
 | 
			
		||||
use helm::chart::Chart;
 | 
			
		||||
use output::{message_empty, message_error};
 | 
			
		||||
use std::{error::Error, process::exit};
 | 
			
		||||
 | 
			
		||||
@@ -24,6 +25,12 @@ struct Args {
 | 
			
		||||
    /// Name of the working dir
 | 
			
		||||
    #[arg(short, long)]
 | 
			
		||||
    workdir: Option<String>,
 | 
			
		||||
    /// Configure the chart section from cli
 | 
			
		||||
    /// If only one chart is needed
 | 
			
		||||
    #[arg(long)]
 | 
			
		||||
    chart: Option<String>,
 | 
			
		||||
    #[arg(long)]
 | 
			
		||||
    version_override: Option<String>,
 | 
			
		||||
    /// Path to the configuration file
 | 
			
		||||
    #[arg(short, long)]
 | 
			
		||||
    config: String,
 | 
			
		||||
@@ -53,12 +60,26 @@ fn exec(args: Args) -> Result<(), Box<dyn Error>> {
 | 
			
		||||
    let workdir_path = workdir::setup_workdir(args.workdir)?;
 | 
			
		||||
    let mut config: Config = read_config(args.config.clone())?;
 | 
			
		||||
    let config_full_path = get_full_path_dir(args.config.clone())?;
 | 
			
		||||
    if let Some(chart_arg) = args.chart {
 | 
			
		||||
        config.charts = config.charts.into_iter().filter(|chart| chart.chart.name == chart_arg).collect::<Vec<ChartExtended>>();
 | 
			
		||||
        if let Some(version_arg) = args.version_override {
 | 
			
		||||
            config.charts.iter_mut()
 | 
			
		||||
                .for_each(|chart| chart.chart.version = version_arg.clone());
 | 
			
		||||
        };
 | 
			
		||||
    };
 | 
			
		||||
    config.apply_includes(config_full_path.clone())?;
 | 
			
		||||
    config
 | 
			
		||||
        .charts
 | 
			
		||||
        .into_iter()
 | 
			
		||||
        .try_for_each(|mut chart| -> Result<(), Box<dyn Error>> {
 | 
			
		||||
            chart.populate_variables(config.variables.clone());
 | 
			
		||||
            let mut chart_mirrors = config.mirrors.clone();
 | 
			
		||||
            chart_mirrors.retain(|mirror| mirror.check_chart(&chart) == false);
 | 
			
		||||
 | 
			
		||||
            if chart_mirrors.is_empty() {
 | 
			
		||||
                output::message_info(&format!("mirrors are up-to-date for {}", chart.chart.name.clone()));
 | 
			
		||||
                return Ok(());
 | 
			
		||||
            }
 | 
			
		||||
            // First step is to pull the chart to the working dir
 | 
			
		||||
            let current_repo = chart.chart.find_repo(config.repositories.clone())?;
 | 
			
		||||
            let chart_path = current_repo.pull_chart(chart.chart.clone(), workdir_path.clone())?;
 | 
			
		||||
@@ -77,16 +98,18 @@ fn exec(args: Args) -> Result<(), Box<dyn Error>> {
 | 
			
		||||
                        patch.apply(chart_path.clone(), config.patches.clone())
 | 
			
		||||
                    })?
 | 
			
		||||
            }
 | 
			
		||||
            config
 | 
			
		||||
                .mirrors
 | 
			
		||||
            chart_mirrors
 | 
			
		||||
                .iter()
 | 
			
		||||
                .try_for_each(|mirror| -> Result<(), Box<dyn Error>> {
 | 
			
		||||
                    mirror_from_mirror_obj(mirror.clone())?.push(
 | 
			
		||||
                        workdir_path.clone(),
 | 
			
		||||
                        chart_path.clone(),
 | 
			
		||||
                        chart.clone(),
 | 
			
		||||
                        args.dry_run,
 | 
			
		||||
                    )
 | 
			
		||||
                    if chart.chart.mirrors.contains(&mirror.name) {
 | 
			
		||||
                        mirror_from_mirror_obj(mirror.clone())?.push(
 | 
			
		||||
                            workdir_path.clone(),
 | 
			
		||||
                            chart_path.clone(),
 | 
			
		||||
                            chart.clone(),
 | 
			
		||||
                            args.dry_run,
 | 
			
		||||
                        )?
 | 
			
		||||
                    };
 | 
			
		||||
                    Ok(())
 | 
			
		||||
                })?;
 | 
			
		||||
            message_empty(&format!("{}", chart.chart.name));
 | 
			
		||||
            Ok(())
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
use serde::{Deserialize, Serialize};
 | 
			
		||||
 | 
			
		||||
use crate::config::ChartExtended;
 | 
			
		||||
use crate::{cli::{self, cli_exec, cli_exec_from_dir}, config::ChartExtended, output::message_info, template::render};
 | 
			
		||||
 | 
			
		||||
pub(crate) mod custom_command;
 | 
			
		||||
pub(crate) mod git;
 | 
			
		||||
@@ -20,6 +20,26 @@ pub(crate) struct Mirror {
 | 
			
		||||
    pub(crate) name: String,
 | 
			
		||||
    pub(crate) git: Option<git::GitMirror>,
 | 
			
		||||
    pub(crate) custom_command: Option<CustomCommandsMirror>,
 | 
			
		||||
    pub(crate) check: Option<String>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Mirror {
 | 
			
		||||
    pub(crate) fn check_chart(&self, chart_local: &ChartExtended) -> bool {
 | 
			
		||||
        match &self.check {
 | 
			
		||||
            Some(check_cmd) => {
 | 
			
		||||
                let cmd = match render(check_cmd.to_string(), chart_local) {
 | 
			
		||||
                    Ok(res) => res,
 | 
			
		||||
                    Err(_) => return false,
 | 
			
		||||
                };
 | 
			
		||||
                message_info(&format!("checking the mirror {}", self.name.clone()));
 | 
			
		||||
                match cli_exec(cmd.clone()) {
 | 
			
		||||
                    Ok(_) => return true,
 | 
			
		||||
                    Err(_) => return false,
 | 
			
		||||
                }
 | 
			
		||||
            },
 | 
			
		||||
            None => false,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user