Skip to content

Commit

Permalink
feat: update to move cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
pxseu committed Oct 16, 2023
1 parent f70aa54 commit 7673fbc
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 34 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async-compression = { version = "0.4", features = ["tokio", "gzip"] }

# *nix only deps
[target.'cfg(all(not(windows), not(macos)))'.dependencies]
hop = { version = "0.1.9", features = [
hop = { version = "0.1", features = [
"chrono",
"rustls-tls-webpki-roots",
], default-features = false }
Expand Down
8 changes: 4 additions & 4 deletions src/commands/ignite/groups/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod add;
mod create;
mod delete;
mod list;
mod r#move;
pub mod utils;

use anyhow::Result;
Expand All @@ -15,8 +15,8 @@ pub enum Commands {
Create(create::Options),
#[clap(name = "rm", alias = "delete")]
Delete(delete::Options),
#[clap(name = "add", alias = "add-deployment")]
Add(add::Options),
#[clap(name = "move", alias = "add-deployment", alias = "add")]
Move(r#move::Options),
List(list::Options),
}

Expand All @@ -32,7 +32,7 @@ pub async fn handle(options: Options, state: State) -> Result<()> {
match options.commands {
Commands::Create(options) => create::handle(options, state).await,
Commands::Delete(options) => delete::handle(options, state).await,
Commands::Add(options) => add::handle(options, state).await,
Commands::Move(options) => r#move::handle(options, state).await,
Commands::List(options) => list::handle(options, state).await,
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use anyhow::{ensure, Result};
use clap::Parser;
use console::style;

use crate::commands::ignite::groups::utils::{fetch_grouped_deployments, format_groups};
use crate::commands::ignite::utils::get_deployment;
use crate::config::EXEC_NAME;
use crate::state::State;

#[derive(Debug, Parser)]
#[clap(about = "Add a deployment to an Ignite group")]
#[clap(about = "Move a Deployment to a group")]
#[group(skip)]
pub struct Options {
#[clap(help = "The ID of the group")]
#[clap(help = "The ID of the group, or \"none\" to remove from a group")]
pub group: Option<String>,
#[clap(help = "Deployment ID to add")]
pub deployment: Option<String>,
Expand All @@ -18,29 +20,8 @@ pub struct Options {
pub async fn handle(options: Options, state: State) -> Result<()> {
let project = state.ctx.current_project_error()?;

let group = if let Some(group) = options.group {
group
} else {
let mut groups = state.hop.ignite.groups.get_all(&project.id).await?;

ensure!(
!groups.is_empty(),
"No groups found, create one with `{EXEC_NAME} ignite groups create`"
);

groups.sort_unstable_by_key(|group| group.position);

let dialoguer_groups = dialoguer::Select::new()
.with_prompt("Select group")
.default(0)
.items(&format_groups(&groups)?)
.interact()?;

groups[dialoguer_groups].id.clone()
};

let deployments = if let Some(deployment) = options.deployment {
deployment
let deployment = if let Some(deployment) = options.deployment {
get_deployment(&state.http, &deployment).await?
} else {
let (deployments_fmt, deployments, validator) =
fetch_grouped_deployments(&state, false, true).await?;
Expand All @@ -59,14 +40,49 @@ pub async fn handle(options: Options, state: State) -> Result<()> {
console::Term::stderr().clear_last_lines(1)?
};

deployments[idx].id.clone()
deployments[idx].to_owned()
};

let group = if let Some(group) = options.group {
if group.is_empty() || ["none", "null"].contains(&group.to_lowercase().as_str()) {
None
} else {
Some(group)
}
} else {
let mut groups = state.hop.ignite.groups.get_all(&project.id).await?;

ensure!(
!groups.is_empty(),
"No groups found, create one with `{EXEC_NAME} ignite groups create`"
);

groups.sort_unstable_by_key(|group| group.position);

let mut formated = format_groups(&groups)?;

if deployment.group_id.is_some() {
formated.push(style("None (remove from a group)").white().to_string());
}

let dialoguer_groups = dialoguer::Select::new()
.with_prompt("Select group")
.default(0)
.items(&formated)
.interact()?;

if dialoguer_groups == groups.len() - 1 {
None
} else {
Some(groups[dialoguer_groups].id.clone())
}
};

state
.hop
.ignite
.groups
.add_deployment(&group, &deployments)
.move_deployment(group.as_deref(), &deployment.id)
.await?;

log::info!("Added deployment to group");
Expand Down

0 comments on commit 7673fbc

Please sign in to comment.