pub mod extensions;
pub mod secrets;
use crate::cmds::accounts::get_current_account;
use crate::cmds::host::extensions::Extensions;
use crate::cmds::host::secrets::Secrets;
use crate::cmds::logs::{Logs, Sync, print_logs_metadata_table};
use crate::{Accounts, HostCallFlags, deploy_host_local};
use anyhow::bail;
use clap::Subcommand;
use clio::ClioPath;
use ordinary_config::OrdinaryHostConfig;
use ordinaryd::{AppHost, GlobalArgs};
use saferlmdb::Environment;
use std::env::home_dir;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tracing::instrument;
#[derive(Subcommand, Debug)]
pub enum Host {
#[clap(visible_aliases(["create", "init"]))]
New {
domain: String,
#[arg(long)]
dir: Option<PathBuf>,
},
Build {
#[arg(short, long, default_value_t = false)]
force: bool,
#[arg(short, long, value_parser = clap::value_parser!(ClioPath).exists().is_dir(), default_value = ".")]
project: ClioPath,
},
#[clap(visible_aliases(["run", "preview"]))]
Start {
#[command(flatten)]
app_host: AppHost,
#[command(flatten)]
global_args: GlobalArgs,
#[arg(short, long, default_value_t = false)]
disable_defaults: bool,
},
Publish {
#[command(flatten)]
host: HostCallFlags,
#[arg(short, long, value_parser = clap::value_parser!(ClioPath).exists().is_dir(), default_value = ".")]
project: ClioPath,
},
Deploy {
#[command(flatten)]
host: HostCallFlags,
#[arg(short, long, value_parser = clap::value_parser!(ClioPath).exists().is_dir(), default_value = ".")]
project: ClioPath,
#[arg(short, long, default_value_t = false)]
local: bool,
},
#[clap(visible_aliases(["ext", "extension"]))]
Extensions {
#[command(subcommand)]
extensions: Extensions,
#[arg(short, long, value_parser = clap::value_parser!(ClioPath).exists().is_dir(), default_value = ".")]
project: ClioPath,
},
Secrets {
#[command(subcommand)]
secrets: Secrets,
#[arg(short, long, value_parser = clap::value_parser!(ClioPath).exists().is_dir(), default_value = ".")]
project: ClioPath,
},
Logs {
#[command(flatten)]
host: HostCallFlags,
#[command(subcommand)]
logs: Logs,
},
#[clap(visible_aliases(["account", "accts", "acct"]))]
Accounts {
#[command(flatten)]
host: HostCallFlags,
#[command(subcommand)]
accounts: Accounts,
},
Moderation {
#[command(flatten)]
host: HostCallFlags,
#[command(subcommand)]
moderation: Moderation,
},
}
#[derive(Subcommand, Debug)]
pub enum Moderation {
Lock {
#[arg(short, long)]
account: String,
},
Unlock {
#[arg(short, long)]
account: String,
},
}
impl Host {
#[expect(clippy::missing_panics_doc, clippy::too_many_lines)]
#[instrument(skip_all, name = "host")]
pub async fn handle(&self, env: &Arc<Environment>) -> anyhow::Result<()> {
match self {
Self::New { domain, dir } => ordinary_modify::host::new(dir.as_deref(), domain)?,
Self::Build { force, project } => {
let project = project.to_str().expect("failed to get string from project");
let env_file = Path::new(project).join(".env");
if env_file.exists() {
dotenvy::from_path(env_file)?;
}
let env_file = Path::new(project).join(".env");
if env_file.exists() {
dotenvy::from_path(env_file)?;
}
ordinary_build::host(project, *force)?;
}
Self::Publish { host, project } => {
let project = project.to_str().expect("failed to get string from project");
let env_file = Path::new(project).join(".env");
if env_file.exists() {
dotenvy::from_path(env_file)?;
}
ordinary_build::host(project, true)?;
let config = OrdinaryHostConfig::get(project, true)?;
let account = get_current_account()?;
let client = host.client(&account, env)?;
client.host_deploy(project).await?;
if !config.extensions.is_empty() {
client.extensions_upload_all(project).await?;
}
}
Self::Deploy {
host,
project,
local,
} => {
let project = project.to_str().expect("failed to get string from project");
if *local {
let data_dir = match home_dir() {
Some(home) => home.join(".ordinary"),
None => bail!("no home dir"),
};
let config = OrdinaryHostConfig::get(project, true)?;
config.validate()?;
deploy_host_local(data_dir, project, &config)?;
} else {
let account = get_current_account()?;
let client = host.client(&account, env)?;
let env_file = Path::new(project).join(".env");
if env_file.exists() {
dotenvy::from_path(env_file)?;
}
client.host_deploy(project).await?;
}
tracing::info!("host deployed");
}
Self::Extensions {
extensions,
project,
} => {
let project = project.to_str().expect("failed to get string from project");
extensions.handle(project, env).await?;
}
Self::Secrets { secrets, project } => {
let project = project.to_str().expect("failed to get string from project");
secrets.handle(project, env).await?;
}
Self::Logs { logs, host } => {
let account = get_current_account()?;
let client = host.client(&account, env)?;
match logs {
Logs::Query {
query,
json_columns,
sync,
} => {
if sync == &Some(true) {
client.root_logs_sync(None, None).await?;
}
let res = client.root_logs_query(query, json_columns.as_ref())?;
print!("{res}");
}
Logs::List => client.root_logs_file_list()?,
Logs::Read { name } => {
if let Some(name) = name {
client.root_logs_file_read(name)?;
} else {
client.root_logs_file_read_all()?;
}
}
Logs::Sync { sync } => match sync {
Sync::Info => {
let remote_metadata = client.root_logs_remote_metadata().await?;
let local_metadata = client.root_logs_local_metadata()?;
print_logs_metadata_table(remote_metadata, local_metadata);
}
Sync::All { force } => {
client.root_logs_sync(*force, None).await?;
}
Sync::File { name } => {
client.root_logs_sync(None, Some(name)).await?;
}
},
}
}
Self::Moderation { moderation, host } => {
let account = get_current_account()?;
let client = host.client(&account, env)?;
match moderation {
Moderation::Lock { account } => {
client.root_lock_account(account.as_str()).await?;
}
Moderation::Unlock { account } => {
client.root_unlock_account(account.as_str()).await?;
}
}
}
Self::Accounts { accounts, host } => {
accounts
.handle(
host.host_domain.as_deref(),
host.danger_accept_invalid_certs,
host.insecure,
env,
)
.await?;
}
Self::Start { .. } => unreachable!("handled in main.rs"),
}
Ok(())
}
}