config init now creates directory structure as fallback

This commit is contained in:
Valerie Wolfe 2023-08-08 11:11:02 -04:00
parent 3b7ea51e04
commit 8120a7cd9a
2 changed files with 45 additions and 3 deletions

View file

@ -3,8 +3,10 @@
use std::{ use std::{
collections::HashMap, collections::HashMap,
env::var, env::var,
fs::read_to_string, fs::{ create_dir, read_to_string, File },
path::PathBuf io::Error,
path::PathBuf,
process::exit
}; };
use tera::Context; use tera::Context;
@ -13,6 +15,8 @@ use toml::{
Value Value
}; };
use crate::error;
/// configuration struct /// configuration struct
pub struct Config { pub struct Config {
pub dir: String, pub dir: String,
@ -27,6 +31,14 @@ impl Config {
let mut dir = PathBuf::from(&home); let mut dir = PathBuf::from(&home);
dir.push(".config/oink"); dir.push(".config/oink");
if !dir.exists() {
println!("missing directory at {dir:?}, creating...");
let setup = setup_dirs(&dir);
if setup.is_err() { error::setup_dirs(&dir); }
println!("created configuration directory, exiting...");
exit(0);
}
// read toml file // read toml file
let mut toml_path = dir.clone(); let mut toml_path = dir.clone();
toml_path.push("oink.toml"); toml_path.push("oink.toml");
@ -81,3 +93,25 @@ impl Config {
} }
} }
fn setup_dirs(path: &PathBuf) -> Result<(), Error> {
// create main directory
create_dir(path)?;
// create template directory
let mut templates = path.clone();
templates.push("templates/");
create_dir(templates)?;
// create generated directory
let mut generated = path.clone();
generated.push("generated/");
create_dir(generated)?;
// create main toml
let mut toml = path.clone();
toml.push("oink.toml");
File::create(toml)?;
Ok(())
}

View file

@ -1,7 +1,15 @@
use std::process::exit; use std::{
path::PathBuf,
process::exit
};
pub fn no_targets() { pub fn no_targets() {
println!("oink: configuration has no targets"); println!("oink: configuration has no targets");
exit(2); exit(2);
} }
pub fn setup_dirs(path: &PathBuf) {
println!("oink: failed to create directory structure at {path:?}");
exit(3);
}