ordinary 0.11.1

Ordinary CLI
Documentation
// Copyright (C) 2026 The Ordinary Authors.
//
// SPDX-License-Identifier: BSD-3-Clause

use crate::HostCallFlags;
use crate::cmds::accounts::get_current_account;
use clap::Subcommand;
use tracing::instrument;

#[derive(Subcommand, Debug)]
pub enum Assets {
    /// write assets to application running on `ordinaryd` instance
    Write {
        #[command(flatten)]
        host: HostCallFlags,

        /// name of a specific asset to write (optional).
        /// will write all when the `--name` flag is not passed.
        #[arg(short, long)]
        name: Option<String>,
    },
    /// prune all assets on the server that are not known locally
    Prune {
        #[command(flatten)]
        host: HostCallFlags,
    },
    /// deletes assets on the server
    ///
    /// when no flags are passed, all assets are deleted.
    #[clap(visible_aliases(["del"]))]
    Delete {
        #[command(flatten)]
        host: HostCallFlags,

        /// which items to skip when deleting all
        #[arg(long, value_delimiter = ',', num_args = 1..)]
        skip: Option<Vec<String>>,
    },
}

impl Assets {
    #[instrument(skip_all, name = "assets")]
    pub async fn handle(&self, project: &str) -> anyhow::Result<()> {
        match self {
            Self::Write { name, host } => {
                let account = get_current_account(host.insecure)?;
                let client = host.client(&account)?;

                if let Some(name) = name {
                    client.assets_write(project, name).await?;
                } else {
                    client.assets_write_all(project).await?;
                }
            }
            Self::Prune { host } => {
                let account = get_current_account(host.insecure)?;
                let client = host.client(&account)?;

                client.assets_prune(project).await?;
            }
            Self::Delete { skip, host } => {
                let account = get_current_account(host.insecure)?;
                let client = host.client(&account)?;

                client
                    .assets_delete_all(project, skip.clone().unwrap_or_default())
                    .await?;
            }
        }

        Ok(())
    }
}