From 07021e4bd1416342d146751d4c1cbbff943cd509 Mon Sep 17 00:00:00 2001 From: Sebastian Imlay Date: Mon, 23 Nov 2020 10:41:54 -0800 Subject: [PATCH] Search extensions directory for fluvio extensions rather than PATH --- Cargo.lock | 10 ++++++++++ install.sh | 2 ++ src/cli/Cargo.toml | 1 + src/cli/src/install/mod.rs | 18 +++++++++++++++--- src/cli/src/install/plugins.rs | 8 ++------ src/cli/src/install/update.rs | 4 ++-- src/cli/src/root_cli.rs | 30 ++++++++++++++++++++++-------- 7 files changed, 54 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d1e06642ab..4bab314c1d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -942,6 +942,7 @@ dependencies = [ "fluvio-future", "fluvio-package-index", "hex", + "home", "http-types", "k8-client", "k8-config", @@ -1725,6 +1726,15 @@ dependencies = [ "digest", ] +[[package]] +name = "home" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2456aef2e6b6a9784192ae780c0f15bc57df0e918585282325e8c8ac27737654" +dependencies = [ + "winapi", +] + [[package]] name = "hostfile" version = "0.2.0" diff --git a/install.sh b/install.sh index 07ff076c28..38a4e54d40 100755 --- a/install.sh +++ b/install.sh @@ -6,6 +6,7 @@ set -u set -o pipefail readonly FLUVIO_BIN="${HOME}/.fluvio/bin" +readonly FLUVIO_EXTENSIONS="${HOME}/.fluvio/extensions" readonly FLUVIO_LATEST_URL="https://packages.fluvio.io/v1/latest" # Ensure that this architecture is supported and matches the @@ -507,6 +508,7 @@ main() { # After verification, install the file and make it executable say "⬇️ Downloaded Fluvio, installing..." ensure mkdir -p "${FLUVIO_BIN}" + ensure mkdir -p "${FLUVIO_EXTENSIONS}" local _install_file="${FLUVIO_BIN}/fluvio" ensure mv "${_temp_file}" "${_install_file}" ensure chmod +x "${_install_file}" diff --git a/src/cli/Cargo.toml b/src/cli/Cargo.toml index f508439e45..1aff56b356 100644 --- a/src/cli/Cargo.toml +++ b/src/cli/Cargo.toml @@ -38,6 +38,7 @@ color-eyre = "0.5.5" which = "4.0.2" sha2 = "0.9.1" hex = "0.4.2" +home = "0.5.3" # Fluvio dependencies diff --git a/src/cli/src/install/mod.rs b/src/cli/src/install/mod.rs index ca98db9915..15fdc49e79 100644 --- a/src/cli/src/install/mod.rs +++ b/src/cli/src/install/mod.rs @@ -9,10 +9,22 @@ use crate::CliError; pub mod update; pub mod plugins; -fn fluvio_bin_dir() -> Result { +pub(crate) fn fluvio_extensions_dir() -> Result { + if let Ok(dir) = std::env::var("FLUVIO_DIR") { + // Assume this is like `~/.fluvio + let path = PathBuf::from(dir).join("extensions"); + if path.exists() { + return Ok(path); + } + } + let home = - dirs::home_dir().ok_or_else(|| IoError::new(ErrorKind::NotFound, "Homedir not found"))?; - Ok(home.join(".fluvio/bin/")) + home::home_dir().ok_or_else(|| IoError::new(ErrorKind::NotFound, "Homedir not found"))?; + let path = home.join(".fluvio/extensions/"); + if path.exists() { + return Ok(path); + } + Err(IoError::new(ErrorKind::NotFound, "Fluvio extensions directory not found").into()) } /// Fetches the latest version of the package with the given ID diff --git a/src/cli/src/install/plugins.rs b/src/cli/src/install/plugins.rs index 85c89c2920..cb433a0393 100644 --- a/src/cli/src/install/plugins.rs +++ b/src/cli/src/install/plugins.rs @@ -3,7 +3,7 @@ use fluvio_index::{PackageId, HttpAgent, MaybeVersion}; use crate::CliError; use crate::install::{ - fetch_latest_version, fetch_package_file, fluvio_bin_dir, install_bin, install_println, + fetch_latest_version, fetch_package_file, fluvio_extensions_dir, install_bin, install_println, }; use crate::install::update::{ check_update_required, prompt_required_update, check_update_available, prompt_available_update, @@ -79,12 +79,8 @@ impl InstallOpt { install_println("🔑 Downloaded and verified package file"); // Install the package to the ~/.fluvio/bin/ dir - let fluvio_dir = fluvio_bin_dir()?; + let fluvio_dir = fluvio_extensions_dir()?; install_bin(&fluvio_dir, id.name.as_str(), &package_file)?; - install_println(format!( - "✅ Successfully installed ~/.fluvio/bin/{}", - &id.name - )); Ok("".to_string()) } diff --git a/src/cli/src/install/update.rs b/src/cli/src/install/update.rs index 14e995a1d4..c5507e8c12 100644 --- a/src/cli/src/install/update.rs +++ b/src/cli/src/install/update.rs @@ -5,7 +5,7 @@ use semver::Version; use fluvio_index::{PackageId, HttpAgent, MaybeVersion}; use crate::CliError; use crate::install::{ - fetch_latest_version, fetch_package_file, install_bin, fluvio_bin_dir, install_println, + fetch_latest_version, fetch_package_file, install_bin, fluvio_extensions_dir, install_println, }; const FLUVIO_PACKAGE_ID: &str = "fluvio/fluvio"; @@ -48,7 +48,7 @@ async fn update_self(agent: &HttpAgent) -> Result { install_println("🔑 Downloaded and verified package file"); // Install the package to the ~/.fluvio/bin/ dir - let fluvio_dir = fluvio_bin_dir()?; + let fluvio_dir = fluvio_extensions_dir()?; install_bin(&fluvio_dir, "fluvio", &package_file)?; install_println(format!( "✅ Successfully installed ~/.fluvio/bin/{}", diff --git a/src/cli/src/root_cli.rs b/src/cli/src/root_cli.rs index ea36461aa2..28f82a0bec 100644 --- a/src/cli/src/root_cli.rs +++ b/src/cli/src/root_cli.rs @@ -286,24 +286,38 @@ impl CompletionCmd { } fn process_external_subcommand(mut args: Vec) -> Result<()> { + use std::fs; use std::process::Command; - use which::{CanonicalPath, Error as WhichError}; + use std::path::PathBuf; // The external subcommand's name is given as the first argument, take it. let cmd = args.remove(0); - // Check for a matching external command in the environment + let external_subcommand = format!("fluvio-{}", cmd); - let subcommand_path = match CanonicalPath::new(&external_subcommand) { - Ok(path) => path, - Err(WhichError::CannotFindBinaryPath) => { + let mut subcommand_path: Option = None; + + let fluvio_dir = crate::install::fluvio_extensions_dir()?; + + if let Ok(entries) = fs::read_dir(&fluvio_dir) { + for entry in entries { + if let Ok(entry) = entry { + if entry.path().ends_with(&external_subcommand) { + subcommand_path = Some(entry.path()); + break; + } + } + } + } + let subcommand_path = match subcommand_path { + Some(path) => path, + None => { println!( - "Unable to find plugin '{}'. Make sure it is executable and in your PATH.", - &external_subcommand + "Unable to find plugin '{}'. Make sure it is installed in {:?}.", + &external_subcommand, fluvio_dir, ); std::process::exit(1); } - other => other?, }; // Print the fully-qualified command to debug