Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
add cfg_dir function to get/create .wash directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Wilkinson committed Dec 12, 2021
1 parent 70d4489 commit 980ea1f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
27 changes: 27 additions & 0 deletions src/cfg.rs
Original file line number Diff line number Diff line change
@@ -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<PathBuf> {
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)
}
8 changes: 5 additions & 3 deletions src/ctx/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
cfg::cfg_dir,
generate::{
interactive::{prompt_for_choice, user_question},
project_variables::StringEntry,
Expand All @@ -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";
Expand Down Expand Up @@ -406,11 +407,12 @@ pub(crate) fn context_dir(cmd_dir: Option<PathBuf>) -> Result<PathBuf> {
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)
}
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use smithy::{GenerateCli, LintCli, ValidateCli};
use structopt::{clap::AppSettings, StructOpt};

mod call;
mod cfg;
mod claims;
mod ctl;
mod ctx;
Expand Down

0 comments on commit 980ea1f

Please sign in to comment.