Skip to content

Commit

Permalink
refactor(pipeline): refactor the artifact module of the data pipeline (
Browse files Browse the repository at this point in the history
…#46)

- [x] move artifact configuration to a separate submodule;
  • Loading branch information
rfprod authored Nov 11, 2023
1 parent 17cb1f5 commit 3bef321
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 70 deletions.
85 changes: 85 additions & 0 deletions src/data_pipeline/artifact/config/mod.rs
Original file line number Diff line number Diff line change
@@ -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<String>) -> 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<String>) -> usize {
let is_some = context_arg.is_some();
let context_arg_input = if is_some {
match context_arg.unwrap().trim().parse::<String>() {
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,
}
}
}
81 changes: 11 additions & 70 deletions src/data_pipeline/artifact/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -19,13 +17,6 @@ pub fn main(contexts: Contexts, context_arg: Option<String>, 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>,
}
Expand All @@ -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!(
Expand All @@ -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<String>) -> usize {
let is_some = context_arg.is_some();
let context_arg_input = if is_some {
match context_arg.unwrap().trim().parse::<String>() {
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,
}
}
}

0 comments on commit 3bef321

Please sign in to comment.