use std::process::Command;
#[derive(Debug)]
pub struct OpCommand {
args: Vec<String>,
account: Option<String>,
raw_output: bool,
}
impl OpCommand {
pub fn new(subcommand: &str) -> Self {
Self {
args: vec![subcommand.to_string()],
account: None,
raw_output: false,
}
}
pub fn raw(mut self) -> Self {
self.raw_output = true;
self
}
pub fn subcommand(mut self, sub: &str) -> Self {
self.args.push(sub.to_string());
self
}
pub fn arg(mut self, arg: &str) -> Self {
self.args.push(arg.to_string());
self
}
pub fn arg_opt(self, arg: Option<&str>) -> Self {
match arg {
Some(a) => self.arg(a),
None => self,
}
}
pub fn flag(mut self, flag: &str) -> Self {
self.args.push(format!("--{}", flag));
self
}
pub fn flag_if(self, flag: &str, condition: bool) -> Self {
if condition {
self.flag(flag)
} else {
self
}
}
pub fn option(mut self, name: &str, value: &str) -> Self {
self.args.push(format!("--{}", name));
self.args.push(value.to_string());
self
}
pub fn option_opt(self, name: &str, value: Option<&str>) -> Self {
match value {
Some(v) => self.option(name, v),
None => self,
}
}
pub fn option_multi(mut self, name: &str, values: Option<&[&str]>) -> Self {
if let Some(vals) = values {
for val in vals {
self.args.push(format!("--{}", name));
self.args.push(val.to_string());
}
}
self
}
pub fn account(mut self, account: Option<&str>) -> Self {
self.account = account.map(|s| s.to_string());
self
}
pub fn build(self, op_path: &str) -> Command {
let mut cmd = Command::new(op_path);
for arg in &self.args {
cmd.arg(arg);
}
if !self.raw_output {
cmd.arg("--format");
cmd.arg("json");
}
if let Some(account) = &self.account {
cmd.arg("--account");
cmd.arg(account);
}
cmd
}
pub fn is_raw(&self) -> bool {
self.raw_output
}
}
impl OpCommand {
pub fn whoami() -> Self {
Self::new("whoami")
}
pub fn signin() -> Self {
Self::new("signin").raw()
}
pub fn signout() -> Self {
Self::new("signout").raw()
}
}
impl OpCommand {
pub fn account_list() -> Self {
Self::new("account").subcommand("list")
}
pub fn account_get(account: Option<&str>) -> Self {
let cmd = Self::new("account").subcommand("get");
match account {
Some(a) => cmd.arg(a),
None => cmd,
}
}
pub fn account_add(address: &str, email: &str) -> Self {
Self::new("account")
.subcommand("add")
.option("address", address)
.option("email", email)
.raw()
}
pub fn account_forget(account: &str) -> Self {
Self::new("account")
.subcommand("forget")
.arg(account)
.raw()
}
}
impl OpCommand {
pub fn vault_list() -> Self {
Self::new("vault").subcommand("list")
}
pub fn vault_get(vault: &str) -> Self {
Self::new("vault").subcommand("get").arg(vault)
}
pub fn vault_create(name: &str) -> Self {
Self::new("vault").subcommand("create").arg(name)
}
pub fn vault_edit(vault: &str) -> Self {
Self::new("vault").subcommand("edit").arg(vault)
}
pub fn vault_delete(vault: &str) -> Self {
Self::new("vault").subcommand("delete").arg(vault).raw()
}
pub fn vault_user_list(vault: &str) -> Self {
Self::new("vault")
.subcommand("user")
.subcommand("list")
.arg(vault)
}
pub fn vault_user_grant(vault: &str, user: &str) -> Self {
Self::new("vault")
.subcommand("user")
.subcommand("grant")
.option("vault", vault)
.option("user", user)
.raw()
}
pub fn vault_user_revoke(vault: &str, user: &str) -> Self {
Self::new("vault")
.subcommand("user")
.subcommand("revoke")
.option("vault", vault)
.option("user", user)
.raw()
}
pub fn vault_group_list(vault: &str) -> Self {
Self::new("vault")
.subcommand("group")
.subcommand("list")
.arg(vault)
}
pub fn vault_group_grant(vault: &str, group: &str) -> Self {
Self::new("vault")
.subcommand("group")
.subcommand("grant")
.option("vault", vault)
.option("group", group)
.raw()
}
pub fn vault_group_revoke(vault: &str, group: &str) -> Self {
Self::new("vault")
.subcommand("group")
.subcommand("revoke")
.option("vault", vault)
.option("group", group)
.raw()
}
}
impl OpCommand {
pub fn item_list() -> Self {
Self::new("item").subcommand("list")
}
pub fn item_get(item: &str) -> Self {
Self::new("item").subcommand("get").arg(item)
}
pub fn item_create(category: &str, title: &str) -> Self {
Self::new("item")
.subcommand("create")
.option("category", category)
.option("title", title)
}
pub fn item_edit(item: &str) -> Self {
Self::new("item").subcommand("edit").arg(item)
}
pub fn item_delete(item: &str) -> Self {
Self::new("item").subcommand("delete").arg(item).raw()
}
pub fn item_move(item: &str, destination_vault: &str) -> Self {
Self::new("item")
.subcommand("move")
.arg(item)
.option("destination-vault", destination_vault)
.raw()
}
pub fn item_share(item: &str) -> Self {
Self::new("item").subcommand("share").arg(item).raw()
}
pub fn item_template_list() -> Self {
Self::new("item").subcommand("template").subcommand("list")
}
pub fn item_template_get(template: &str) -> Self {
Self::new("item")
.subcommand("template")
.subcommand("get")
.arg(template)
}
}
impl OpCommand {
pub fn document_list() -> Self {
Self::new("document").subcommand("list")
}
pub fn document_get(document: &str) -> Self {
Self::new("document").subcommand("get").arg(document).raw()
}
pub fn document_create(file_path: &str) -> Self {
Self::new("document").subcommand("create").arg(file_path)
}
pub fn document_edit(document: &str, file_path: &str) -> Self {
Self::new("document")
.subcommand("edit")
.arg(document)
.arg(file_path)
}
pub fn document_delete(document: &str) -> Self {
Self::new("document").subcommand("delete").arg(document).raw()
}
}
impl OpCommand {
pub fn user_list() -> Self {
Self::new("user").subcommand("list")
}
pub fn user_get(user: &str) -> Self {
Self::new("user").subcommand("get").arg(user)
}
pub fn user_provision(email: &str, name: &str) -> Self {
Self::new("user")
.subcommand("provision")
.option("email", email)
.option("name", name)
}
pub fn user_confirm(user: &str) -> Self {
Self::new("user").subcommand("confirm").arg(user).raw()
}
pub fn user_edit(user: &str) -> Self {
Self::new("user").subcommand("edit").arg(user)
}
pub fn user_suspend(user: &str) -> Self {
Self::new("user").subcommand("suspend").arg(user).raw()
}
pub fn user_reactivate(user: &str) -> Self {
Self::new("user").subcommand("reactivate").arg(user).raw()
}
pub fn user_delete(user: &str) -> Self {
Self::new("user").subcommand("delete").arg(user).raw()
}
}
impl OpCommand {
pub fn group_list() -> Self {
Self::new("group").subcommand("list")
}
pub fn group_get(group: &str) -> Self {
Self::new("group").subcommand("get").arg(group)
}
pub fn group_create(name: &str) -> Self {
Self::new("group").subcommand("create").arg(name)
}
pub fn group_edit(group: &str) -> Self {
Self::new("group").subcommand("edit").arg(group)
}
pub fn group_delete(group: &str) -> Self {
Self::new("group").subcommand("delete").arg(group).raw()
}
pub fn group_user_list(group: &str) -> Self {
Self::new("group")
.subcommand("user")
.subcommand("list")
.arg(group)
}
pub fn group_user_grant(group: &str, user: &str) -> Self {
Self::new("group")
.subcommand("user")
.subcommand("grant")
.option("group", group)
.option("user", user)
.raw()
}
pub fn group_user_revoke(group: &str, user: &str) -> Self {
Self::new("group")
.subcommand("user")
.subcommand("revoke")
.option("group", group)
.option("user", user)
.raw()
}
}
impl OpCommand {
pub fn connect_server_list() -> Self {
Self::new("connect").subcommand("server").subcommand("list")
}
pub fn connect_server_get(server: &str) -> Self {
Self::new("connect")
.subcommand("server")
.subcommand("get")
.arg(server)
}
pub fn connect_server_create(name: &str) -> Self {
Self::new("connect")
.subcommand("server")
.subcommand("create")
.arg(name)
}
pub fn connect_server_edit(server: &str) -> Self {
Self::new("connect")
.subcommand("server")
.subcommand("edit")
.arg(server)
}
pub fn connect_server_delete(server: &str) -> Self {
Self::new("connect")
.subcommand("server")
.subcommand("delete")
.arg(server)
.raw()
}
pub fn connect_token_list(server: &str) -> Self {
Self::new("connect")
.subcommand("token")
.subcommand("list")
.option("server", server)
}
pub fn connect_token_create(server: &str, name: &str) -> Self {
Self::new("connect")
.subcommand("token")
.subcommand("create")
.arg(name)
.option("server", server)
.raw()
}
pub fn connect_token_edit(server: &str, token: &str) -> Self {
Self::new("connect")
.subcommand("token")
.subcommand("edit")
.arg(token)
.option("server", server)
}
pub fn connect_token_delete(server: &str, token: &str) -> Self {
Self::new("connect")
.subcommand("token")
.subcommand("delete")
.arg(token)
.option("server", server)
.raw()
}
pub fn connect_vault_grant(server: &str, vault: &str) -> Self {
Self::new("connect")
.subcommand("vault")
.subcommand("grant")
.option("server", server)
.option("vault", vault)
.raw()
}
pub fn connect_vault_revoke(server: &str, vault: &str) -> Self {
Self::new("connect")
.subcommand("vault")
.subcommand("revoke")
.option("server", server)
.option("vault", vault)
.raw()
}
}
impl OpCommand {
pub fn service_account_create(name: &str) -> Self {
Self::new("service-account")
.subcommand("create")
.arg(name)
.raw()
}
pub fn service_account_ratelimit() -> Self {
Self::new("service-account").subcommand("ratelimit")
}
}
impl OpCommand {
pub fn events_api_create(name: &str) -> Self {
Self::new("events-api").subcommand("create").arg(name).raw()
}
}
impl OpCommand {
pub fn read(reference: &str) -> Self {
Self::new("read").arg(reference).raw()
}
pub fn inject() -> Self {
Self::new("inject").raw()
}
pub fn run() -> Self {
Self::new("run").raw()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn get_args(cmd: &Command) -> Vec<&str> {
cmd.get_args().map(|s| s.to_str().unwrap()).collect()
}
#[test]
fn test_new_creates_command_with_subcommand() {
let cmd = OpCommand::new("test").build("op");
assert_eq!(get_args(&cmd), vec!["test", "--format", "json"]);
}
#[test]
fn test_raw_disables_json_format() {
let cmd = OpCommand::new("test").raw().build("op");
assert_eq!(get_args(&cmd), vec!["test"]);
}
#[test]
fn test_subcommand_chains() {
let cmd = OpCommand::new("vault")
.subcommand("user")
.subcommand("list")
.build("op");
assert_eq!(
get_args(&cmd),
vec!["vault", "user", "list", "--format", "json"]
);
}
#[test]
fn test_arg_adds_positional_argument() {
let cmd = OpCommand::new("item").subcommand("get").arg("my-item").build("op");
assert_eq!(
get_args(&cmd),
vec!["item", "get", "my-item", "--format", "json"]
);
}
#[test]
fn test_arg_opt_with_some() {
let cmd = OpCommand::new("test").arg_opt(Some("value")).build("op");
assert_eq!(get_args(&cmd), vec!["test", "value", "--format", "json"]);
}
#[test]
fn test_arg_opt_with_none() {
let cmd = OpCommand::new("test").arg_opt(None).build("op");
assert_eq!(get_args(&cmd), vec!["test", "--format", "json"]);
}
#[test]
fn test_flag_adds_flag() {
let cmd = OpCommand::new("test").flag("reveal").build("op");
assert_eq!(get_args(&cmd), vec!["test", "--reveal", "--format", "json"]);
}
#[test]
fn test_flag_if_true() {
let cmd = OpCommand::new("test").flag_if("archive", true).build("op");
assert_eq!(get_args(&cmd), vec!["test", "--archive", "--format", "json"]);
}
#[test]
fn test_flag_if_false() {
let cmd = OpCommand::new("test").flag_if("archive", false).build("op");
assert_eq!(get_args(&cmd), vec!["test", "--format", "json"]);
}
#[test]
fn test_option_adds_key_value() {
let cmd = OpCommand::new("test").option("vault", "Personal").build("op");
assert_eq!(
get_args(&cmd),
vec!["test", "--vault", "Personal", "--format", "json"]
);
}
#[test]
fn test_option_opt_with_some() {
let cmd = OpCommand::new("test")
.option_opt("vault", Some("Work"))
.build("op");
assert_eq!(
get_args(&cmd),
vec!["test", "--vault", "Work", "--format", "json"]
);
}
#[test]
fn test_option_opt_with_none() {
let cmd = OpCommand::new("test").option_opt("vault", None).build("op");
assert_eq!(get_args(&cmd), vec!["test", "--format", "json"]);
}
#[test]
fn test_option_multi_with_values() {
let cmd = OpCommand::new("test")
.option_multi("tags", Some(&["tag1", "tag2"]))
.build("op");
assert_eq!(
get_args(&cmd),
vec!["test", "--tags", "tag1", "--tags", "tag2", "--format", "json"]
);
}
#[test]
fn test_option_multi_with_none() {
let cmd = OpCommand::new("test").option_multi("tags", None).build("op");
assert_eq!(get_args(&cmd), vec!["test", "--format", "json"]);
}
#[test]
fn test_account_adds_account_flag() {
let cmd = OpCommand::new("test").account(Some("my-account")).build("op");
assert_eq!(
get_args(&cmd),
vec!["test", "--format", "json", "--account", "my-account"]
);
}
#[test]
fn test_account_with_none() {
let cmd = OpCommand::new("test").account(None).build("op");
assert_eq!(get_args(&cmd), vec!["test", "--format", "json"]);
}
#[test]
fn test_is_raw() {
let raw_cmd = OpCommand::new("test").raw();
let json_cmd = OpCommand::new("test");
assert!(raw_cmd.is_raw());
assert!(!json_cmd.is_raw());
}
#[test]
fn test_whoami_command() {
let cmd = OpCommand::whoami().build("op");
assert_eq!(get_args(&cmd), vec!["whoami", "--format", "json"]);
}
#[test]
fn test_signin_command() {
let cmd = OpCommand::signin().build("op");
assert_eq!(get_args(&cmd), vec!["signin"]);
}
#[test]
fn test_signout_command() {
let cmd = OpCommand::signout().build("op");
assert_eq!(get_args(&cmd), vec!["signout"]);
}
#[test]
fn test_account_list_command() {
let cmd = OpCommand::account_list().build("op");
assert_eq!(get_args(&cmd), vec!["account", "list", "--format", "json"]);
}
#[test]
fn test_account_get_without_account() {
let cmd = OpCommand::account_get(None).build("op");
assert_eq!(get_args(&cmd), vec!["account", "get", "--format", "json"]);
}
#[test]
fn test_account_get_with_account() {
let cmd = OpCommand::account_get(Some("my-account")).build("op");
assert_eq!(
get_args(&cmd),
vec!["account", "get", "my-account", "--format", "json"]
);
}
#[test]
fn test_account_add_command() {
let cmd = OpCommand::account_add("my.1password.com", "user@example.com").build("op");
assert_eq!(
get_args(&cmd),
vec![
"account",
"add",
"--address",
"my.1password.com",
"--email",
"user@example.com"
]
);
}
#[test]
fn test_account_forget_command() {
let cmd = OpCommand::account_forget("my-account").build("op");
assert_eq!(get_args(&cmd), vec!["account", "forget", "my-account"]);
}
#[test]
fn test_vault_list_command() {
let cmd = OpCommand::vault_list().build("op");
assert_eq!(get_args(&cmd), vec!["vault", "list", "--format", "json"]);
}
#[test]
fn test_vault_get_command() {
let cmd = OpCommand::vault_get("Personal").build("op");
assert_eq!(
get_args(&cmd),
vec!["vault", "get", "Personal", "--format", "json"]
);
}
#[test]
fn test_vault_create_command() {
let cmd = OpCommand::vault_create("NewVault").build("op");
assert_eq!(
get_args(&cmd),
vec!["vault", "create", "NewVault", "--format", "json"]
);
}
#[test]
fn test_vault_create_with_description() {
let cmd = OpCommand::vault_create("NewVault")
.option("description", "My description")
.build("op");
assert_eq!(
get_args(&cmd),
vec![
"vault",
"create",
"NewVault",
"--description",
"My description",
"--format",
"json"
]
);
}
#[test]
fn test_vault_create_with_allow_admins_to_manage() {
let cmd = OpCommand::vault_create("NewVault")
.option("allow-admins-to-manage", "true")
.build("op");
assert_eq!(
get_args(&cmd),
vec![
"vault",
"create",
"NewVault",
"--allow-admins-to-manage",
"true",
"--format",
"json"
]
);
}
#[test]
fn test_vault_create_with_allow_admins_false() {
let cmd = OpCommand::vault_create("NewVault")
.option("allow-admins-to-manage", "false")
.build("op");
assert_eq!(
get_args(&cmd),
vec![
"vault",
"create",
"NewVault",
"--allow-admins-to-manage",
"false",
"--format",
"json"
]
);
}
#[test]
fn test_vault_edit_command() {
let cmd = OpCommand::vault_edit("Personal").build("op");
assert_eq!(
get_args(&cmd),
vec!["vault", "edit", "Personal", "--format", "json"]
);
}
#[test]
fn test_vault_delete_command() {
let cmd = OpCommand::vault_delete("OldVault").build("op");
assert_eq!(get_args(&cmd), vec!["vault", "delete", "OldVault"]);
}
#[test]
fn test_vault_user_list_command() {
let cmd = OpCommand::vault_user_list("Personal").build("op");
assert_eq!(
get_args(&cmd),
vec!["vault", "user", "list", "Personal", "--format", "json"]
);
}
#[test]
fn test_vault_user_grant_command() {
let cmd = OpCommand::vault_user_grant("vault1", "user@example.com")
.option("permissions", "allow_viewing,allow_editing")
.build("op");
assert_eq!(
get_args(&cmd),
vec![
"vault",
"user",
"grant",
"--vault",
"vault1",
"--user",
"user@example.com",
"--permissions",
"allow_viewing,allow_editing"
]
);
}
#[test]
fn test_vault_user_revoke_command() {
let cmd = OpCommand::vault_user_revoke("vault1", "user@example.com").build("op");
assert_eq!(
get_args(&cmd),
vec![
"vault",
"user",
"revoke",
"--vault",
"vault1",
"--user",
"user@example.com"
]
);
}
#[test]
fn test_vault_group_list_command() {
let cmd = OpCommand::vault_group_list("Personal").build("op");
assert_eq!(
get_args(&cmd),
vec!["vault", "group", "list", "Personal", "--format", "json"]
);
}
#[test]
fn test_vault_group_grant_command() {
let cmd = OpCommand::vault_group_grant("vault1", "Admins").build("op");
assert_eq!(
get_args(&cmd),
vec!["vault", "group", "grant", "--vault", "vault1", "--group", "Admins"]
);
}
#[test]
fn test_vault_group_revoke_command() {
let cmd = OpCommand::vault_group_revoke("vault1", "Admins").build("op");
assert_eq!(
get_args(&cmd),
vec!["vault", "group", "revoke", "--vault", "vault1", "--group", "Admins"]
);
}
#[test]
fn test_item_list_command() {
let cmd = OpCommand::item_list().build("op");
assert_eq!(get_args(&cmd), vec!["item", "list", "--format", "json"]);
}
#[test]
fn test_item_list_with_vault() {
let cmd = OpCommand::item_list()
.option("vault", "Personal")
.build("op");
assert_eq!(
get_args(&cmd),
vec!["item", "list", "--vault", "Personal", "--format", "json"]
);
}
#[test]
fn test_item_get_command() {
let cmd = OpCommand::item_get("my-login").build("op");
assert_eq!(
get_args(&cmd),
vec!["item", "get", "my-login", "--format", "json"]
);
}
#[test]
fn test_item_get_with_reveal() {
let cmd = OpCommand::item_get("my-login")
.flag("reveal")
.option_opt("vault", Some("Work"))
.build("op");
assert_eq!(
get_args(&cmd),
vec![
"item",
"get",
"my-login",
"--reveal",
"--vault",
"Work",
"--format",
"json"
]
);
}
#[test]
fn test_item_create_command() {
let cmd = OpCommand::item_create("Login", "My Service").build("op");
assert_eq!(
get_args(&cmd),
vec![
"item",
"create",
"--category",
"Login",
"--title",
"My Service",
"--format",
"json"
]
);
}
#[test]
fn test_item_edit_command() {
let cmd = OpCommand::item_edit("my-login").build("op");
assert_eq!(
get_args(&cmd),
vec!["item", "edit", "my-login", "--format", "json"]
);
}
#[test]
fn test_item_delete_command() {
let cmd = OpCommand::item_delete("my-login").build("op");
assert_eq!(get_args(&cmd), vec!["item", "delete", "my-login"]);
}
#[test]
fn test_item_move_command() {
let cmd = OpCommand::item_move("my-item", "Work").build("op");
assert_eq!(
get_args(&cmd),
vec!["item", "move", "my-item", "--destination-vault", "Work"]
);
}
#[test]
fn test_item_share_command() {
let cmd = OpCommand::item_share("my-item").build("op");
assert_eq!(get_args(&cmd), vec!["item", "share", "my-item"]);
}
#[test]
fn test_item_template_list_command() {
let cmd = OpCommand::item_template_list().build("op");
assert_eq!(
get_args(&cmd),
vec!["item", "template", "list", "--format", "json"]
);
}
#[test]
fn test_item_template_get_command() {
let cmd = OpCommand::item_template_get("Login").build("op");
assert_eq!(
get_args(&cmd),
vec!["item", "template", "get", "Login", "--format", "json"]
);
}
#[test]
fn test_document_list_command() {
let cmd = OpCommand::document_list().build("op");
assert_eq!(
get_args(&cmd),
vec!["document", "list", "--format", "json"]
);
}
#[test]
fn test_document_get_command() {
let cmd = OpCommand::document_get("my-doc").build("op");
assert_eq!(get_args(&cmd), vec!["document", "get", "my-doc"]);
}
#[test]
fn test_document_create_command() {
let cmd = OpCommand::document_create("/path/to/file.pdf").build("op");
assert_eq!(
get_args(&cmd),
vec!["document", "create", "/path/to/file.pdf", "--format", "json"]
);
}
#[test]
fn test_document_edit_command() {
let cmd = OpCommand::document_edit("my-doc", "/path/to/new.pdf").build("op");
assert_eq!(
get_args(&cmd),
vec![
"document",
"edit",
"my-doc",
"/path/to/new.pdf",
"--format",
"json"
]
);
}
#[test]
fn test_document_delete_command() {
let cmd = OpCommand::document_delete("my-doc").build("op");
assert_eq!(get_args(&cmd), vec!["document", "delete", "my-doc"]);
}
#[test]
fn test_user_list_command() {
let cmd = OpCommand::user_list().build("op");
assert_eq!(get_args(&cmd), vec!["user", "list", "--format", "json"]);
}
#[test]
fn test_user_get_command() {
let cmd = OpCommand::user_get("user@example.com").build("op");
assert_eq!(
get_args(&cmd),
vec!["user", "get", "user@example.com", "--format", "json"]
);
}
#[test]
fn test_user_provision_command() {
let cmd = OpCommand::user_provision("new@example.com", "New User").build("op");
assert_eq!(
get_args(&cmd),
vec![
"user",
"provision",
"--email",
"new@example.com",
"--name",
"New User",
"--format",
"json"
]
);
}
#[test]
fn test_user_confirm_command() {
let cmd = OpCommand::user_confirm("user@example.com").build("op");
assert_eq!(get_args(&cmd), vec!["user", "confirm", "user@example.com"]);
}
#[test]
fn test_user_edit_command() {
let cmd = OpCommand::user_edit("user@example.com").build("op");
assert_eq!(
get_args(&cmd),
vec!["user", "edit", "user@example.com", "--format", "json"]
);
}
#[test]
fn test_user_suspend_command() {
let cmd = OpCommand::user_suspend("user@example.com").build("op");
assert_eq!(get_args(&cmd), vec!["user", "suspend", "user@example.com"]);
}
#[test]
fn test_user_reactivate_command() {
let cmd = OpCommand::user_reactivate("user@example.com").build("op");
assert_eq!(
get_args(&cmd),
vec!["user", "reactivate", "user@example.com"]
);
}
#[test]
fn test_user_delete_command() {
let cmd = OpCommand::user_delete("user@example.com").build("op");
assert_eq!(get_args(&cmd), vec!["user", "delete", "user@example.com"]);
}
#[test]
fn test_group_list_command() {
let cmd = OpCommand::group_list().build("op");
assert_eq!(get_args(&cmd), vec!["group", "list", "--format", "json"]);
}
#[test]
fn test_group_get_command() {
let cmd = OpCommand::group_get("Admins").build("op");
assert_eq!(
get_args(&cmd),
vec!["group", "get", "Admins", "--format", "json"]
);
}
#[test]
fn test_group_create_command() {
let cmd = OpCommand::group_create("NewGroup").build("op");
assert_eq!(
get_args(&cmd),
vec!["group", "create", "NewGroup", "--format", "json"]
);
}
#[test]
fn test_group_edit_command() {
let cmd = OpCommand::group_edit("Admins").build("op");
assert_eq!(
get_args(&cmd),
vec!["group", "edit", "Admins", "--format", "json"]
);
}
#[test]
fn test_group_delete_command() {
let cmd = OpCommand::group_delete("OldGroup").build("op");
assert_eq!(get_args(&cmd), vec!["group", "delete", "OldGroup"]);
}
#[test]
fn test_group_user_list_command() {
let cmd = OpCommand::group_user_list("Admins").build("op");
assert_eq!(
get_args(&cmd),
vec!["group", "user", "list", "Admins", "--format", "json"]
);
}
#[test]
fn test_group_user_grant_command() {
let cmd = OpCommand::group_user_grant("Admins", "user@example.com").build("op");
assert_eq!(
get_args(&cmd),
vec![
"group",
"user",
"grant",
"--group",
"Admins",
"--user",
"user@example.com"
]
);
}
#[test]
fn test_group_user_revoke_command() {
let cmd = OpCommand::group_user_revoke("Admins", "user@example.com").build("op");
assert_eq!(
get_args(&cmd),
vec![
"group",
"user",
"revoke",
"--group",
"Admins",
"--user",
"user@example.com"
]
);
}
#[test]
fn test_connect_server_list_command() {
let cmd = OpCommand::connect_server_list().build("op");
assert_eq!(
get_args(&cmd),
vec!["connect", "server", "list", "--format", "json"]
);
}
#[test]
fn test_connect_server_get_command() {
let cmd = OpCommand::connect_server_get("my-server").build("op");
assert_eq!(
get_args(&cmd),
vec!["connect", "server", "get", "my-server", "--format", "json"]
);
}
#[test]
fn test_connect_server_create_command() {
let cmd = OpCommand::connect_server_create("new-server").build("op");
assert_eq!(
get_args(&cmd),
vec!["connect", "server", "create", "new-server", "--format", "json"]
);
}
#[test]
fn test_connect_server_edit_command() {
let cmd = OpCommand::connect_server_edit("my-server").build("op");
assert_eq!(
get_args(&cmd),
vec!["connect", "server", "edit", "my-server", "--format", "json"]
);
}
#[test]
fn test_connect_server_delete_command() {
let cmd = OpCommand::connect_server_delete("old-server").build("op");
assert_eq!(
get_args(&cmd),
vec!["connect", "server", "delete", "old-server"]
);
}
#[test]
fn test_connect_token_list_command() {
let cmd = OpCommand::connect_token_list("my-server").build("op");
assert_eq!(
get_args(&cmd),
vec![
"connect",
"token",
"list",
"--server",
"my-server",
"--format",
"json"
]
);
}
#[test]
fn test_connect_token_create_command() {
let cmd = OpCommand::connect_token_create("my-server", "new-token").build("op");
assert_eq!(
get_args(&cmd),
vec![
"connect",
"token",
"create",
"new-token",
"--server",
"my-server"
]
);
}
#[test]
fn test_connect_token_edit_command() {
let cmd = OpCommand::connect_token_edit("my-server", "my-token").build("op");
assert_eq!(
get_args(&cmd),
vec![
"connect",
"token",
"edit",
"my-token",
"--server",
"my-server",
"--format",
"json"
]
);
}
#[test]
fn test_connect_token_delete_command() {
let cmd = OpCommand::connect_token_delete("my-server", "old-token").build("op");
assert_eq!(
get_args(&cmd),
vec![
"connect",
"token",
"delete",
"old-token",
"--server",
"my-server"
]
);
}
#[test]
fn test_connect_vault_grant_command() {
let cmd = OpCommand::connect_vault_grant("my-server", "Personal").build("op");
assert_eq!(
get_args(&cmd),
vec![
"connect",
"vault",
"grant",
"--server",
"my-server",
"--vault",
"Personal"
]
);
}
#[test]
fn test_connect_vault_revoke_command() {
let cmd = OpCommand::connect_vault_revoke("my-server", "Personal").build("op");
assert_eq!(
get_args(&cmd),
vec![
"connect",
"vault",
"revoke",
"--server",
"my-server",
"--vault",
"Personal"
]
);
}
#[test]
fn test_service_account_create_command() {
let cmd = OpCommand::service_account_create("my-service").build("op");
assert_eq!(
get_args(&cmd),
vec!["service-account", "create", "my-service"]
);
}
#[test]
fn test_service_account_ratelimit_command() {
let cmd = OpCommand::service_account_ratelimit().build("op");
assert_eq!(
get_args(&cmd),
vec!["service-account", "ratelimit", "--format", "json"]
);
}
#[test]
fn test_events_api_create_command() {
let cmd = OpCommand::events_api_create("my-integration").build("op");
assert_eq!(
get_args(&cmd),
vec!["events-api", "create", "my-integration"]
);
}
#[test]
fn test_read_command() {
let cmd = OpCommand::read("op://vault/item/field").build("op");
assert_eq!(get_args(&cmd), vec!["read", "op://vault/item/field"]);
}
#[test]
fn test_inject_command() {
let cmd = OpCommand::inject().build("op");
assert_eq!(get_args(&cmd), vec!["inject"]);
}
#[test]
fn test_run_command() {
let cmd = OpCommand::run().build("op");
assert_eq!(get_args(&cmd), vec!["run"]);
}
#[test]
fn test_complex_item_create() {
let cmd = OpCommand::item_create("API Credential", "GitHub Token")
.option("vault", "Work")
.flag("favorite")
.option("url", "https://github.com")
.arg("username=myuser")
.arg("credential=token123")
.build("op");
assert_eq!(
get_args(&cmd),
vec![
"item",
"create",
"--category",
"API Credential",
"--title",
"GitHub Token",
"--vault",
"Work",
"--favorite",
"--url",
"https://github.com",
"username=myuser",
"credential=token123",
"--format",
"json"
]
);
}
#[test]
fn test_chained_methods_preserve_order() {
let cmd = OpCommand::new("test")
.arg("pos1")
.flag("flag1")
.option("opt1", "val1")
.arg("pos2")
.flag("flag2")
.option("opt2", "val2")
.build("op");
assert_eq!(
get_args(&cmd),
vec![
"test",
"pos1",
"--flag1",
"--opt1",
"val1",
"pos2",
"--flag2",
"--opt2",
"val2",
"--format",
"json"
]
);
}
}