Skip to content

Commit

Permalink
Merge pull request fermyon#1494 from kate-goldenring/brew
Browse files Browse the repository at this point in the history
Conditionally use Homebrew location for Spin plugins and templates
  • Loading branch information
kate-goldenring authored Jun 8, 2023
2 parents ffe6a06 + f30f1f5 commit 459ee0e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 15 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = { workspace = true }

[dependencies]
anyhow = "1.0"
dirs = "4.0"
sha2 = "0.10"
tokio = { version = "1", features = ["rt", "time"] }

Expand Down
30 changes: 30 additions & 0 deletions crates/common/src/data_dir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//! 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<PathBuf> {
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<PathBuf> {
if let Ok(brew_prefix) = std::env::var("HOMEBREW_PREFIX") {
if std::env::current_exe()
.map(|p| p.starts_with(&brew_prefix))
.unwrap_or(false)
{
let data_dir = Path::new(&brew_prefix).join("etc").join("fermyon-spin");
return Some(data_dir);
}
}
None
}
1 change: 1 addition & 0 deletions crates/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
15 changes: 7 additions & 8 deletions crates/plugins/src/store.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -24,14 +25,12 @@ impl PluginStore {
}

pub fn try_default() -> Result<Self> {
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 data_dir = if let Ok(test_dir) = std::env::var("TEST_PLUGINS_DIRECTORY") {
PathBuf::from(test_dir).join("spin")
} else {
default_data_dir()?
};
let plugins_dir = data_dir.join("spin").join("plugins");
Ok(Self::new(plugins_dir))
Ok(Self::new(data_dir.join("plugins")))
}

/// Gets the path to where Spin plugin are installed.
Expand Down
10 changes: 3 additions & 7 deletions crates/templates/src/store.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -20,11 +20,7 @@ impl TemplateStore {
}

pub(crate) fn try_default() -> anyhow::Result<Self> {
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<str>) -> PathBuf {
Expand Down

0 comments on commit 459ee0e

Please sign in to comment.