main.rs

  1#![cfg_attr(target_os = "windows", allow(unused, dead_code))]
  2
  3use clap::{Parser, Subcommand};
  4use std::path::PathBuf;
  5
  6#[derive(Parser)]
  7#[command(disable_version_flag = true)]
  8struct Cli {
  9    #[command(subcommand)]
 10    command: Option<Commands>,
 11    /// Used for SSH/Git password authentication, to remove the need for netcat as a dependency,
 12    /// by having Zed act like netcat communicating over a Unix socket.
 13    #[arg(long, hide = true)]
 14    askpass: Option<String>,
 15    /// Used for loading the environment from the project.
 16    #[arg(long, hide = true)]
 17    printenv: bool,
 18}
 19
 20#[derive(Subcommand)]
 21enum Commands {
 22    Run {
 23        #[arg(long)]
 24        log_file: PathBuf,
 25        #[arg(long)]
 26        pid_file: PathBuf,
 27        #[arg(long)]
 28        stdin_socket: PathBuf,
 29        #[arg(long)]
 30        stdout_socket: PathBuf,
 31        #[arg(long)]
 32        stderr_socket: PathBuf,
 33    },
 34    Proxy {
 35        #[arg(long)]
 36        reconnect: bool,
 37        #[arg(long)]
 38        identifier: String,
 39    },
 40    Version,
 41}
 42
 43#[cfg(windows)]
 44fn main() {
 45    unimplemented!()
 46}
 47
 48#[cfg(not(windows))]
 49fn main() {
 50    use release_channel::{RELEASE_CHANNEL, ReleaseChannel};
 51    use remote::proxy::ProxyLaunchError;
 52    use remote_server::unix::{execute_proxy, execute_run};
 53
 54    let cli = Cli::parse();
 55
 56    if let Some(socket_path) = &cli.askpass {
 57        askpass::main(socket_path);
 58        return;
 59    }
 60
 61    if cli.printenv {
 62        util::shell_env::print_env();
 63        return;
 64    }
 65
 66    let result = match cli.command {
 67        Some(Commands::Run {
 68            log_file,
 69            pid_file,
 70            stdin_socket,
 71            stdout_socket,
 72            stderr_socket,
 73        }) => execute_run(
 74            log_file,
 75            pid_file,
 76            stdin_socket,
 77            stdout_socket,
 78            stderr_socket,
 79        ),
 80        Some(Commands::Proxy {
 81            identifier,
 82            reconnect,
 83        }) => match execute_proxy(identifier, reconnect) {
 84            Ok(_) => Ok(()),
 85            Err(err) => {
 86                if let Some(err) = err.downcast_ref::<ProxyLaunchError>() {
 87                    std::process::exit(err.to_exit_code());
 88                }
 89                Err(err)
 90            }
 91        },
 92        Some(Commands::Version) => {
 93            let release_channel = *RELEASE_CHANNEL;
 94            match release_channel {
 95                ReleaseChannel::Stable | ReleaseChannel::Preview => {
 96                    println!("{}", env!("ZED_PKG_VERSION"))
 97                }
 98                ReleaseChannel::Nightly | ReleaseChannel::Dev => {
 99                    println!(
100                        "{}",
101                        option_env!("ZED_COMMIT_SHA").unwrap_or(release_channel.dev_name())
102                    )
103                }
104            };
105            std::process::exit(0);
106        }
107        None => {
108            eprintln!("usage: remote <run|proxy|version>");
109            std::process::exit(1);
110        }
111    };
112    if let Err(error) = result {
113        log::error!("exiting due to error: {}", error);
114        std::process::exit(1);
115    }
116}