use std::path::PathBuf;
use clap::{ArgAction, Parser, Subcommand, ValueEnum};
use dekopon_core::{AgentId, CapabilityId, ProviderId};
#[derive(Clone, Debug, Parser)]
#[command(
name = "dekopon",
version,
about = "Capability-oriented control plane for self-hosted AI agents",
long_about = None,
propagate_version = true
)]
pub struct Cli {
#[arg(long, global = true, value_name = "PATH")]
pub config: Option<PathBuf>,
#[arg(
short = 'o',
long = "output",
global = true,
value_enum,
default_value_t = OutputFormat::Table,
value_name = "FORMAT"
)]
pub output: OutputFormat,
#[arg(long, global = true)]
pub no_color: bool,
#[arg(long, global = true, conflicts_with = "verbose")]
pub quiet: bool,
#[arg(
short = 'v',
global = true,
action = ArgAction::Count,
conflicts_with = "quiet"
)]
pub verbose: u8,
#[command(subcommand)]
pub command: Command,
}
#[derive(Clone, Debug, Subcommand)]
pub enum Command {
Version,
Auth {
#[command(subcommand)]
account: AuthCommand,
},
Get {
#[command(subcommand)]
resource: GetCommand,
},
Describe {
#[command(subcommand)]
resource: DescribeCommand,
},
Validate,
Config {
#[command(subcommand)]
command: ConfigCommand,
},
}
#[derive(Clone, Debug, Subcommand)]
pub enum AuthCommand {
#[command(name = "chatgpt")]
ChatGpt {
#[command(subcommand)]
command: ChatGptAuthCommand,
},
}
#[derive(Clone, Debug, Subcommand)]
pub enum ChatGptAuthCommand {
Login {
#[arg(long, value_name = "PATH")]
auth_file: Option<PathBuf>,
},
Status {
#[arg(long, value_name = "PATH")]
auth_file: Option<PathBuf>,
},
Logout {
#[arg(long, value_name = "PATH")]
auth_file: Option<PathBuf>,
},
Export {
#[arg(long, value_name = "PATH")]
auth_file: Option<PathBuf>,
#[arg(
long,
value_enum,
default_value_t = ExportFormat::Secret,
value_name = "FORM"
)]
format: ExportFormat,
#[arg(
long,
value_name = "NAME",
default_value = "dekopon-chatgpt-auth",
value_parser = dns_subdomain
)]
secret_name: String,
#[arg(long, value_name = "NAMESPACE", value_parser = dns_label)]
namespace: Option<String>,
#[arg(long, required = true, conflicts_with = "quiet")]
expose_credential: bool,
#[arg(long)]
allow_terminal: bool,
},
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, ValueEnum)]
#[value(rename_all = "lower")]
pub enum ExportFormat {
Secret,
Raw,
}
fn dns_subdomain(value: &str) -> Result<String, String> {
dns_name(value, 253, true)
}
fn dns_label(value: &str) -> Result<String, String> {
dns_name(value, 63, false)
}
fn dns_name(value: &str, limit: usize, dots: bool) -> Result<String, String> {
let shape = if dots {
"lowercase letters, digits, '-' and '.'"
} else {
"lowercase letters, digits and '-'"
};
let requirement = format!(
"must be at most {limit} characters of {shape}, starting and ending with a letter or digit"
);
if value.is_empty() || value.len() > limit {
return Err(requirement);
}
let permitted = |character: char| {
character.is_ascii_lowercase()
|| character.is_ascii_digit()
|| character == '-'
|| (dots && character == '.')
};
let alphanumeric =
|character: char| character.is_ascii_lowercase() || character.is_ascii_digit();
if !value.chars().all(permitted)
|| !value.starts_with(alphanumeric)
|| !value.ends_with(alphanumeric)
|| value.contains("..")
{
return Err(requirement);
}
Ok(value.to_owned())
}
#[derive(Clone, Debug, Subcommand)]
pub enum GetCommand {
Agent {
name: AgentId,
},
Agents,
Capability {
name: CapabilityId,
},
Capabilities,
Provider {
name: ProviderId,
},
Providers,
}
#[derive(Clone, Debug, Subcommand)]
pub enum DescribeCommand {
Agent {
name: AgentId,
},
}
#[derive(Clone, Debug, Subcommand)]
pub enum ConfigCommand {
View,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, ValueEnum)]
#[value(rename_all = "lower")]
pub enum OutputFormat {
Table,
Wide,
Json,
Yaml,
Name,
}
#[cfg(test)]
mod tests {
use clap::{CommandFactory, Parser};
use super::{
AuthCommand, ChatGptAuthCommand, Cli, Command, ExportFormat, GetCommand, OutputFormat,
dns_label, dns_subdomain,
};
#[test]
fn clap_definition_is_internally_consistent() {
Cli::command().debug_assert();
}
#[test]
fn parses_chatgpt_auth_commands() {
let cli = Cli::try_parse_from([
"dekopon",
"auth",
"chatgpt",
"status",
"--auth-file",
"auth.json",
])
.expect("valid auth command");
assert!(matches!(
cli.command,
Command::Auth {
account: AuthCommand::ChatGpt {
command: ChatGptAuthCommand::Status { .. }
}
}
));
}
#[test]
fn export_requires_the_credential_acknowledgement() {
let refused = Cli::try_parse_from(["dekopon", "auth", "chatgpt", "export"])
.expect_err("export without acknowledgement must not parse");
assert!(refused.to_string().contains("--expose-credential"));
let cli = Cli::try_parse_from([
"dekopon",
"auth",
"chatgpt",
"export",
"--expose-credential",
])
.expect("acknowledged export parses");
assert!(matches!(
cli.command,
Command::Auth {
account: AuthCommand::ChatGpt {
command: ChatGptAuthCommand::Export {
format: ExportFormat::Secret,
expose_credential: true,
allow_terminal: false,
..
}
}
}
));
}
#[test]
fn export_rejects_a_secret_name_the_api_server_would_reject() {
assert!(dns_subdomain("dekopon-chatgpt-auth").is_ok());
assert!(dns_subdomain("dekopon.chatgpt.auth").is_ok());
assert!(dns_subdomain("").is_err());
assert!(dns_subdomain("-leading").is_err());
assert!(dns_subdomain("trailing-").is_err());
assert!(dns_subdomain("Upper").is_err());
assert!(dns_subdomain("double..dot").is_err());
assert!(dns_subdomain(&"a".repeat(254)).is_err());
assert!(dns_label("dekopon").is_ok());
assert!(dns_label("dekopon.agents").is_err());
assert!(dns_label(&"a".repeat(64)).is_err());
}
#[test]
fn parses_global_output_after_nested_command() {
let cli = Cli::try_parse_from(["dekopon", "get", "agents", "-o", "wide"])
.expect("valid command line");
assert_eq!(cli.output, OutputFormat::Wide);
assert!(matches!(
cli.command,
Command::Get {
resource: GetCommand::Agents
}
));
}
}