110 lines
2.8 KiB
Rust
110 lines
2.8 KiB
Rust
use std::{io::Read, net::SocketAddr};
|
|
|
|
use anyhow::Context;
|
|
use clap::{Parser, Subcommand};
|
|
use copy::LocalCopierState;
|
|
use grpc::void_pin_server::VoidPinServer;
|
|
use grpc_server::GrpcServer;
|
|
use remote_copy::RemoteCopierState;
|
|
use state::State;
|
|
use tonic::transport;
|
|
|
|
mod grpc {
|
|
include!("gen/voidpin.v1.rs");
|
|
}
|
|
|
|
mod grpc_server;
|
|
|
|
mod copy;
|
|
mod remote_copy;
|
|
mod state;
|
|
|
|
#[derive(Parser)]
|
|
#[command(author, version, about, long_about = None, subcommand_required = true)]
|
|
struct Command {
|
|
#[command(subcommand)]
|
|
command: Option<Commands>,
|
|
}
|
|
|
|
#[derive(Subcommand)]
|
|
enum Commands {
|
|
Listen {
|
|
#[arg(long, env = "VOIDPIN_GRPC_HOST", default_value = "0.0.0.0:7900")]
|
|
grpc: SocketAddr,
|
|
},
|
|
Copy {},
|
|
Remote {
|
|
#[command(subcommand)]
|
|
command: RemoteCommands,
|
|
},
|
|
}
|
|
|
|
#[derive(Subcommand)]
|
|
enum RemoteCommands {
|
|
Copy {
|
|
#[arg(
|
|
long = "remote-host",
|
|
env = "VOIDPIN_REMOTE",
|
|
default_value = "http://0.0.0.0:7900"
|
|
)]
|
|
remote_host: String,
|
|
},
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() -> anyhow::Result<()> {
|
|
dotenv::dotenv().ok();
|
|
tracing_subscriber::fmt::init();
|
|
|
|
let cli = Command::parse();
|
|
tracing::debug!("Starting cli");
|
|
|
|
let state = State::new();
|
|
|
|
match cli.command.unwrap() {
|
|
Commands::Listen { grpc } => {
|
|
tracing::info!(grpc = grpc.to_string(), "starting listener");
|
|
transport::Server::builder()
|
|
.add_service(VoidPinServer::new(GrpcServer::new(state)))
|
|
.serve(grpc)
|
|
.await?;
|
|
}
|
|
Commands::Copy {} => {
|
|
let mut input = String::new();
|
|
std::io::stdin()
|
|
.read_to_string(&mut input)
|
|
.context("failed to read from stdin")?;
|
|
|
|
if input.is_empty() {
|
|
tracing::info!("no content to put in clipboard");
|
|
return Ok(());
|
|
}
|
|
|
|
tracing::debug!(content = &input, "found content");
|
|
state.local_copier().copy(input.as_bytes()).await?;
|
|
}
|
|
Commands::Remote { command } => match command {
|
|
RemoteCommands::Copy { remote_host } => {
|
|
let mut input = String::new();
|
|
std::io::stdin()
|
|
.read_to_string(&mut input)
|
|
.context("failed to read from stdin")?;
|
|
|
|
if input.is_empty() {
|
|
tracing::info!("no content to put in clipboard");
|
|
return Ok(());
|
|
}
|
|
|
|
tracing::debug!(content = &input, "found content");
|
|
state
|
|
.remote_copier(&remote_host)
|
|
.copy(input.as_bytes())
|
|
.await?;
|
|
}
|
|
},
|
|
_ => (),
|
|
}
|
|
|
|
Ok(())
|
|
}
|