-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoriginal.rs
More file actions
98 lines (83 loc) · 3.25 KB
/
original.rs
File metadata and controls
98 lines (83 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use colored::*;
use dialoguer::Select;
use serde_json::Value;
use std::fs;
use std::path::PathBuf;
use crate::{cli, utils};
use crate::errors::TemplateError;
pub fn fetch_project_from_json(json_source: Option<&str>) -> Result<(), Box<dyn std::error::Error>> {
let json_data = fetch_json_data(json_source)?;
let json: Value = serde_json::from_str(&json_data)
.map_err(|e| TemplateError::InvalidJsonFormat(e.to_string()))?;
let main_categories: Vec<&String> = json.as_object().unwrap().keys().collect();
let selected_category = select_category(&main_categories)?;
let items = json[selected_category]
.as_array()
.ok_or("Failed to retrieve items in the selected category")?;
if items.is_empty() {
println!("There are no available projects in the current category.");
return Ok(());
}
let formatted_choices = format_choices(items);
let selected_item = select_item(items, &formatted_choices, selected_category)?;
process_selected_item(selected_item)?;
Ok(())
}
fn fetch_json_data(json_source: Option<&str>) -> Result<String, Box<dyn std::error::Error>> {
if let Some(source) = json_source {
if source.starts_with("http://") || source.starts_with("https://") {
Ok(reqwest::blocking::get(source)?.text()?)
} else {
Err(Box::new(TemplateError::InvalidSourceUrl("The provided source URL is invalid.".to_string())))
}
} else {
let cache_path = utils::read_config_json("data.json");
Ok(fs::read_to_string(cache_path)?)
}
}
fn select_category<'a>(categories: &'a [&'a String]) -> Result<&'a String, Box<dyn std::error::Error>> {
let main_selection = Select::new()
.with_prompt("Please select a category")
.items(categories)
.default(0)
.interact()?;
Ok(categories[main_selection])
}
fn format_choices(items: &[Value]) -> Vec<String> {
items
.iter()
.map(|item| {
let name = item["name"].as_str().unwrap().to_string();
let desc = item["desc"].as_str().unwrap().to_string();
format!("{} {}", name.green(), desc)
})
.collect()
}
fn select_item<'a>(
items: &'a [Value],
formatted_choices: &[String],
category: &String,
) -> Result<&'a Value, Box<dyn std::error::Error>> {
let item_selection = Select::new()
.with_prompt(format!("Please select a project under {}", category))
.items(formatted_choices)
.default(0)
.interact()?;
Ok(&items[item_selection])
}
fn process_selected_item(selected_item: &Value) -> Result<(), Box<dyn std::error::Error>> {
let link = selected_item["link"].as_str().unwrap();
let project_name = selected_item["name"].as_str().unwrap();
let matches = cli::build_cli().get_matches();
let order_info = utils::resolve_order_info(&matches);
let dir_path = PathBuf::from(&order_info.target_dir);
let target_path = utils::get_target_path(&dir_path, project_name)?;
utils::clone_repository(link, &target_path)?;
println!(
"You selected the project: {}",
selected_item["name"].as_str().unwrap().green()
);
println!("Link: {}", link.blue());
println!("Desc: {}", selected_item["desc"].as_str().unwrap().bright_yellow());
Ok(())
}