Skip to main content

ordinary/cmds/
templates.rs

1// Copyright (C) 2026 Ordinary Labs, LLC.
2//
3// SPDX-License-Identifier: AGPL-3.0-only
4
5use crate::cmds::accounts::get_current_account;
6use clap::Subcommand;
7use ordinary_api::client::OrdinaryApiClient;
8
9#[derive(Subcommand, Debug)]
10pub enum Templates {
11    /// add a new template to the Ordinary project
12    Add {
13        /// template name
14        name: String,
15        /// HTTP route (must start with leading "/")
16        route: String,
17        /// MIME type for template output:
18        /// [reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/MIME_types/Common_types)
19        mime: String,
20        /// whether the template is an error template
21        #[arg(short, long)]
22        error: Option<bool>,
23    },
24    /// upload templates to application running on `ordinaryd` instance
25    Upload {
26        /// name of a specific template to upload (optional).
27        /// will upload all when the `--name` flag is not passed.
28        #[arg(short, long)]
29        name: Option<String>,
30    },
31}
32
33impl Templates {
34    pub async fn handle(
35        &self,
36        api_domain: Option<&str>,
37        accept_invalid_certs: bool,
38        project: &str,
39        insecure: bool,
40    ) -> anyhow::Result<()> {
41        let account = get_current_account(insecure)?;
42        let client = OrdinaryApiClient::new(
43            &account.host,
44            &account.name,
45            api_domain,
46            accept_invalid_certs,
47            crate::USER_AGENT,
48            false,
49        )?;
50
51        match self {
52            Self::Add {
53                name,
54                route,
55                mime,
56                error,
57            } => {
58                ordinary_modify::add_template(
59                    project,
60                    name,
61                    route,
62                    mime,
63                    "",
64                    "",
65                    "",
66                    error.unwrap_or(false),
67                    None,
68                    None,
69                )?;
70            }
71            Self::Upload { name } => {
72                if let Some(name) = name {
73                    client.upload(project, name).await?;
74                } else {
75                    client.upload_all(project).await?;
76                }
77            }
78        }
79
80        Ok(())
81    }
82}