Skip to content

Commit

Permalink
feat: Add blueprint:list command to scotty cli
Browse files Browse the repository at this point in the history
  • Loading branch information
stmh committed Dec 30, 2024
1 parent 4cb6dea commit f7f0184
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
10 changes: 1 addition & 9 deletions src/api/handlers/blueprints.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
use std::collections::HashMap;

use axum::{debug_handler, extract::State, response::IntoResponse, Json};
use serde::{Deserialize, Serialize};

use crate::{app_state::SharedAppState, settings::app_blueprint::AppBlueprint};

#[derive(Debug, Clone, Serialize, Deserialize, utoipa::ToSchema, utoipa::ToResponse)]
pub struct AppBlueprintList {
pub blueprints: HashMap<String, AppBlueprint>,
}
use crate::{app_state::SharedAppState, settings::app_blueprint::AppBlueprintList};

#[debug_handler]
#[utoipa::path(
Expand Down
44 changes: 43 additions & 1 deletion src/scottyctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ use notification_types::{
RemoveNotificationRequest, WebhookContext,
};
use owo_colors::OwoColorize;
use tabled::{builder::Builder, settings::Style};
use settings::app_blueprint::AppBlueprintList;
use tabled::{
builder::Builder,
settings::{object::Columns, Style, Width},
};
use tasks::{
running_app_context::RunningAppContext,
task_details::{State, TaskDetails},
Expand Down Expand Up @@ -87,8 +91,15 @@ enum Commands {
/// remove notificattions to other services
#[command(name = "notify:remove")]
NotifyRemove(NotifyRemoveCommand),

/// List all available blueprints
#[command(name = "blueprint:list")]
BlueprintList,
}

#[derive(Debug, Parser)]
struct BlueprintListCommand {}

#[derive(Debug, Parser)]
struct RunCommand {
/// Name of the app
Expand Down Expand Up @@ -350,6 +361,10 @@ async fn main() -> anyhow::Result<()> {
Commands::NotifyRemove(cmd) => {
remove_notification(&server_settings, cmd).await?;
}

Commands::BlueprintList => {
list_blueprints(&server_settings).await?;
}
}
Ok(())
}
Expand Down Expand Up @@ -680,3 +695,30 @@ async fn remove_notification(
print_app_info(&app_data)?;
Ok(())
}

async fn list_blueprints(server: &ServerSettings) -> anyhow::Result<()> {
let result = get(server, "blueprints").await?;
let blueprints: AppBlueprintList = serde_json::from_value(result)?;

let mut builder = Builder::default();
builder.push_record(vec!["Id", "Name", "Description", "Required Services"]);
for blueprint in blueprints.blueprints {
let id = blueprint.0;
let blueprint = blueprint.1;
builder.push_record(vec![
&id,
&blueprint.name,
&blueprint.description,
&blueprint.required_services.join(", "),
]);
}

let mut table = builder.build();
table.with(Style::modern_rounded());
table.modify(Columns::single(0), Width::wrap(15).keep_words(true));
table.modify(Columns::single(1), Width::wrap(15).keep_words(true));
table.modify(Columns::single(3), Width::wrap(10).keep_words(true));

println!("{}", table);
Ok(())
}
5 changes: 5 additions & 0 deletions src/settings/app_blueprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ pub struct AppBlueprintValidationError {
msg: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, utoipa::ToSchema, utoipa::ToResponse)]
pub struct AppBlueprintList {
pub blueprints: HashMap<String, AppBlueprint>,
}

// The error type has to implement Display
impl std::fmt::Display for AppBlueprintValidationError {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand Down

0 comments on commit f7f0184

Please sign in to comment.