diff --git a/src/cfg.rs b/src/cfg.rs new file mode 100644 index 00000000..0c86f112 --- /dev/null +++ b/src/cfg.rs @@ -0,0 +1,27 @@ +use crate::util::Result; +use std::{ + fs, + io::{Error, ErrorKind}, + path::PathBuf, +}; + +const WASH_DIR: &str = ".wash"; + +/// Get the path to the `.wash` configuration directory. +/// Creates the directory if it does not exist. +pub fn cfg_dir() -> Result { + let home = dirs::home_dir().ok_or_else(|| { + Error::new( + ErrorKind::NotFound, + "No home directory found. Please set $HOME.", + ) + })?; + + let wash = home.join(WASH_DIR); + + if !wash.exists() { + fs::create_dir_all(&wash)?; + } + + Ok(wash) +} diff --git a/src/ctx/mod.rs b/src/ctx/mod.rs index 64b7b757..3c1b7216 100644 --- a/src/ctx/mod.rs +++ b/src/ctx/mod.rs @@ -1,4 +1,5 @@ use crate::{ + cfg::cfg_dir, generate::{ interactive::{prompt_for_choice, user_question}, project_variables::StringEntry, @@ -21,7 +22,7 @@ use structopt::{clap::AppSettings, StructOpt}; pub mod context; use context::{DefaultContext, WashContext}; -const CTX_DIR: &str = ".wash/contexts"; +const CTX_DIR: &str = "contexts"; const INDEX_JSON: &str = "index.json"; const HOST_CONFIG_PATH: &str = ".wash/host_config.json"; const HOST_CONFIG_NAME: &str = "host_config"; @@ -406,11 +407,12 @@ pub(crate) fn context_dir(cmd_dir: Option) -> Result { let dir = if let Some(dir) = cmd_dir { dir } else { - home_dir()?.join(CTX_DIR) + cfg_dir()?.join(CTX_DIR) }; + // Ensure user supplied context exists if std::fs::metadata(&dir).is_err() { - let _ = std::fs::create_dir(&dir); + let _ = std::fs::create_dir_all(&dir); } Ok(dir) } diff --git a/src/main.rs b/src/main.rs index 7b5279c9..ebae8cf1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,7 @@ use smithy::{GenerateCli, LintCli, ValidateCli}; use structopt::{clap::AppSettings, StructOpt}; mod call; +mod cfg; mod claims; mod ctl; mod ctx;