allow specifying path for generate_file()
This commit is contained in:
25
src/main.rs
25
src/main.rs
@ -27,7 +27,7 @@ fn run_as_cli(client: &mut postgres::Client) -> Result<(), Box<dyn Error>> {
|
|||||||
|
|
||||||
match option.trim_end() {
|
match option.trim_end() {
|
||||||
"usage" => show_usage(),
|
"usage" => show_usage(),
|
||||||
"generate" => generate_file(client, None)?,
|
"generate" => generate_file(client, None, None)?,
|
||||||
"convert" => convert_folders(client, None)?,
|
"convert" => convert_folders(client, None)?,
|
||||||
"import" => import_file(client, None)?,
|
"import" => import_file(client, None)?,
|
||||||
"add_url" => add_url(client, None)?,
|
"add_url" => add_url(client, None)?,
|
||||||
@ -55,7 +55,11 @@ fn run_with_input(
|
|||||||
let value = arguments[2].clone();
|
let value = arguments[2].clone();
|
||||||
|
|
||||||
match arguments[1].as_str() {
|
match arguments[1].as_str() {
|
||||||
"generate" => generate_file(client, Option::Some(value))?,
|
"generate" => generate_file(
|
||||||
|
client,
|
||||||
|
Option::Some(value),
|
||||||
|
Option::Some(arguments[3].clone()),
|
||||||
|
)?,
|
||||||
"convert" => convert_folders(client, Option::Some(value))?,
|
"convert" => convert_folders(client, Option::Some(value))?,
|
||||||
"import" => import_file(client, Option::Some(value))?,
|
"import" => import_file(client, Option::Some(value))?,
|
||||||
"add_url" => add_url(client, Option::Some(value))?,
|
"add_url" => add_url(client, Option::Some(value))?,
|
||||||
@ -79,8 +83,10 @@ fn run_with_input(
|
|||||||
fn generate_file(
|
fn generate_file(
|
||||||
client: &mut postgres::Client,
|
client: &mut postgres::Client,
|
||||||
website: Option<String>,
|
website: Option<String>,
|
||||||
|
folder_path: Option<String>,
|
||||||
) -> Result<(), Box<dyn Error>> {
|
) -> Result<(), Box<dyn Error>> {
|
||||||
let mut website_name = String::new();
|
let mut website_name = String::new();
|
||||||
|
let mut file_path = String::new();
|
||||||
|
|
||||||
match website {
|
match website {
|
||||||
Some(val) => website_name = val,
|
Some(val) => website_name = val,
|
||||||
@ -91,8 +97,17 @@ fn generate_file(
|
|||||||
io::stdin().read_line(&mut website_name)?;
|
io::stdin().read_line(&mut website_name)?;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
website_name = website_name.trim().to_string();
|
||||||
|
|
||||||
let website_name = website_name.trim();
|
match folder_path {
|
||||||
|
Some(val) => file_path = val,
|
||||||
|
None => {
|
||||||
|
println!("Please input the folder_path of where to save the file");
|
||||||
|
io::stdin().read_line(&mut file_path)?;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
file_path = file_path.trim().to_string();
|
||||||
|
file_path.push_str(format!("/{website_name}_generated.txt").as_str());
|
||||||
|
|
||||||
let output = client.query_one("SELECT website_exists($1);", &[&website_name])?;
|
let output = client.query_one("SELECT website_exists($1);", &[&website_name])?;
|
||||||
|
|
||||||
@ -104,9 +119,7 @@ fn generate_file(
|
|||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
let filename = format!("{website_name}_generated.txt");
|
let mut file = File::create(file_path)?;
|
||||||
let mut file = File::create_new(filename)?;
|
|
||||||
|
|
||||||
let output = client.query("SELECT * FROM get_urls_and_paths($1);", &[&website_name])?;
|
let output = client.query("SELECT * FROM get_urls_and_paths($1);", &[&website_name])?;
|
||||||
|
|
||||||
for row in output {
|
for row in output {
|
||||||
|
Reference in New Issue
Block a user