mod account;
mod approve;
mod attach;
mod host;
mod keyring;
mod machine;
mod remote;
mod sessions;
mod token;
use std::process::ExitCode;
use clap::{Parser, Subcommand};
const VERSION: &str = match option_env!("TETER_VERSION") {
Some(version) if !version.is_empty() => version,
_ if matches!(env!("CARGO_PKG_VERSION").as_bytes(), b"0.0.0") => "dev",
_ => env!("CARGO_PKG_VERSION"),
};
#[derive(Parser)]
#[command(version = VERSION)]
struct Cli {
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
Link(host::LinkArgs),
Status(host::StatusArgs),
Kill(host::KillArgs),
Ls(remote::LsArgs),
New(remote::NewArgs),
Attach(attach::AttachArgs),
Signup(account::SignupArgs),
Login(account::LoginArgs),
Logout(account::LogoutArgs),
Password(account::PasswordArgs),
RotateKeys(account::RotateArgs),
Token(token::TokenArgs),
Register(machine::RegisterArgs),
Unregister(machine::UnregisterArgs),
Machine(machine::MachineArgs),
#[command(hide = true)]
Session(host::SessionArgs),
}
fn endpoint(relay: &str, path: &str) -> String {
let host_at = relay.find("://").map_or(0, |at| at + 3);
let path_at = relay
.get(host_at..)
.and_then(|rest| rest.find('/'))
.map_or(relay.len(), |at| host_at + at);
let (origin, relay_path) = relay.split_at(path_at);
let relay_path = relay_path.trim_end_matches('/');
let relay_path = relay_path
.strip_suffix("/backend")
.or_else(|| relay_path.strip_suffix("/frontend"))
.unwrap_or(relay_path);
format!("{origin}{relay_path}/{path}")
}
#[tokio::main]
async fn main() -> ExitCode {
let cli = Cli::parse();
if !matches!(cli.command, Command::Attach(_)) {
tracing_subscriber::fmt()
.with_writer(std::io::stderr)
.with_env_filter(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "warn".into()),
)
.init();
}
let (result, failure) = match cli.command {
Command::Link(args) => {
let in_use_status = args.in_use_status;
let result = host::link(args).await;
let failure = match &result {
Err(e) if teter_backend::LinkInUse::is(e) => ExitCode::from(in_use_status),
_ => ExitCode::FAILURE,
};
(result, failure)
}
Command::Status(args) => (host::status(args).await, ExitCode::FAILURE),
Command::Kill(args) => (host::kill(args).await, ExitCode::FAILURE),
Command::Ls(args) => (remote::ls(args).await, ExitCode::FAILURE),
Command::New(args) => (remote::new(args).await, ExitCode::FAILURE),
Command::Signup(args) => (account::signup(args).await, ExitCode::FAILURE),
Command::Login(args) => (account::login(args).await, ExitCode::FAILURE),
Command::Logout(args) => (account::logout(args).await, ExitCode::FAILURE),
Command::Password(args) => (account::change_password(args).await, ExitCode::FAILURE),
Command::RotateKeys(args) => (account::rotate_keys(args).await, ExitCode::FAILURE),
Command::Token(args) => (token::token(args).await, ExitCode::FAILURE),
Command::Register(args) => (machine::register(args).await, ExitCode::FAILURE),
Command::Unregister(args) => (machine::unregister(args).await, ExitCode::FAILURE),
Command::Machine(args) => (machine::machine(args).await, ExitCode::FAILURE),
Command::Session(args) => (host::serve_session(args).await, host::session_failure()),
Command::Attach(args) => std::process::exit(attach::attach(args).await),
};
match result {
Ok(()) => ExitCode::SUCCESS,
Err(e) => {
eprintln!("teter: {e}");
failure
}
}
}
#[cfg(test)]
mod tests {
use super::endpoint;
#[test]
fn either_endpoint_or_the_relay_alone_names_the_relay() {
for relay in [
"wss://relay.example.com",
"wss://relay.example.com/",
"wss://relay.example.com/backend",
"wss://relay.example.com/frontend/",
] {
assert_eq!(
endpoint(relay, "backend"),
"wss://relay.example.com/backend"
);
assert_eq!(
endpoint(relay, "frontend"),
"wss://relay.example.com/frontend"
);
}
assert_eq!(
endpoint("ws://backend", "frontend"),
"ws://backend/frontend"
);
assert_eq!(
endpoint("ws://frontend/", "backend"),
"ws://frontend/backend"
);
assert_eq!(
endpoint("ws://backend/backend", "frontend"),
"ws://backend/frontend"
);
assert_eq!(
endpoint("wss://example.com/teter/backend", "frontend"),
"wss://example.com/teter/frontend"
);
}
}