From dc88ccbfc5625c26073b047565e4976017f1633d Mon Sep 17 00:00:00 2001 From: Alexandr Sorokin Date: Wed, 29 May 2024 14:44:53 +0300 Subject: [PATCH] Add app version to inventory Closes #160 --- src/task/inventory.rs | 24 +++++++++++++++++++++++- tests/mod.rs | 23 +++++++++++++++++------ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/src/task/inventory.rs b/src/task/inventory.rs index 2b12133..6f3d48a 100644 --- a/src/task/inventory.rs +++ b/src/task/inventory.rs @@ -1,6 +1,7 @@ use std::convert::TryFrom; use std::fs::File; use std::io; +use std::io::Write; use std::path::PathBuf; use clap::ArgMatches; @@ -21,11 +22,30 @@ use crate::task::Validate; use crate::{ error::{GeninError, GeninErrorKind}, task::cluster::ins::Role, + APP_VERSION, }; use super::cluster::ClusterError; use super::utils::create_file_or_copy; +pub struct HeaderInfo { + title: String, + version: String, +} +impl Default for HeaderInfo { + fn default() -> Self { + Self { + title: "Inventory generated by Genin".to_owned(), + version: APP_VERSION.to_owned(), + } + } +} +impl ToString for HeaderInfo { + fn to_string(&self) -> String { + format!("# {}\n# version: {}\n", self.title, self.version) + } +} + #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] pub struct Inventory { pub all: InventoryParts, @@ -268,7 +288,9 @@ impl Inventory { .to_owned(), ); - let file = create_file_or_copy(path, args.get_flag("force"))?; + let mut file = create_file_or_copy(path, args.get_flag("force"))?; + let info = HeaderInfo::default().to_string(); + file.write_all(info.as_bytes()).unwrap(); serde_yaml::to_writer(file, &self)?; diff --git a/tests/mod.rs b/tests/mod.rs index 66732c3..aa143f0 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -4,6 +4,8 @@ use std::{ process::{Command, Output}, }; +const INVENTORY_HEADER_SIZE: usize = 2; + fn create_file(path: &str) { File::create(path).unwrap(); } @@ -21,6 +23,15 @@ fn cleanup_test_dir(path: &str) { create_test_dir(path); } +pub fn read_inventory(path: &str) -> String { + read_to_string(path) + .unwrap() + .lines() + .map(String::from) + .collect::>()[INVENTORY_HEADER_SIZE..] + .join("\n") +} + #[allow(unused)] pub fn build_result_from_output(output: Output) -> String { let mut result = Vec::new(); @@ -196,7 +207,7 @@ fn sequential_upgrade_from_state() { let cluster_to_cluster_new = format!( "{cluster_to_cluster_new}\n{}", - read_to_string("tests/.sequential_upgrade_from_state/v1_inventory.yml").unwrap() + read_inventory("tests/.sequential_upgrade_from_state/v1_inventory.yml") ); insta::assert_display_snapshot!("cluster_to_cluster_new", cluster_to_cluster_new); @@ -225,7 +236,7 @@ fn sequential_upgrade_from_state() { let cluster_new_to_cluster_v2 = format!( "{cluster_new_to_cluster_v2}\n{}", - read_to_string("tests/.sequential_upgrade_from_state/v2_inventory.yml").unwrap() + read_inventory("tests/.sequential_upgrade_from_state/v2_inventory.yml") ); insta::assert_display_snapshot!("cluster_new_to_cluster_v2", cluster_new_to_cluster_v2); @@ -252,7 +263,7 @@ fn sequential_upgrade_from_state() { let cluster_v2_to_cluster_v3 = build_result_from_output(output); let cluster_v2_to_cluster_v3 = format!( "{cluster_v2_to_cluster_v3}\n{}", - read_to_string("tests/.sequential_upgrade_from_state/v3_inventory.yml").unwrap() + read_inventory("tests/.sequential_upgrade_from_state/v3_inventory.yml") ); insta::assert_display_snapshot!("cluster_v2_to_cluster_v3", cluster_v2_to_cluster_v3); @@ -282,7 +293,7 @@ fn sequential_upgrade_with_decreasing() { let cluster_v3_to_cluster_v4 = format!( "{cluster_v3_to_cluster_v4}\n{}", - read_to_string("tests/.sequential_upgrade_with_decreasing/v1_inventory.yml").unwrap() + read_inventory("tests/.sequential_upgrade_with_decreasing/v1_inventory.yml") ); insta::assert_display_snapshot!("cluster_v3_to_cluster_v4", cluster_v3_to_cluster_v4); @@ -310,7 +321,7 @@ fn sequential_upgrade_with_decreasing() { let cluster_v4_to_cluster_v5 = format!( "{cluster_v4_to_cluster_v5}\n{}", - read_to_string("tests/.sequential_upgrade_with_decreasing/v2_inventory.yml").unwrap() + read_inventory("tests/.sequential_upgrade_with_decreasing/v2_inventory.yml") ); insta::assert_display_snapshot!("cluster_v4_to_cluster_v5", cluster_v4_to_cluster_v5); @@ -342,7 +353,7 @@ fn upgrade_consistency_100_times() { let consistency_100_times = format!( "{consistency_100_times}\n{}", - read_to_string("tests/.upgrade_consistency_100_times/inventory.yml").unwrap() + read_inventory("tests/.upgrade_consistency_100_times/inventory.yml") ); insta::assert_display_snapshot!("consistency_100_times", consistency_100_times);