Skip to content

Commit

Permalink
Move templates and plugins shared data dir methods to common crate
Browse files Browse the repository at this point in the history
Signed-off-by: Kate Goldenring <kate.goldenring@fermyon.com>
  • Loading branch information
kate-goldenring committed May 30, 2023
1 parent de2d1a8 commit 8c961f7
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 38 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
29 changes: 29 additions & 0 deletions crates/common/src/data_dir.rs
Original file line number Diff line number Diff line change
@@ -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<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") {
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
}
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;
24 changes: 5 additions & 19 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,25 +25,10 @@ impl PluginStore {
}

pub fn try_default() -> Result<Self> {
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.
Expand Down
22 changes: 3 additions & 19 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,23 +20,7 @@ impl TemplateStore {
}

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

0 comments on commit 8c961f7

Please sign in to comment.