cardanowall-cli 0.2.0

The cardanowall CLI: a standalone Label 309 Proof-of-Existence verifier and toolkit.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! `cardanowall gateway` — manage named service-gateway profiles.
//!
//! A profile pairs a service-gateway `base_url` with an optional opaque `api_key`
//! and is stored under `[gateways.<name>]` in `config.toml` (created `0600`). This
//! is NOT a login: the gateway API is key-only, so a profile is just a saved
//! endpoint-plus-key the network commands resolve when no `--base-url` / env is
//! given.
//!
//! Verbs:
//!
//! - `add <name> --base-url <url> [--api-key-stdin]` — when `--api-key-stdin` is
//!   omitted and stdin is a TTY, the key is read from a hidden prompt.
//! - `use <name>`    — set `default_gateway = "<name>"`.
//! - `list`          — name + base_url + masked key + which is default.
//! - `show <name>`   — one profile (`--json` aware; key masked unless `--reveal`).
//! - `remove <name>` — drop a profile (and clear `default_gateway` if it pointed
//!   at it).
//!
//! Exit codes: `0` ok / `4` CLI input error (unknown profile, missing base URL,
//! malformed config).

use clap::{Args, Subcommand};

use crate::config::{
    load_config_for_edit, system_config_path, write_config, GatewayProfile, SystemConfigEnv,
};
use crate::secret::{SecretEnv, SystemSecretEnv};
use crate::util::CliError;

/// Arguments for `cardanowall gateway`.
#[derive(Debug, Args)]
pub struct GatewayArgs {
    /// The gateway-profile verb to run.
    #[command(subcommand)]
    pub verb: GatewayVerb,
}

/// The gateway-profile verbs.
#[derive(Debug, Subcommand)]
pub enum GatewayVerb {
    /// Save a named gateway profile (base URL + optional API key).
    Add(GatewayAddArgs),
    /// Set the active gateway profile by name.
    Use(GatewayUseArgs),
    /// List saved gateway profiles (API keys masked).
    List(GatewayListArgs),
    /// Show one gateway profile (API key masked unless --reveal).
    Show(GatewayShowArgs),
    /// Remove a saved gateway profile.
    Remove(GatewayRemoveArgs),
}

impl GatewayArgs {
    /// Whether the active verb was invoked with `--json`.
    #[must_use]
    pub fn json_mode(&self) -> bool {
        match &self.verb {
            GatewayVerb::Add(a) => a.json,
            GatewayVerb::Use(a) => a.json,
            GatewayVerb::List(a) => a.json,
            GatewayVerb::Show(a) => a.json,
            GatewayVerb::Remove(a) => a.json,
        }
    }
}

/// Run the `gateway` command.
///
/// # Errors
///
/// Returns [`CliError`] with the verb's mapped exit code.
pub fn run(args: GatewayArgs) -> Result<(), CliError> {
    run_with_env(args, &SystemConfigEnv, &SystemSecretEnv)
}

/// Test-friendly entry point: the config + secret env are injected so the suite
/// drives `add`/`list`/`use`/`remove` against a temp config and a fake prompt.
///
/// # Errors
///
/// Returns [`CliError`] with the verb's mapped exit code.
pub fn run_with_env(
    args: GatewayArgs,
    config_env: &dyn crate::config::ConfigEnv,
    secret_env: &dyn SecretEnv,
) -> Result<(), CliError> {
    match args.verb {
        GatewayVerb::Add(a) => run_add(a, config_env, secret_env),
        GatewayVerb::Use(a) => run_use(a, config_env),
        GatewayVerb::List(a) => run_list(a, config_env),
        GatewayVerb::Show(a) => run_show(a, config_env),
        GatewayVerb::Remove(a) => run_remove(a, config_env),
    }
}

// ===========================================================================
// gateway add
// ===========================================================================

/// Arguments for `cardanowall gateway add`.
#[derive(Debug, Args)]
pub struct GatewayAddArgs {
    /// the profile name (e.g. `prod`).
    pub name: String,
    /// the service-gateway base URL (required).
    #[arg(long = "base-url")]
    pub base_url: String,
    /// read the opaque API key from stdin (otherwise prompt when a TTY).
    #[arg(long = "api-key-stdin")]
    pub api_key_stdin: bool,
    /// emit a machine-readable JSON acknowledgement.
    #[arg(long)]
    pub json: bool,
}

fn run_add(
    args: GatewayAddArgs,
    config_env: &dyn crate::config::ConfigEnv,
    secret_env: &dyn SecretEnv,
) -> Result<(), CliError> {
    validate_name(&args.name)?;
    let base_url = args.base_url.trim().to_string();
    if base_url.is_empty() {
        return Err(CliError::input("gateway add: --base-url must not be empty"));
    }

    // The API key is moderately-secret: read it from stdin when asked, else from a
    // hidden prompt on a TTY, else leave it unset (a key-less public gateway).
    let api_key = if args.api_key_stdin {
        let raw = secret_env.read_stdin()?;
        let trimmed = raw.trim().to_string();
        if trimmed.is_empty() {
            None
        } else {
            Some(trimmed)
        }
    } else if secret_env.stdin_is_terminal() {
        let entered = secret_env.prompt_hidden(&format!(
            "Enter API key for gateway '{}' (blank = none): ",
            args.name
        ))?;
        let trimmed = entered.trim().to_string();
        if trimmed.is_empty() {
            None
        } else {
            Some(trimmed)
        }
    } else {
        None
    };

    let mut config = load_config_for_edit(config_env)?;
    config.gateways.insert(
        args.name.clone(),
        GatewayProfile {
            base_url: base_url.clone(),
            api_key,
        },
    );
    // First profile added becomes the default so the user need not also `use` it.
    if config.default_gateway.is_none() {
        config.default_gateway = Some(args.name.clone());
    }
    write_config(config_env, &config)?;

    let has_key = config
        .gateways
        .get(&args.name)
        .and_then(|p| p.api_key.as_ref())
        .is_some();
    if args.json {
        let value = serde_json::json!({
            "status": "ok",
            "name": args.name,
            "base_url": base_url,
            "has_api_key": has_key,
            "is_default": config.default_gateway.as_deref() == Some(args.name.as_str()),
        });
        println!("{value}");
    } else {
        println!("ok: saved gateway profile '{}' -> {base_url}", args.name);
        if config.default_gateway.as_deref() == Some(args.name.as_str()) {
            println!("  (set as the default gateway)");
        }
    }
    Ok(())
}

// ===========================================================================
// gateway use
// ===========================================================================

/// Arguments for `cardanowall gateway use`.
#[derive(Debug, Args)]
pub struct GatewayUseArgs {
    /// the profile name to make active.
    pub name: String,
    /// emit a machine-readable JSON acknowledgement.
    #[arg(long)]
    pub json: bool,
}

fn run_use(
    args: GatewayUseArgs,
    config_env: &dyn crate::config::ConfigEnv,
) -> Result<(), CliError> {
    let mut config = load_config_for_edit(config_env)?;
    if !config.gateways.contains_key(&args.name) {
        return Err(unknown_profile(&args.name, &config));
    }
    config.default_gateway = Some(args.name.clone());
    write_config(config_env, &config)?;
    if args.json {
        println!(
            "{}",
            serde_json::json!({ "status": "ok", "default_gateway": args.name })
        );
    } else {
        println!("ok: default gateway is now '{}'", args.name);
    }
    Ok(())
}

// ===========================================================================
// gateway list
// ===========================================================================

/// Arguments for `cardanowall gateway list`.
#[derive(Debug, Args)]
pub struct GatewayListArgs {
    /// emit a machine-readable JSON array.
    #[arg(long)]
    pub json: bool,
}

fn run_list(
    args: GatewayListArgs,
    config_env: &dyn crate::config::ConfigEnv,
) -> Result<(), CliError> {
    let config = load_config_for_edit(config_env)?;
    let default = config.default_gateway.as_deref();

    if args.json {
        let profiles: Vec<serde_json::Value> = config
            .gateways
            .iter()
            .map(|(name, p)| {
                serde_json::json!({
                    "name": name,
                    "base_url": p.base_url,
                    "api_key": mask_key(p.api_key.as_deref()),
                    "has_api_key": p.api_key.is_some(),
                    "is_default": default == Some(name.as_str()),
                })
            })
            .collect();
        println!("{}", serde_json::json!({ "gateways": profiles }));
        return Ok(());
    }

    if config.gateways.is_empty() {
        println!(
            "no gateway profiles. Add one with 'cardanowall gateway add <name> --base-url <url>'."
        );
        return Ok(());
    }
    for (name, p) in &config.gateways {
        let marker = if default == Some(name.as_str()) {
            "* "
        } else {
            "  "
        };
        println!(
            "{marker}{name}\t{}\t{}",
            p.base_url,
            mask_key(p.api_key.as_deref())
        );
    }
    Ok(())
}

// ===========================================================================
// gateway show
// ===========================================================================

/// Arguments for `cardanowall gateway show`.
#[derive(Debug, Args)]
pub struct GatewayShowArgs {
    /// the profile name to show.
    pub name: String,
    /// reveal the full API key instead of masking it.
    #[arg(long)]
    pub reveal: bool,
    /// emit a machine-readable JSON object.
    #[arg(long)]
    pub json: bool,
}

fn run_show(
    args: GatewayShowArgs,
    config_env: &dyn crate::config::ConfigEnv,
) -> Result<(), CliError> {
    let config = load_config_for_edit(config_env)?;
    let Some(profile) = config.gateways.get(&args.name) else {
        return Err(unknown_profile(&args.name, &config));
    };
    let is_default = config.default_gateway.as_deref() == Some(args.name.as_str());
    let key_display = if args.reveal {
        profile.api_key.clone().unwrap_or_default()
    } else {
        mask_key(profile.api_key.as_deref())
    };

    if args.json {
        let value = serde_json::json!({
            "name": args.name,
            "base_url": profile.base_url,
            "api_key": if args.reveal { serde_json::Value::from(profile.api_key.clone()) } else { serde_json::Value::from(key_display.clone()) },
            "has_api_key": profile.api_key.is_some(),
            "is_default": is_default,
        });
        println!("{value}");
    } else {
        println!("name:      {}", args.name);
        println!("base_url:  {}", profile.base_url);
        println!("api_key:   {key_display}");
        println!("default:   {}", if is_default { "yes" } else { "no" });
    }
    Ok(())
}

// ===========================================================================
// gateway remove
// ===========================================================================

/// Arguments for `cardanowall gateway remove`.
#[derive(Debug, Args)]
pub struct GatewayRemoveArgs {
    /// the profile name to remove.
    pub name: String,
    /// emit a machine-readable JSON acknowledgement.
    #[arg(long)]
    pub json: bool,
}

fn run_remove(
    args: GatewayRemoveArgs,
    config_env: &dyn crate::config::ConfigEnv,
) -> Result<(), CliError> {
    let mut config = load_config_for_edit(config_env)?;
    if config.gateways.remove(&args.name).is_none() {
        return Err(unknown_profile(&args.name, &config));
    }
    // Clearing the default that pointed at the removed profile keeps the config
    // internally consistent (no dangling default_gateway).
    if config.default_gateway.as_deref() == Some(args.name.as_str()) {
        config.default_gateway = None;
    }
    write_config(config_env, &config)?;
    if args.json {
        println!(
            "{}",
            serde_json::json!({ "status": "ok", "removed": args.name })
        );
    } else {
        println!("ok: removed gateway profile '{}'", args.name);
    }
    Ok(())
}

// ===========================================================================
// Shared helpers
// ===========================================================================

/// Mask an API key for display: keep nothing, show a fixed sigil. A non-empty key
/// renders as `********` (length-hiding); an absent key renders as `<none>`.
fn mask_key(key: Option<&str>) -> String {
    match key {
        Some(k) if !k.is_empty() => "********".to_string(),
        _ => "<none>".to_string(),
    }
}

/// Profile names key a TOML table and a directory-free identifier; keep them to a
/// conservative `[A-Za-z0-9._-]+` so they never need quoting on disk.
fn validate_name(name: &str) -> Result<(), CliError> {
    if name.is_empty()
        || !name
            .bytes()
            .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'_' | b'-'))
    {
        return Err(CliError::input(format!(
            "gateway: profile name must be non-empty and match [A-Za-z0-9._-]; got \"{name}\""
        )));
    }
    Ok(())
}

fn unknown_profile(name: &str, config: &crate::config::CardanoWallConfig) -> CliError {
    let known: Vec<&str> = config.gateways.keys().map(String::as_str).collect();
    let hint = if known.is_empty() {
        "no gateway profiles are defined".to_string()
    } else {
        format!("known profiles: {}", known.join(", "))
    };
    let path = system_config_path()
        .map(|p| p.display().to_string())
        .unwrap_or_else(|_| "config.toml".to_string());
    CliError::input(format!(
        "gateway: no profile named \"{name}\" in {path} ({hint})"
    ))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn masks_key() {
        assert_eq!(mask_key(Some("supersecret")), "********");
        assert_eq!(mask_key(Some("")), "<none>");
        assert_eq!(mask_key(None), "<none>");
    }

    #[test]
    fn validates_name() {
        assert!(validate_name("prod").is_ok());
        assert!(validate_name("prod.eu-1_test").is_ok());
        assert_eq!(validate_name("bad name").unwrap_err().code, 4);
        assert_eq!(validate_name("").unwrap_err().code, 4);
        assert_eq!(validate_name("a/b").unwrap_err().code, 4);
    }
}