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

fix/feat: add cfg module to get or create the .wash directory #216

Merged
merged 4 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
This conversation was marked as resolved.
Show resolved Hide resolved
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