use clap::{Parser, Subcommand};
use crate::commands;
#[derive(Parser)]
#[command(
name = "envbyte",
about = "Team .env manager - encrypted, versioned, collaborative",
version,
long_about = "Envbyte shares .env files across a team without anyone sending a secret to \
anyone. Each member holds an identity key; the project key is sealed to it."
)]
pub struct Cli {
#[command(subcommand)]
pub command: Commands,
}
#[derive(Subcommand)]
pub enum Commands {
Create {
project_name: String,
},
Init {
project_name: Option<String>,
},
Projects,
Push {
#[arg(short, long, default_value = "update")]
message: String,
#[arg(short, long)]
file: Option<String>,
#[arg(long)]
force: bool,
},
Pull {
#[arg(short, long)]
file: Option<String>,
#[arg(long)]
force: bool,
},
Commit { message: String },
Logs {
#[arg(long)]
file: Option<String>,
#[arg(long)]
remote: bool,
},
Rollback {
#[arg(long)]
address: Option<String>,
#[arg(long)]
local: Option<String>,
},
Add { email: String },
Members,
Remove { user_id: String },
Role { user_id: String, role: String },
Rotate {
#[arg(short = 'y', long)]
yes: bool,
},
Identity {
#[command(subcommand)]
action: Option<IdentityAction>,
},
Register,
Verify,
Login,
Logout,
Whoami,
ResetPassword,
Status,
Audit,
}
#[derive(Subcommand)]
pub enum IdentityAction {
Show,
Publish,
Export,
Replace,
}
pub async fn dispatch(command: Commands) -> Result<(), String> {
match command {
Commands::Create { project_name } => commands::project::create(project_name).await,
Commands::Init { project_name } => commands::project::init(project_name).await,
Commands::Projects => commands::project::list().await,
Commands::Push {
message,
file,
force,
} => commands::sync::push(message, file, force).await,
Commands::Pull { file, force } => commands::sync::pull(file, force).await,
Commands::Commit { message } => commands::snapshot::commit(message).await,
Commands::Logs { file, remote } => commands::history::show(file, remote).await,
Commands::Rollback { address, local } => commands::rollback::rollback(address, local).await,
Commands::Add { email } => commands::members::add(email).await,
Commands::Members => commands::members::list().await,
Commands::Remove { user_id } => commands::members::remove(user_id).await,
Commands::Role { user_id, role } => commands::members::change_role(user_id, role).await,
Commands::Rotate { yes } => commands::rotate::rotate(yes).await,
Commands::Identity { action } => match action.unwrap_or(IdentityAction::Show) {
IdentityAction::Show => commands::identity::show().await,
IdentityAction::Publish => commands::identity::publish().await,
IdentityAction::Export => commands::identity::export().await,
IdentityAction::Replace => commands::identity::replace().await,
},
Commands::Register => commands::account::register().await,
Commands::Verify => commands::account::verify().await,
Commands::Login => commands::account::login().await,
Commands::Logout => commands::account::logout().await,
Commands::Whoami => commands::account::whoami().await,
Commands::ResetPassword => commands::account::reset_password().await,
Commands::Status => commands::status::show().await,
Commands::Audit => commands::audit::show().await,
}
}
#[cfg(test)]
mod tests {
use super::*;
use clap::CommandFactory;
#[test]
fn the_command_surface_is_well_formed() {
Cli::command().debug_assert();
}
#[test]
fn parses_non_interactive_push_options() {
let cli = Cli::try_parse_from([
"envbyte",
"push",
"--file",
".env.ci",
"--message",
"deploy",
])
.unwrap();
match cli.command {
Commands::Push {
message,
file,
force,
} => {
assert_eq!(message, "deploy");
assert_eq!(file.as_deref(), Some(".env.ci"));
assert!(!force, "merging is the default");
}
_ => panic!("expected push"),
}
}
#[test]
fn parses_the_safe_pull_overwrite_flag() {
let cli = Cli::try_parse_from(["envbyte", "pull", "--file", ".env.ci", "--force"]).unwrap();
match cli.command {
Commands::Pull { file, force } => {
assert_eq!(file.as_deref(), Some(".env.ci"));
assert!(force);
}
_ => panic!("expected pull"),
}
}
#[test]
fn rotation_can_be_scripted() {
let cli = Cli::try_parse_from(["envbyte", "rotate", "-y"]).unwrap();
assert!(matches!(cli.command, Commands::Rotate { yes: true }));
let cli = Cli::try_parse_from(["envbyte", "rotate"]).unwrap();
assert!(matches!(cli.command, Commands::Rotate { yes: false }));
}
#[test]
fn init_no_longer_requires_a_project_name() {
let cli = Cli::try_parse_from(["envbyte", "init"]).unwrap();
assert!(matches!(cli.command, Commands::Init { project_name: None }));
}
#[test]
fn identity_defaults_to_showing() {
let cli = Cli::try_parse_from(["envbyte", "identity"]).unwrap();
assert!(matches!(cli.command, Commands::Identity { action: None }));
}
}