Skip to content

Commit

Permalink
make config not delete unknown keys + config is no longer global
Browse files Browse the repository at this point in the history
  • Loading branch information
HJfod committed May 16, 2022
1 parent 3a32403 commit ced3412
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 81 deletions.
127 changes: 62 additions & 65 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ use std::vec::Vec;
use std::process::exit;
use std::fs;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use colored::Colorize;
use std::collections::HashMap;

type Other = HashMap<String, serde_json::Value>;

#[derive(Serialize, Deserialize, Clone)]
#[serde(rename_all = "kebab-case")]
pub struct Installation {
pub path: PathBuf,
pub executable: String,
#[serde(flatten)]
other: Option<Other>,
other: HashMap<String, Value>,
}

#[derive(Serialize, Deserialize, Clone)]
Expand All @@ -28,17 +27,9 @@ pub struct Config {
pub installations: Option<Vec<Installation>>,
pub default_developer: Option<String>,
#[serde(flatten)]
other: Option<Other>,
other: HashMap<String, Value>,
}

static mut CONFIG: Config = Config {
default_installation: 0,
working_installation: None,
installations: None,
default_developer: None,
other: None,
};

impl Config {
pub fn data_dir() -> PathBuf {
// get data dir per-platform
Expand All @@ -56,67 +47,73 @@ impl Config {
data_dir
}

pub fn init() {
unsafe {
let config_json = Config::data_dir().join("config.json");
if !config_json.exists() {
println!(
"{}{}{}{}",
"WARNING: It seems you don't have Geode installed! \
Please install Geode first using the official installer \
(".yellow(),
"https://github.com/geode-sdk/installer/releases/latest".cyan(),
")".yellow(),
"\nYou may still use the CLI, but be warned that certain \
operations will cause crashes.\n".purple()
);
fs::create_dir_all(Config::data_dir()).unwrap();
return;
}
CONFIG = match serde_json::from_str(
&fs::read_to_string(&config_json).unwrap()
) {
Ok(p) => p,
Err(e) => {
println!("Unable to parse config.json: {}", e);
exit(1);
}
};
if CONFIG.installations.is_none() {
println!(
"{}{}{}{}",
"WARNING: It seems you don't have any installations of Geode! \
Please install Geode first using the official installer \
(".yellow(),
"https://github.com/geode-sdk/installer/releases/latest".cyan(),
")".yellow(),
"\nYou may still use the CLI, but be warned that certain \
operations will cause crashes.\n".purple()
);
return;
}
if CONFIG.working_installation.is_none() {
CONFIG.working_installation = Some(CONFIG.default_installation);
pub fn init(&mut self) {
let config_json = Config::data_dir().join("config.json");
if !config_json.exists() {
println!(
"{}{}{}{}",
"WARNING: It seems you don't have Geode installed! \
Please install Geode first using the official installer \
(".yellow(),
"https://github.com/geode-sdk/installer/releases/latest".cyan(),
")".yellow(),
"\nYou may still use the CLI, but be warned that certain \
operations will cause crashes.\n".purple()
);
fs::create_dir_all(Config::data_dir()).unwrap();
return;
}
*self = match serde_json::from_str(
&fs::read_to_string(&config_json).unwrap()
) {
Ok(p) => p,
Err(e) => {
println!("Unable to parse config.json: {}", e);
exit(1);
}
};
if self.installations.is_none() {
println!(
"{}{}{}{}",
"WARNING: It seems you don't have any installations of Geode! \
Please install Geode first using the official installer \
(".yellow(),
"https://github.com/geode-sdk/installer/releases/latest".cyan(),
")".yellow(),
"\nYou may still use the CLI, but be warned that certain \
operations will cause crashes.\n".purple()
);
return;
}
if self.working_installation.is_none() {
self.working_installation = Some(
self.default_installation
);
}
}

pub fn get() -> &'static mut Config {
unsafe { &mut CONFIG }
pub fn new() -> Config {
let mut config = Config {
default_installation: 0,
working_installation: None,
installations: None,
default_developer: None,
other: HashMap::new(),
};
config.init();
config
}

pub fn save() {
unsafe {
fs::write(
Config::data_dir().join("config.json"),
serde_json::to_string(&CONFIG).unwrap()
).unwrap();
}
pub fn save(&mut self) {
fs::write(
Config::data_dir().join("config.json"),
serde_json::to_string(self).unwrap()
).unwrap();
}

pub fn work_inst() -> &'static Installation {
&Config::get().installations.as_ref().unwrap()[
Config::get().working_installation.unwrap()
pub fn work_inst(&mut self) -> &Installation {
&self.installations.as_ref().unwrap()[
self.working_installation.unwrap()
]
}
}
24 changes: 12 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,13 @@ fn main() {
Ok(_) => {},
Err(e) => println!("Unable to enable ANSI support: {}", e)
}

Config::init();
let mut config = Config::new();

let args = Cli::parse();

match args.command {
Commands::New { location, name } => template::build_template(name, location),
Commands::New { location, name } => template::build_template(&mut config, name, location),

Commands::Pkg { resource_dir, exec_dir, out_file, install, cached } => {
if let Err(e) = package::create_geode(
Expand All @@ -200,7 +200,7 @@ fn main() {

if install {
if let Err(e) = package::install_geode_file(
&Config::work_inst().path,
&config.work_inst().path,
&out_file
) {
print_error!("Error installing package: {}", e);
Expand All @@ -222,18 +222,18 @@ fn main() {
Commands::Config { cwi, dev } => {
let mut some_set = false;
if let Some(ver) = cwi {
if ver >= Config::get().installations.as_ref().unwrap().len() {
if ver >= config.installations.as_ref().unwrap().len() {
print_error!(
"Provided index is higher than your \
amount of installations!"
);
}
Config::get().working_installation = cwi;
config.working_installation = cwi;
some_set = true;
println!("Updated working installation");
}
if dev.is_some() {
Config::get().default_developer = dev;
config.default_developer = dev;
some_set = true;
println!("Updated default developer");
}
Expand All @@ -251,13 +251,13 @@ fn main() {
GEODE_CLI_VERSION.to_string().yellow(),
unsafe {link::geode_target_version()}.to_string().red(),
std::env::current_exe().unwrap().to_str().unwrap().cyan(),
match Config::get().default_developer.as_ref() {
match config.default_developer.as_ref() {
Some(s) => s,
None => "<none>"
}.purple(),
Config::data_dir().to_str().unwrap().cyan(),
Config::get().working_installation.unwrap().to_string().red(),
Config::work_inst().path.to_str().unwrap().cyan(),
config.working_installation.unwrap().to_string().red(),
config.work_inst().path.to_str().unwrap().cyan(),
);
}
},
Expand Down Expand Up @@ -321,11 +321,11 @@ fn main() {

Commands::Install { path } => {
package::install_geode_file(
&Config::work_inst().path,
&config.work_inst().path,
&path
).unwrap();
}
}

Config::save();
config.save();
}
8 changes: 4 additions & 4 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn ask_value(prompt: &str, default: &str, required: bool) -> String {
}
}

pub fn build_template(name: Option<String>, location: Option<PathBuf>) {
pub fn build_template(config: &mut Config, name: Option<String>, location: Option<PathBuf>) {
let name = match name {
Some(s) => ask_value("Name", s.as_str(), true),
None => ask_value("Name", "", true)
Expand All @@ -109,19 +109,19 @@ pub fn build_template(name: Option<String>, location: Option<PathBuf>) {
let version = ask_value("Version", "v1.0.0", true);
let developer = ask_value(
"Developer",
Config::get().default_developer.as_ref().unwrap_or(&String::new()),
config.default_developer.as_ref().unwrap_or(&String::new()),
true
);

if Config::get().default_developer.is_none() {
if config.default_developer.is_none() {
println!("{}{}{}\n{}{}",
"Using ".bright_cyan(),
developer,
" as default developer name for future projects.".bright_cyan(),
"If this is undesirable, use ".bright_cyan(),
"`geode config --dev <NAME>`".bright_yellow()
);
Config::get().default_developer = Some(developer.clone());
config.default_developer = Some(developer.clone());
}

let description = ask_value("Description", "", false);
Expand Down

0 comments on commit ced3412

Please sign in to comment.