added interactive mode (#44)
Co-authored-by: kjuulh <contact@kjuulh.io> Reviewed-on: #44
This commit is contained in:
@@ -5,3 +5,4 @@ pub mod schema;
|
||||
pub mod selectors;
|
||||
mod shell;
|
||||
pub mod storage;
|
||||
pub mod ui;
|
||||
|
@@ -6,18 +6,21 @@ use crate::{
|
||||
executor::executor::DynExecutor,
|
||||
git::DynGitProvider,
|
||||
schema::models::{Action, Git},
|
||||
ui::DynUI,
|
||||
};
|
||||
|
||||
pub struct GitSelector {
|
||||
git_provider: DynGitProvider,
|
||||
executor: DynExecutor,
|
||||
ui: DynUI,
|
||||
}
|
||||
|
||||
impl GitSelector {
|
||||
pub fn new(git_provider: DynGitProvider, executor: DynExecutor) -> Self {
|
||||
pub fn new(git_provider: DynGitProvider, executor: DynExecutor, ui: DynUI) -> Self {
|
||||
Self {
|
||||
git_provider,
|
||||
executor,
|
||||
ui,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +30,7 @@ impl GitSelector {
|
||||
action_path: &PathBuf,
|
||||
action: &Action,
|
||||
dryrun: bool,
|
||||
interactive: bool,
|
||||
) -> eyre::Result<()> {
|
||||
tracing::info!("fetching repos");
|
||||
for repo in &git.repositories {
|
||||
@@ -46,6 +50,10 @@ impl GitSelector {
|
||||
continue;
|
||||
}
|
||||
|
||||
if interactive {
|
||||
self.ui.confirm().await?;
|
||||
}
|
||||
|
||||
if let Some(push) = &git.push {
|
||||
self.git_provider
|
||||
.push_branch(repo, &push.branch.name)
|
||||
|
@@ -6,12 +6,14 @@ use crate::{
|
||||
executor::executor::DynExecutor,
|
||||
git::{gitea::DynGiteaProvider, DynGitProvider},
|
||||
schema::models::{Action, Gitea},
|
||||
ui::DynUI,
|
||||
};
|
||||
|
||||
pub struct GiteaSelector {
|
||||
gitea_provider: DynGiteaProvider,
|
||||
git_provider: DynGitProvider,
|
||||
executor: DynExecutor,
|
||||
ui: DynUI,
|
||||
}
|
||||
|
||||
impl GiteaSelector {
|
||||
@@ -19,11 +21,13 @@ impl GiteaSelector {
|
||||
gitea_provider: DynGiteaProvider,
|
||||
git_provider: DynGitProvider,
|
||||
executor: DynExecutor,
|
||||
ui: DynUI,
|
||||
) -> Self {
|
||||
Self {
|
||||
gitea_provider,
|
||||
git_provider,
|
||||
executor,
|
||||
ui,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +37,7 @@ impl GiteaSelector {
|
||||
action_path: &PathBuf,
|
||||
action: &Action,
|
||||
dryrun: bool,
|
||||
interactive: bool,
|
||||
) -> eyre::Result<()> {
|
||||
tracing::info!("fetching repos");
|
||||
for repo in &git.repositories {
|
||||
@@ -52,6 +57,10 @@ impl GiteaSelector {
|
||||
continue;
|
||||
}
|
||||
|
||||
if interactive {
|
||||
self.ui.confirm().await?;
|
||||
}
|
||||
|
||||
if let Some(push) = &git.push {
|
||||
self.git_provider
|
||||
.push_branch(repo, &push.pull_request.name)
|
||||
|
@@ -6,12 +6,14 @@ use crate::{
|
||||
executor::executor::DynExecutor,
|
||||
git::{github::DynGitHubProvider, DynGitProvider},
|
||||
schema::models::{Action, GitHub},
|
||||
ui::DynUI,
|
||||
};
|
||||
|
||||
pub struct GitHubSelector {
|
||||
github_provider: DynGitHubProvider,
|
||||
git_provider: DynGitProvider,
|
||||
executor: DynExecutor,
|
||||
ui: DynUI,
|
||||
}
|
||||
|
||||
impl GitHubSelector {
|
||||
@@ -19,11 +21,13 @@ impl GitHubSelector {
|
||||
github_provider: DynGitHubProvider,
|
||||
git_provider: DynGitProvider,
|
||||
executor: DynExecutor,
|
||||
ui: DynUI,
|
||||
) -> Self {
|
||||
Self {
|
||||
github_provider,
|
||||
git_provider,
|
||||
executor,
|
||||
ui,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +37,7 @@ impl GitHubSelector {
|
||||
action_path: &PathBuf,
|
||||
action: &Action,
|
||||
dryrun: bool,
|
||||
interactive: bool,
|
||||
) -> eyre::Result<()> {
|
||||
tracing::info!("fetching repos");
|
||||
for repo in &git.repositories {
|
||||
@@ -52,6 +57,10 @@ impl GitHubSelector {
|
||||
continue;
|
||||
}
|
||||
|
||||
if interactive {
|
||||
self.ui.confirm().await?;
|
||||
}
|
||||
|
||||
if let Some(push) = &git.push {
|
||||
self.git_provider
|
||||
.push_branch(repo, &push.pull_request.name)
|
||||
|
12
crates/octopush_core/src/ui/mod.rs
Normal file
12
crates/octopush_core/src/ui/mod.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
pub mod terminal_ui;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use async_trait::async_trait;
|
||||
|
||||
#[async_trait]
|
||||
pub trait UI {
|
||||
async fn confirm(&self) -> eyre::Result<()>;
|
||||
}
|
||||
|
||||
pub type DynUI = Arc<dyn UI + Send + Sync>;
|
47
crates/octopush_core/src/ui/terminal_ui.rs
Normal file
47
crates/octopush_core/src/ui/terminal_ui.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use std::{
|
||||
io::{self, Write},
|
||||
process::exit,
|
||||
};
|
||||
|
||||
use async_trait::async_trait;
|
||||
|
||||
use super::UI;
|
||||
|
||||
pub struct TerminalUI {}
|
||||
|
||||
impl TerminalUI {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
|
||||
pub async fn query_console(&self) -> eyre::Result<()> {
|
||||
print!("Continue? ([Y]es/[N]o): ");
|
||||
std::io::stdout().lock().flush()?;
|
||||
let mut input = String::new();
|
||||
let _ = io::stdin().read_line(&mut input)?;
|
||||
|
||||
if input.to_lowercase().starts_with("y") {
|
||||
return Ok(());
|
||||
} else if input.to_lowercase().starts_with("n") {
|
||||
exit(0)
|
||||
} else {
|
||||
Err(eyre::anyhow!("input not valid"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl UI for TerminalUI {
|
||||
async fn confirm(&self) -> eyre::Result<()> {
|
||||
match self.query_console().await {
|
||||
Ok(_) => Ok(()),
|
||||
Err(e) => {
|
||||
if e.to_string().starts_with("input not valid") {
|
||||
self.query_console().await
|
||||
} else {
|
||||
Err(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user