Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add incompatibility warnings if the tab binary or version differs #180

Merged
merged 2 commits into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 150 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions common/tab-api/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use sysinfo::{RefreshKind, SystemExt};
pub struct DaemonConfig {
pub pid: i32,
pub port: u16,
pub executable: Option<String>,
pub tab_version: Option<String>,
pub auth_token: String,
}

Expand Down
1 change: 1 addition & 0 deletions tab-command/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ simplelog = "0.8"
# error management
anyhow = "1.0"
thiserror = "1.0"
semver = "0.11"

# config
serde = {version = "1.0", features = ["derive"]}
Expand Down
58 changes: 52 additions & 6 deletions tab-command/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::path::PathBuf;

use clap::ArgMatches;
use semver::Version;

use crate::prelude::*;
use service::{main::*, terminal::disable_raw_mode, terminal::reset_cursor};
Expand All @@ -9,7 +12,7 @@ use crate::bus::MainBus;
use message::main::{MainRecv, MainShutdown};

use lifeline::dyn_bus::DynBus;
use tab_api::{launch::*, log::get_level, tab::normalize_name};
use tab_api::{config::DaemonConfig, launch::*, log::get_level, tab::normalize_name};
use tab_websocket::resource::connection::WebsocketResource;

mod bus;
Expand All @@ -19,7 +22,7 @@ mod prelude;
mod service;
mod state;

pub fn command_main(args: ArgMatches) -> anyhow::Result<()> {
pub fn command_main(args: ArgMatches, tab_version: &'static str) -> anyhow::Result<()> {
TermLogger::init(
get_level().unwrap_or(LevelFilter::Warn),
simplelog::ConfigBuilder::new()
Expand All @@ -38,7 +41,7 @@ pub fn command_main(args: ArgMatches) -> anyhow::Result<()> {
.build()
.unwrap();

let result = runtime.block_on(async { main_async(args).await });
let result = runtime.block_on(async { main_async(args, tab_version).await });

runtime.shutdown_background();

Expand All @@ -49,10 +52,10 @@ pub fn command_main(args: ArgMatches) -> anyhow::Result<()> {
Ok(())
}

async fn main_async(matches: ArgMatches<'_>) -> anyhow::Result<()> {
async fn main_async(matches: ArgMatches<'_>, tab_version: &'static str) -> anyhow::Result<()> {
let select_tab = matches.value_of("TAB-NAME");
let close_tabs = matches.values_of("CLOSE-TAB");
let (mut tx, rx_shutdown, _service) = spawn().await?;
let (mut tx, rx_shutdown, _service) = spawn(tab_version).await?;
let completion = matches.is_present("AUTOCOMPLETE-TAB");
let close_completion = matches.is_present("AUTOCOMPLETE-CLOSE-TAB");
let shutdown = matches.is_present("SHUTDOWN");
Expand Down Expand Up @@ -82,12 +85,15 @@ async fn main_async(matches: ArgMatches<'_>) -> anyhow::Result<()> {
Ok(())
}

async fn spawn() -> anyhow::Result<(
async fn spawn(
tab_version: &'static str,
) -> anyhow::Result<(
impl Sender<MainRecv>,
impl Receiver<MainShutdown>,
MainService,
)> {
let daemon_file = launch_daemon().await?;
validate_daemon(&daemon_file, tab_version);
let ws_url = format!("ws://127.0.0.1:{}/cli", daemon_file.port);

debug!("daemon is ready");
Expand All @@ -109,3 +115,43 @@ async fn spawn() -> anyhow::Result<(

Ok((tx, main_shutdown, service))
}

fn validate_daemon(config: &DaemonConfig, tab_version: &'static str) {
let executable = std::env::current_exe()
.ok()
.map(|path| path.to_str().map(str::to_string))
.flatten();

let tab_version = Version::parse(tab_version).ok();
let daemon_version = config
.tab_version
.as_ref()
.map(String::as_str)
.map(Version::parse)
.map(Result::ok)
.flatten();

if let (Some(tab_version), Some(daemon_version)) = (tab_version, daemon_version) {
if tab_version.major != daemon_version.major || tab_version.minor != daemon_version.minor {
eprintln!("Warning: The tab command (v{}) has an incompatible version with the running daemon (v{})", tab_version, daemon_version);
eprintln!(
" You should run `tab --shutdown` to terminate your tabs and relaunch the daemon."
);

return;
}
}

if let (Some(executable), Some(daemon_exec)) = (&executable, &config.executable) {
if executable != daemon_exec {
eprintln!(
"Warning: The tab command has a different executable path than the running daemon."
);
eprintln!(" You may want to run `tab --shutdown` to terminate your tabs and relaunch the daemon.");

eprintln!(" Tab command: {}", executable);
eprintln!(" Daemon command: {}", daemon_exec);
return;
}
}
}
Loading