boomack-cli 0.1.0

CLI client for Boomack
use clap::{Args, Subcommand};
use serde::Serialize;
use boomack::client::panels::{
    get_panel_ids_request,
    get_panel_layout_request,
    set_panel_layout_request,
    clear_panel_request,
    PanelExportParameters,
    export_panel_request,
    delete_panel_request,
};
use super::structs::load_structure;
use super::{CliConfig, run_request};

#[derive(Args, Serialize)]
pub struct PanelCommandArguments {
    #[clap(subcommand)]
    command: Option<PanelSubCommands>,
}

#[derive(Subcommand, Serialize)]
pub enum PanelSubCommands {
    #[clap(name = "ls", about = "List all panels")]
    List,

    #[clap(about = "Get the layout of a panel")]
    Get(PanelIdArguments),

    #[clap(about = "Create new panel with given layout")]
    Add(PanelLayoutUpdateArguments),

    #[clap(name = "layout", about = "Set the layout of a panel")]
    Set(PanelLayoutUpdateArguments),

    #[clap(about = "Clear all slots in a panel")]
    Clear(PanelIdArguments),

    #[clap(about = "Export panel content to a path on the server")]
    Export(PanelExportArguments),

    #[clap(alias = "rm", about = "Delete a panel")]
    Delete(PanelDeleteArguments),
}

#[derive(Args, Serialize)]
pub struct PanelIdArguments {
    #[clap(value_name = "PANEL_ID",
        help = "The ID of the panel; defaults to 'default'")]
    panel_id: Option<String>,
}

#[derive(Args, Serialize)]
pub struct PanelLayoutUpdateArguments {
    #[clap(value_name = "PANEL_ID",
        help = "The ID of the panel")]
    panel_id: String,

    #[clap(value_name = "LAYOUT",
        help = "A YAML/JSON encoded layout request, a path to a JSON/YAML file, or 'STDIN'")]
    layout: String,
}

#[derive(Args, Serialize)]
pub struct PanelExportArguments {
    #[clap(value_name = "PANEL_ID",
        help = "The ID of the panel; defaults to 'default'")]
    panel_id: Option<String>,

    #[clap(short = 'p', long = "path", value_name = "SERVER_PATH",
        help = "A relative path in the filesystem of the server to export to")]
    path: Option<String>,

    #[clap(short = 't', long = "theme", value_name = "THEME",
        help = "A theme to use during the export")]
    theme: Option<String>,
}

impl<'l> PanelExportArguments {
    pub fn to_parameters(&'l self) -> PanelExportParameters<'l> {
        PanelExportParameters {
            path: self.path.as_deref(),
            theme: self.theme.as_deref(),
        }
    }
}

#[derive(Args, Serialize)]
pub struct PanelDeleteArguments {
    #[clap(value_name = "PANEL_ID",
        help = "The ID of the panel")]
    panel_id: String,
}

pub fn dispatch_panel_command(cfg: &CliConfig, args: &PanelCommandArguments) -> i32 {
    let mut stdin_consumed = false;
    let request = if let Some(sub_command) = &args.command {
        match sub_command {
            PanelSubCommands::List => get_panel_ids_request(),
            PanelSubCommands::Get(args) => get_panel_layout_request(args.panel_id.as_deref()),
            PanelSubCommands::Add(args) => set_panel_layout_request(&args.panel_id,
                load_structure(&args.layout, true, &mut stdin_consumed)),
            PanelSubCommands::Set(args) => set_panel_layout_request(&args.panel_id,
                load_structure(&args.layout, true, &mut stdin_consumed)),
            PanelSubCommands::Export(args) => export_panel_request(args.panel_id.as_deref(),
                args.to_parameters()),
            PanelSubCommands::Clear(args) => clear_panel_request(args.panel_id.as_deref()),
            PanelSubCommands::Delete(args) => delete_panel_request(&args.panel_id),
        }
    } else {
        get_panel_ids_request()
    };
    run_request(cfg, request)
}