From 3bef3218ff6355bade50ff4911c1d4764e3fb786 Mon Sep 17 00:00:00 2001 From: Vadim Date: Sat, 11 Nov 2023 23:43:15 +0200 Subject: [PATCH] refactor(pipeline): refactor the artifact module of the data pipeline (#46) - [x] move artifact configuration to a separate submodule; --- src/data_pipeline/artifact/config/mod.rs | 85 ++++++++++++++++++++++++ src/data_pipeline/artifact/mod.rs | 81 +++------------------- 2 files changed, 96 insertions(+), 70 deletions(-) create mode 100644 src/data_pipeline/artifact/config/mod.rs diff --git a/src/data_pipeline/artifact/config/mod.rs b/src/data_pipeline/artifact/config/mod.rs new file mode 100644 index 0000000..0cf9409 --- /dev/null +++ b/src/data_pipeline/artifact/config/mod.rs @@ -0,0 +1,85 @@ +/// Artifact module for the data pipeline. +/// +use colored::Colorize; +use std::env::{self}; + +/// Artifact module context configuration. +pub fn choose_context(contexts: [&str; 2], context_arg: Option) -> usize { + let p = DataPipelineArtifactConfiguration::new(contexts); + p.choose_context(context_arg) +} + +/// Artifact module file system configuration. +pub fn fs_config(contexts: [&str; 2], collection: String) -> ArtifactFileConfig { + let p = DataPipelineArtifactConfiguration::new(contexts); + p.fs_config(collection) +} + +pub struct ArtifactFileConfig { + pub json_collection_path: String, + pub artifact_base_path: String, + pub artifact_file_name: String, + pub encrypted_artifact_file_name: String, +} + +struct DataPipelineArtifactConfiguration<'a> { + contexts: [&'a str; 2], +} + +impl<'a> DataPipelineArtifactConfiguration<'a> { + /// Program constructor. + fn new(contexts: [&'a str; 2]) -> DataPipelineArtifactConfiguration { + DataPipelineArtifactConfiguration { contexts } + } + + /// Artifact module context configuration. + fn choose_context(&self, context_arg: Option) -> usize { + let is_some = context_arg.is_some(); + let context_arg_input = if is_some { + match context_arg.unwrap().trim().parse::() { + Ok(value) => value, + Err(_) => String::new(), + } + } else { + String::new() + }; + + let mut index = usize::MAX; + for (i, ctx) in self.contexts.iter().enumerate() { + if ctx.to_owned().eq(context_arg_input.as_str()) { + index = i; + break; + } + } + + index + } + + /// Artifact module file system configuration. + fn fs_config(&self, collection: String) -> ArtifactFileConfig { + let cwd = match env::current_dir() { + Ok(value) => { + println!("{}: {:?}", "Current directory".cyan().bold(), value); + value.display().to_string() + } + Err(error) => { + panic!("{:?}", error); + } + }; + + let json_base_path = "./.data/output/github/"; + let json_collection_path = json_base_path.to_owned() + collection.as_str() + "/"; + + let artifact_base_path = cwd + "/.data/artifact/github/"; + let artifact_file_name = "github-".to_string() + collection.as_str() + ".tar.gz"; + let encrypted_artifact_file_name = + "github-".to_string() + collection.as_str() + ".tar.gz.gpg"; + + ArtifactFileConfig { + json_collection_path, + artifact_base_path, + artifact_file_name, + encrypted_artifact_file_name, + } + } +} diff --git a/src/data_pipeline/artifact/mod.rs b/src/data_pipeline/artifact/mod.rs index e45b444..529b78a 100644 --- a/src/data_pipeline/artifact/mod.rs +++ b/src/data_pipeline/artifact/mod.rs @@ -1,11 +1,9 @@ -use colored::Colorize; /// Artifact module for the data pipeline. /// -use std::{ - env::{self}, - fs, -}; +use colored::Colorize; +use std::fs; +mod config; mod create_artifact; mod restore_artifact; @@ -19,13 +17,6 @@ pub fn main(contexts: Contexts, context_arg: Option, collection: String) DataPipelineArtifact::new(contexts, context_arg, collection); } -struct ArtifactFileConfig { - json_collection_path: String, - artifact_base_path: String, - artifact_file_name: String, - encrypted_artifact_file_name: String, -} - struct DataPipelineArtifact<'a> { contexts: Contexts<'a>, } @@ -48,23 +39,23 @@ impl<'a> DataPipelineArtifact<'a> { println!("\n{} {:?}", "Selected context".blue().bold(), context); - let context_index = self.choose_context(context); + let context_index = config::choose_context(self.contexts, context); - let config = self.fs_config(collection); + let fs_config = config::fs_config(self.contexts, collection); match context_index { 0 => { - let create_dir_result = fs::create_dir_all(&config.artifact_base_path); + let create_dir_result = fs::create_dir_all(&fs_config.artifact_base_path); if let Ok(_tmp) = create_dir_result { - let source_path = config.json_collection_path; - let output_path = config.artifact_base_path + &config.artifact_file_name; + let source_path = fs_config.json_collection_path; + let output_path = fs_config.artifact_base_path + &fs_config.artifact_file_name; create_artifact::main(&output_path, &source_path); } } 1 => restore_artifact::main( - &config.artifact_base_path, - &config.artifact_file_name, - &config.encrypted_artifact_file_name, + &fs_config.artifact_base_path, + &fs_config.artifact_file_name, + &fs_config.encrypted_artifact_file_name, ), _ => { println!( @@ -76,54 +67,4 @@ impl<'a> DataPipelineArtifact<'a> { } } } - - /// Prompts input from the user, processes it, and returns the selected context index. - fn choose_context(&self, context_arg: Option) -> usize { - let is_some = context_arg.is_some(); - let context_arg_input = if is_some { - match context_arg.unwrap().trim().parse::() { - Ok(value) => value, - Err(_) => String::new(), - } - } else { - String::new() - }; - - let mut index = usize::MAX; - for (i, ctx) in self.contexts.iter().enumerate() { - if ctx.to_owned().eq(context_arg_input.as_str()) { - index = i; - break; - } - } - - index - } - - fn fs_config(&self, collection: String) -> ArtifactFileConfig { - let cwd = match env::current_dir() { - Ok(value) => { - println!("{}: {:?}", "Current directory".cyan().bold(), value); - value.display().to_string() - } - Err(error) => { - panic!("{:?}", error); - } - }; - - let json_base_path = "./.data/output/github/"; - let json_collection_path = json_base_path.to_owned() + collection.as_str() + "/"; - - let artifact_base_path = cwd + "/.data/artifact/github/"; - let artifact_file_name = "github-".to_string() + collection.as_str() + ".tar.gz"; - let encrypted_artifact_file_name = - "github-".to_string() + collection.as_str() + ".tar.gz.gpg"; - - ArtifactFileConfig { - json_collection_path, - artifact_base_path, - artifact_file_name, - encrypted_artifact_file_name, - } - } }