From 8c961f7c93b072dda7a58bf3edcfe6c6381aee30 Mon Sep 17 00:00:00 2001 From: Kate Goldenring Date: Tue, 30 May 2023 17:51:32 -0500 Subject: [PATCH] Move templates and plugins shared data dir methods to common crate Signed-off-by: Kate Goldenring --- Cargo.lock | 1 + crates/common/Cargo.toml | 1 + crates/common/src/data_dir.rs | 29 +++++++++++++++++++++++++++++ crates/common/src/lib.rs | 1 + crates/plugins/src/store.rs | 24 +++++------------------- crates/templates/src/store.rs | 22 +++------------------- 6 files changed, 40 insertions(+), 38 deletions(-) create mode 100644 crates/common/src/data_dir.rs diff --git a/Cargo.lock b/Cargo.lock index 2c85ec60ed..46b41e3795 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5177,6 +5177,7 @@ name = "spin-common" version = "1.3.0-pre0" dependencies = [ "anyhow", + "dirs 4.0.0", "sha2 0.10.6", "tempfile", "tokio", diff --git a/crates/common/Cargo.toml b/crates/common/Cargo.toml index 19543580ad..b9fc146873 100644 --- a/crates/common/Cargo.toml +++ b/crates/common/Cargo.toml @@ -6,6 +6,7 @@ edition = { workspace = true } [dependencies] anyhow = "1.0" +dirs = "4.0" sha2 = "0.10" tokio = { version = "1", features = ["rt", "time"] } diff --git a/crates/common/src/data_dir.rs b/crates/common/src/data_dir.rs new file mode 100644 index 0000000000..62b6f9de40 --- /dev/null +++ b/crates/common/src/data_dir.rs @@ -0,0 +1,29 @@ +//! Resolves Spin's default data directory paths + +use anyhow::{anyhow, Result}; +use std::path::{Path, PathBuf}; + +/// Return the default data directory for Spin +pub fn default_data_dir() -> Result { + if let Some(pkg_mgr_dir) = package_manager_data_dir() { + return Ok(pkg_mgr_dir); + } + + let data_dir = dirs::data_local_dir() + .or_else(|| dirs::home_dir().map(|p| p.join(".spin"))) + .ok_or_else(|| anyhow!("Unable to get local data directory or home directory"))?; + Ok(data_dir.join("spin")) +} + +/// Get the package manager specific data directory +fn package_manager_data_dir() -> Option { + if let Ok(brew_prefix) = std::env::var("HOMEBREW_PREFIX") { + let data_dir = Path::new(&brew_prefix).join("var").join("spin"); + + if data_dir.is_dir() { + return Some(data_dir); + // TODO: check if they also have plugins in non-brew default dir and warn + } + } + None +} diff --git a/crates/common/src/lib.rs b/crates/common/src/lib.rs index 5fb3874d72..f1b927d842 100644 --- a/crates/common/src/lib.rs +++ b/crates/common/src/lib.rs @@ -9,5 +9,6 @@ // - Code should have at least 2 dependents pub mod arg_parser; +pub mod data_dir; pub mod sha256; pub mod sloth; diff --git a/crates/plugins/src/store.rs b/crates/plugins/src/store.rs index dc9ce8ed68..cfc8977eb2 100644 --- a/crates/plugins/src/store.rs +++ b/crates/plugins/src/store.rs @@ -1,5 +1,6 @@ -use anyhow::{anyhow, Context, Result}; +use anyhow::{Context, Result}; use flate2::read::GzDecoder; +use spin_common::data_dir::default_data_dir; use std::{ ffi::OsStr, fs::{self, File}, @@ -24,25 +25,10 @@ impl PluginStore { } pub fn try_default() -> Result { - if let Ok(brew_prefix) = std::env::var("HOMEBREW_PREFIX") { - let plugins_dir = Path::new(&brew_prefix) - .join("var") - .join("spin") - .join("plugins"); - - if plugins_dir.is_dir() { - return Ok(Self::new(plugins_dir)); - // TODO: check if they also have plugins in non-brew default dir and warn - } + if let Ok(test_dir) = std::env::var("TEST_PLUGINS_DIRECTORY") { + return Ok(Self::new(PathBuf::from(test_dir))); } - let data_dir = match std::env::var("TEST_PLUGINS_DIRECTORY") { - Ok(test_dir) => PathBuf::from(test_dir), - Err(_) => dirs::data_local_dir() - .or_else(|| dirs::home_dir().map(|p| p.join(".spin"))) - .ok_or_else(|| anyhow!("Unable to get local data directory or home directory"))?, - }; - let plugins_dir = data_dir.join("spin").join("plugins"); - Ok(Self::new(plugins_dir)) + Ok(Self::new(default_data_dir()?.join("plugins"))) } /// Gets the path to where Spin plugin are installed. diff --git a/crates/templates/src/store.rs b/crates/templates/src/store.rs index 6b34af5bdf..adf4d1385a 100644 --- a/crates/templates/src/store.rs +++ b/crates/templates/src/store.rs @@ -1,7 +1,7 @@ +use anyhow::Context; +use spin_common::data_dir::default_data_dir; use std::path::{Path, PathBuf}; -use anyhow::{anyhow, Context}; - use crate::directory::subdirectories; pub(crate) struct TemplateStore { @@ -20,23 +20,7 @@ impl TemplateStore { } pub(crate) fn try_default() -> anyhow::Result { - if let Ok(brew_prefix) = std::env::var("HOMEBREW_PREFIX") { - let templates_dir = Path::new(&brew_prefix) - .join("var") - .join("spin") - .join("templates"); - - if templates_dir.is_dir() { - return Ok(Self::new(templates_dir)); - // TODO: check if they also have templates in non-brew default dir and warn - } - } - - let data_dir = dirs::data_local_dir() - .or_else(|| dirs::home_dir().map(|p| p.join(".spin"))) - .ok_or_else(|| anyhow!("Unable to get local data directory or home directory"))?; - let templates_dir = data_dir.join("spin").join("templates"); - Ok(Self::new(templates_dir)) + Ok(Self::new(default_data_dir()?.join("templates"))) } pub(crate) fn get_directory(&self, id: impl AsRef) -> PathBuf {