Skip to content

Commit

Permalink
[refactor]: remove UserInterface struct
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Balashov <[email protected]>
  • Loading branch information
0x009922 committed Aug 29, 2023
1 parent fa45c2e commit 7e451be
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 33 deletions.
6 changes: 2 additions & 4 deletions tools/swarm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ mod util;
use clap::Parser;
use cli::Cli;
use color_eyre::{eyre::Context, Result};
use ui::UserInterface;
use util::AbsolutePath;

use crate::{cli::SourceParsed, compose::ResolvedImageSource};

fn main() -> Result<()> {
color_eyre::install()?;
let ui = UserInterface::new();

let Cli {
peers,
Expand All @@ -38,7 +36,7 @@ fn main() -> Result<()> {
let config_dir = AbsolutePath::absolutize(&config_dir_raw)?;

if target_file.exists() && !force {
if let ui::PromptAnswer::No = ui.prompt_remove_target_file(&target_file)? {
if let ui::PromptAnswer::No = ui::prompt_remove_target_file(&target_file)? {
return Ok(());
}
}
Expand All @@ -52,7 +50,7 @@ fn main() -> Result<()> {
}
.build_and_write()?;

ui.log_file_mode_complete(&target_file, &target_file_raw);
ui::log_file_mode_complete(&target_file, &target_file_raw);

Ok(())
}
48 changes: 19 additions & 29 deletions tools/swarm/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ use owo_colors::OwoColorize;
use super::Result;
use crate::util::AbsolutePath;

pub(super) struct UserInterface;

pub(super) enum PromptAnswer {
pub enum PromptAnswer {
Yes,
No,
}
Expand All @@ -23,32 +21,24 @@ impl From<bool> for PromptAnswer {
}
}

impl UserInterface {
pub(super) fn new() -> Self {
Self
}

#[allow(clippy::unused_self)]
pub(super) fn prompt_remove_target_file(&self, file: &AbsolutePath) -> Result<PromptAnswer> {
inquire::Confirm::new(&format!(
"File {} already exists. Remove it?",
file.display().blue().bold()
))
.with_default(false)
.prompt()
.suggestion("You can pass `--force` flag to remove the file anyway")
.map(PromptAnswer::from)
}
pub fn prompt_remove_target_file(file: &AbsolutePath) -> Result<PromptAnswer> {
inquire::Confirm::new(&format!(
"File {} already exists. Remove it?",
file.display().blue().bold()
))
.with_default(false)
.prompt()
.suggestion("You can pass `--force` flag to remove the file anyway")
.map(PromptAnswer::from)
}

#[allow(clippy::unused_self)]
pub(super) fn log_file_mode_complete(&self, file: &AbsolutePath, file_raw: &Path) {
println!(
"✓ Docker compose configuration is ready at:\n\n {}\
pub fn log_file_mode_complete(file: &AbsolutePath, file_raw: &Path) {
println!(
"✓ Docker compose configuration is ready at:\n\n {}\
\n\n You could run `{} {} {}`",
file.display().green().bold(),
"docker compose -f".blue(),
file_raw.display().blue().bold(),
"up".blue(),
);
}
file.display().green().bold(),
"docker compose -f".blue(),
file_raw.display().blue().bold(),
"up".blue(),
);
}

0 comments on commit 7e451be

Please sign in to comment.