Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Feature rename #12 #17

Merged
merged 7 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/app/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub trait Backend: Send {
async fn get_request_saved(&mut self, name: String) -> Result<RequestData>;
async fn find_all_request_name(&mut self) -> Result<Vec<String>>;
async fn remove_request_saved(&mut self, name: String) -> Result<()>;
async fn rename_request_saved(&mut self, request_name: String, new_name: String) -> Result<()>;
}

pub struct AppBackend {
Expand Down Expand Up @@ -214,6 +215,14 @@ impl Backend for AppBackend {
)
.await?
}

async fn rename_request_saved(&mut self, request_name: String, new_name: String) -> Result<()> {
run_command_waiting_response(
&self.file_service,
FileServiceCommandsFactory::rename_file_saved_request(request_name, new_name),
)
.await?
}
}

async fn run_commands<Service, Resp>(
Expand Down
14 changes: 14 additions & 0 deletions src/app/services/files/commands/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,18 @@ impl CommandsFactory {
})
.with_response(rx)
}

pub fn rename_file_saved_request(request_name: String, new_name: String) -> CommandFileService<Result<()>> {
let (tx, rx) = oneshot::channel();

Command::from(move |service: FileServiceInstance| {
let resp = service.rename_data_file(
format!("{REQUESTS_FOLDER}{request_name}"),
format!("{REQUESTS_FOLDER}{new_name}"),
);
tx.send(resp).ok();
service
})
.with_response(rx)
}
}
3 changes: 3 additions & 0 deletions src/app/services/files/facade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ pub trait FileServiceFacade: Send {
fn remove_file(&self, path: PathBuf) -> Result<()>;
fn remove_data_file(&self, path: String) -> Result<()>;
fn remove_temp_file(&self, path: String) -> Result<()>;
fn rename_file(&self, from: PathBuf, to: PathBuf) -> Result<()>;
fn rename_data_file(&self, from: String, to: String) -> Result<()>;
fn rename_temp_file(&self, from: String, to: String) -> Result<()>;
}
16 changes: 16 additions & 0 deletions src/app/services/files/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ impl FileServiceFacade for FileService {
let file_path = self.temp_root_path.join(path);
self.remove_file(file_path)
}

fn rename_file(&self, from: PathBuf, to: PathBuf) -> Result<()> {
Ok(std::fs::rename(from, to)?)
}

fn rename_data_file(&self, from: String, to: String) -> Result<()> {
let from_file_path = self.data_app_root_path.join(from);
let to_file_path = self.data_app_root_path.join(to);
self.rename_file(from_file_path, to_file_path)
}

fn rename_temp_file(&self, from: String, to: String) -> Result<()> {
let from_file_path = self.temp_root_path.join(from);
let to_file_path = self.temp_root_path.join(to);
self.rename_file(from_file_path, to_file_path)
}
}

impl FileService {
Expand Down
12 changes: 11 additions & 1 deletion src/view/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::view::output::writer::CrosstermCliWriter;

pub mod inspect_request;
pub mod remove_request;
pub mod rename_request;
pub mod save_new_request;
pub mod save_request_with_base_request;
pub mod show_list_all_request;
Expand Down Expand Up @@ -50,6 +51,7 @@ pub enum ViewCommandChoice {
RenameSavedRequest {
request_name: String,
new_name: String,
no_confirm: bool,
},

ShowRequests,
Expand All @@ -67,6 +69,7 @@ impl ViewCommandChoice {
use self::show_list_all_request::ShowListAllRequestExecutor;
use self::submit_request::BasicRequestExecutor;
use self::submit_saved_request::SubmitSavedRequestExecutor;
use self::rename_request::RenameRequestExecutor;

let writer_stdout = CrosstermCliWriter::from(stdout());
let writer_stderr = CrosstermCliWriter::from(stderr());
Expand Down Expand Up @@ -131,7 +134,14 @@ impl ViewCommandChoice {
ViewCommandChoice::RenameSavedRequest {
request_name,
new_name,
} => todo!(),
no_confirm,
} => RenameRequestExecutor {
request_name,
new_name,
no_confirm,
writer: writer_stdout,
}
.into(),
}
}
}
Expand Down
61 changes: 61 additions & 0 deletions src/view/commands/rename_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use std::io::{stdin, BufRead};

use anyhow::Ok;
use async_trait::async_trait;

use super::ViewCommand;
use crate::app::backend::Backend;
use crate::view::output::utils::BREAK_LINE;
use crate::view::output::writer::CliWriterRepository;
use crate::view::style::{Color, StyledStr};

pub struct RenameRequestExecutor<Writer: CliWriterRepository> {
pub request_name: String,
pub new_name: String,
pub no_confirm: bool,
pub writer: Writer,
}

#[async_trait]
impl<Writer: CliWriterRepository> ViewCommand for RenameRequestExecutor<Writer> {
async fn execute(mut self: Box<Self>, provider: &mut dyn Backend) -> anyhow::Result<()> {
self.writer.print_lines([BREAK_LINE]);
self.writer.print_lines_styled([[
StyledStr::from(" Renaming from: ").with_color_text(Color::Red),
StyledStr::from(&self.request_name).with_color_text(Color::Yellow),
StyledStr::from(" to: ").with_color_text(Color::Red),
StyledStr::from(&self.new_name).with_color_text(Color::Yellow),
]]);
self.writer.print_lines([BREAK_LINE]);

if self.no_confirm {
SummerGram marked this conversation as resolved.
Show resolved Hide resolved
self.writer.print_lines_styled([[
StyledStr::from(" Are you sure? [y/N] ").with_color_text(Color::Red),
]]);

let mut input = String::new();

stdin().lock().read_line(&mut input)?;

while input.trim().to_lowercase() != "y" {
if input.trim().to_lowercase() == "n" {
self.writer.print_lines_styled([[
StyledStr::from(" Aborted ").with_color_text(Color::Red),
]]);
self.writer.print_lines([BREAK_LINE]);

return Ok(());
}

input = String::new();
stdin().read_line(&mut input)?;
SummerGram marked this conversation as resolved.
Show resolved Hide resolved
}
}

provider.rename_request_saved(self.request_name, self.new_name).await?;

self.writer.print_lines([" Ok "]);

Ok(())
}
}
9 changes: 8 additions & 1 deletion src/view/input/cli_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub fn root_command() -> Command {
.subcommand(
Command::new("rename")
.override_usage(format!(
"treq run <OLD_REQUEST_NAME> <NEW_REQUEST_NAME> [OPTIONS]"
"treq rename <OLD_REQUEST_NAME> <NEW_REQUEST_NAME> [OPTIONS]"
))
.about("Rename request")
.arg(
Expand All @@ -77,6 +77,13 @@ pub fn root_command() -> Command {
.required(true)
.num_args(2)
.help("All entrys"),
)
.arg(
Arg::new("no-confirm")
.long("no-confirm")
.action(ArgAction::SetTrue)
.value_name("NO_CONFIRM")
.help("Do not prompt for confirmation"),
),
)
.subcommand(Command::new("ls").about("List all saved requests"))
Expand Down
3 changes: 3 additions & 0 deletions src/view/input/cli_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub enum CliCommandChoice {
Rename {
request_name: String,
new_name: String,
no_confirm: bool,
},
Inspect {
request_name: String,
Expand Down Expand Up @@ -72,11 +73,13 @@ impl CliInput {
let inputs = clap_args_utils::get_many_inputs(matches)?;
let request_name = inputs[0].to_string();
let new_name = inputs[1].to_string();
let no_confirm = *matches.get_one::<bool>("no-confirm").unwrap_or(&false);

Ok(CliInput {
choice: CliCommandChoice::Rename {
request_name,
new_name,
no_confirm,
},
request_input,
save_options,
Expand Down
2 changes: 2 additions & 0 deletions src/view/input_parsers/main_command_choices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ pub fn parse_inputs_to_main_command_choices(
CliCommandChoice::Rename {
request_name,
new_name,
no_confirm,
} => vec![ViewCommandChoice::RenameSavedRequest {
request_name: request_name.to_string(),
new_name: new_name.to_string(),
no_confirm: *no_confirm,
}],
CliCommandChoice::Edit { request_name } => {
vec![ViewCommandChoice::SaveRequestWithBaseRequest {
Expand Down
Loading