Skip to main content

greentic_dev/
secrets_cli.rs

1use std::ffi::OsString;
2use std::path::PathBuf;
3
4use anyhow::{Context, Result, bail};
5use clap::{Args, Subcommand};
6
7use crate::passthrough::{resolve_binary, run_passthrough};
8
9#[derive(Subcommand, Debug)]
10pub enum SecretsCommand {
11    /// Delegate to greentic-secrets to initialize secrets for a pack
12    Init(SecretsInitArgs),
13}
14
15#[derive(Args, Debug, Clone)]
16pub struct SecretsInitArgs {
17    /// Path to the pack (.gtpack) to initialize
18    #[arg(short = 'p', long = "pack")]
19    pub pack: PathBuf,
20    /// Optional extra args passed through to greentic-secrets (add `--` before flags)
21    #[arg(last = true)]
22    pub passthrough: Vec<String>,
23}
24
25pub fn run_secrets_command(cmd: SecretsCommand) -> Result<()> {
26    match cmd {
27        SecretsCommand::Init(args) => run_init(&args),
28    }
29}
30
31fn run_init(args: &SecretsInitArgs) -> Result<()> {
32    let bin = resolve_binary("greentic-secrets")?;
33    let mut argv = vec![
34        OsString::from("init"),
35        OsString::from("--pack"),
36        args.pack.clone().into_os_string(),
37    ];
38    argv.extend(args.passthrough.iter().map(OsString::from));
39    let status = run_passthrough(&bin, &argv, false)
40        .with_context(|| "failed to execute greentic-secrets")?;
41    if !status.success() {
42        bail!("greentic-secrets exited with status {}", status);
43    }
44    Ok(())
45}