use anyhow::{Context, Result};
use harmont_cloud::HarmontClient;
#[derive(Debug, Clone)]
pub struct ResolvedCtx {
pub api: String,
pub org: Option<String>,
}
impl ResolvedCtx {
pub fn org(&self) -> Result<String> {
self.org.clone().context(
"no organization — set `[cloud] org = \"…\"` in ~/.config/hm/config.toml (or .hm/config.toml), or run `hm cloud org switch <slug>`")
}
}
pub fn client() -> Result<(HarmontClient, ResolvedCtx)> {
let cfg = hm_config::Config::load(None).context("loading config")?; let api = cfg.cloud.api_url.clone();
let token = hm_config::creds::cloud_token(&api)
.context("not logged in — run `hm cloud login` or set HM_API_TOKEN")?;
let client = HarmontClient::with_base_url(token, &api);
Ok((
client,
ResolvedCtx {
api,
org: cfg.cloud.org,
},
))
}
pub fn anon_client() -> Result<(HarmontClient, String)> {
let cfg = hm_config::Config::load(None).context("loading config")?;
let api = cfg.cloud.api_url.clone();
Ok((HarmontClient::anonymous(&api), api))
}
#[derive(Debug, Clone, Copy)]
pub struct RenderPrefs {
pub color: bool,
pub logs: bool,
}
impl RenderPrefs {
#[must_use]
pub fn detect() -> Self {
Self {
color: hm_render::color_enabled(false),
logs: hm_render::stdout_piped(),
}
}
}
pub fn map_raw<E: std::fmt::Debug>(e: harmont_cloud_raw::Error<E>) -> anyhow::Error {
anyhow::anyhow!("{e}")
}