hive-forge: make --json a global flag (closes #421)
This commit is contained in:
parent
3cd9240594
commit
d0a1ee037b
5 changed files with 33 additions and 16 deletions
|
|
@ -26,14 +26,19 @@ pub struct Client {
|
|||
/// Default repo used when a verb doesn't carry an explicit
|
||||
/// `[repo]` override.
|
||||
pub default_repo: String,
|
||||
/// Global `--json` flag — verbs that have a human-readable
|
||||
/// default path branch on `client.json_mode()` to pick the
|
||||
/// JSON output shape instead. Closes #421.
|
||||
json_mode: bool,
|
||||
}
|
||||
|
||||
impl Client {
|
||||
/// Build a client from the standard environment variables.
|
||||
/// `repo_override` (from the global `-r/--repo` flag) takes
|
||||
/// priority over `HIVE_FORGE_REPO`; the env var is the fallback
|
||||
/// default.
|
||||
pub fn from_env(repo_override: Option<String>) -> Result<Self> {
|
||||
/// default. `json_mode` comes from the global `--json` flag —
|
||||
/// per-verb output formatters key off it via `Client::json_mode`.
|
||||
pub fn from_env(repo_override: Option<String>, json_mode: bool) -> Result<Self> {
|
||||
let base = std::env::var("HIVE_FORGE_URL").unwrap_or_else(|_| DEFAULT_URL.to_owned());
|
||||
let default_repo = repo_override
|
||||
.or_else(|| std::env::var("HIVE_FORGE_REPO").ok())
|
||||
|
|
@ -55,9 +60,19 @@ impl Client {
|
|||
http,
|
||||
base,
|
||||
default_repo,
|
||||
json_mode,
|
||||
})
|
||||
}
|
||||
|
||||
/// True when the operator passed the global `--json` flag.
|
||||
/// Verbs that have a human-readable default branch on this to
|
||||
/// emit JSON instead. Verbs whose only output format is JSON
|
||||
/// (e.g. `issue`, `pr`) can ignore it.
|
||||
#[must_use]
|
||||
pub fn json_mode(&self) -> bool {
|
||||
self.json_mode
|
||||
}
|
||||
|
||||
/// Resolve the API base path (`<base>/api/v1`).
|
||||
fn api(&self) -> String {
|
||||
format!("{}/api/v1", self.base)
|
||||
|
|
|
|||
Loading…
Reference in a new issue