Skip to content

Commit ced3412

Browse files
committed
make config not delete unknown keys + config is no longer global
1 parent 3a32403 commit ced3412

File tree

3 files changed

+78
-81
lines changed

3 files changed

+78
-81
lines changed

src/config.rs

Lines changed: 62 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@ use std::vec::Vec;
66
use std::process::exit;
77
use std::fs;
88
use serde::{Deserialize, Serialize};
9+
use serde_json::Value;
910
use colored::Colorize;
1011
use std::collections::HashMap;
1112

12-
type Other = HashMap<String, serde_json::Value>;
13-
1413
#[derive(Serialize, Deserialize, Clone)]
1514
#[serde(rename_all = "kebab-case")]
1615
pub struct Installation {
1716
pub path: PathBuf,
1817
pub executable: String,
1918
#[serde(flatten)]
20-
other: Option<Other>,
19+
other: HashMap<String, Value>,
2120
}
2221

2322
#[derive(Serialize, Deserialize, Clone)]
@@ -28,17 +27,9 @@ pub struct Config {
2827
pub installations: Option<Vec<Installation>>,
2928
pub default_developer: Option<String>,
3029
#[serde(flatten)]
31-
other: Option<Other>,
30+
other: HashMap<String, Value>,
3231
}
3332

34-
static mut CONFIG: Config = Config {
35-
default_installation: 0,
36-
working_installation: None,
37-
installations: None,
38-
default_developer: None,
39-
other: None,
40-
};
41-
4233
impl Config {
4334
pub fn data_dir() -> PathBuf {
4435
// get data dir per-platform
@@ -56,67 +47,73 @@ impl Config {
5647
data_dir
5748
}
5849

59-
pub fn init() {
60-
unsafe {
61-
let config_json = Config::data_dir().join("config.json");
62-
if !config_json.exists() {
63-
println!(
64-
"{}{}{}{}",
65-
"WARNING: It seems you don't have Geode installed! \
66-
Please install Geode first using the official installer \
67-
(".yellow(),
68-
"https://github.com/geode-sdk/installer/releases/latest".cyan(),
69-
")".yellow(),
70-
"\nYou may still use the CLI, but be warned that certain \
71-
operations will cause crashes.\n".purple()
72-
);
73-
fs::create_dir_all(Config::data_dir()).unwrap();
74-
return;
75-
}
76-
CONFIG = match serde_json::from_str(
77-
&fs::read_to_string(&config_json).unwrap()
78-
) {
79-
Ok(p) => p,
80-
Err(e) => {
81-
println!("Unable to parse config.json: {}", e);
82-
exit(1);
83-
}
84-
};
85-
if CONFIG.installations.is_none() {
86-
println!(
87-
"{}{}{}{}",
88-
"WARNING: It seems you don't have any installations of Geode! \
89-
Please install Geode first using the official installer \
90-
(".yellow(),
91-
"https://github.com/geode-sdk/installer/releases/latest".cyan(),
92-
")".yellow(),
93-
"\nYou may still use the CLI, but be warned that certain \
94-
operations will cause crashes.\n".purple()
95-
);
96-
return;
97-
}
98-
if CONFIG.working_installation.is_none() {
99-
CONFIG.working_installation = Some(CONFIG.default_installation);
50+
pub fn init(&mut self) {
51+
let config_json = Config::data_dir().join("config.json");
52+
if !config_json.exists() {
53+
println!(
54+
"{}{}{}{}",
55+
"WARNING: It seems you don't have Geode installed! \
56+
Please install Geode first using the official installer \
57+
(".yellow(),
58+
"https://github.com/geode-sdk/installer/releases/latest".cyan(),
59+
")".yellow(),
60+
"\nYou may still use the CLI, but be warned that certain \
61+
operations will cause crashes.\n".purple()
62+
);
63+
fs::create_dir_all(Config::data_dir()).unwrap();
64+
return;
65+
}
66+
*self = match serde_json::from_str(
67+
&fs::read_to_string(&config_json).unwrap()
68+
) {
69+
Ok(p) => p,
70+
Err(e) => {
71+
println!("Unable to parse config.json: {}", e);
72+
exit(1);
10073
}
74+
};
75+
if self.installations.is_none() {
76+
println!(
77+
"{}{}{}{}",
78+
"WARNING: It seems you don't have any installations of Geode! \
79+
Please install Geode first using the official installer \
80+
(".yellow(),
81+
"https://github.com/geode-sdk/installer/releases/latest".cyan(),
82+
")".yellow(),
83+
"\nYou may still use the CLI, but be warned that certain \
84+
operations will cause crashes.\n".purple()
85+
);
86+
return;
87+
}
88+
if self.working_installation.is_none() {
89+
self.working_installation = Some(
90+
self.default_installation
91+
);
10192
}
10293
}
10394

104-
pub fn get() -> &'static mut Config {
105-
unsafe { &mut CONFIG }
95+
pub fn new() -> Config {
96+
let mut config = Config {
97+
default_installation: 0,
98+
working_installation: None,
99+
installations: None,
100+
default_developer: None,
101+
other: HashMap::new(),
102+
};
103+
config.init();
104+
config
106105
}
107106

108-
pub fn save() {
109-
unsafe {
110-
fs::write(
111-
Config::data_dir().join("config.json"),
112-
serde_json::to_string(&CONFIG).unwrap()
113-
).unwrap();
114-
}
107+
pub fn save(&mut self) {
108+
fs::write(
109+
Config::data_dir().join("config.json"),
110+
serde_json::to_string(self).unwrap()
111+
).unwrap();
115112
}
116113

117-
pub fn work_inst() -> &'static Installation {
118-
&Config::get().installations.as_ref().unwrap()[
119-
Config::get().working_installation.unwrap()
114+
pub fn work_inst(&mut self) -> &Installation {
115+
&self.installations.as_ref().unwrap()[
116+
self.working_installation.unwrap()
120117
]
121118
}
122119
}

src/main.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,13 @@ fn main() {
179179
Ok(_) => {},
180180
Err(e) => println!("Unable to enable ANSI support: {}", e)
181181
}
182-
183-
Config::init();
182+
183+
let mut config = Config::new();
184184

185185
let args = Cli::parse();
186186

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

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

201201
if install {
202202
if let Err(e) = package::install_geode_file(
203-
&Config::work_inst().path,
203+
&config.work_inst().path,
204204
&out_file
205205
) {
206206
print_error!("Error installing package: {}", e);
@@ -222,18 +222,18 @@ fn main() {
222222
Commands::Config { cwi, dev } => {
223223
let mut some_set = false;
224224
if let Some(ver) = cwi {
225-
if ver >= Config::get().installations.as_ref().unwrap().len() {
225+
if ver >= config.installations.as_ref().unwrap().len() {
226226
print_error!(
227227
"Provided index is higher than your \
228228
amount of installations!"
229229
);
230230
}
231-
Config::get().working_installation = cwi;
231+
config.working_installation = cwi;
232232
some_set = true;
233233
println!("Updated working installation");
234234
}
235235
if dev.is_some() {
236-
Config::get().default_developer = dev;
236+
config.default_developer = dev;
237237
some_set = true;
238238
println!("Updated default developer");
239239
}
@@ -251,13 +251,13 @@ fn main() {
251251
GEODE_CLI_VERSION.to_string().yellow(),
252252
unsafe {link::geode_target_version()}.to_string().red(),
253253
std::env::current_exe().unwrap().to_str().unwrap().cyan(),
254-
match Config::get().default_developer.as_ref() {
254+
match config.default_developer.as_ref() {
255255
Some(s) => s,
256256
None => "<none>"
257257
}.purple(),
258258
Config::data_dir().to_str().unwrap().cyan(),
259-
Config::get().working_installation.unwrap().to_string().red(),
260-
Config::work_inst().path.to_str().unwrap().cyan(),
259+
config.working_installation.unwrap().to_string().red(),
260+
config.work_inst().path.to_str().unwrap().cyan(),
261261
);
262262
}
263263
},
@@ -321,11 +321,11 @@ fn main() {
321321

322322
Commands::Install { path } => {
323323
package::install_geode_file(
324-
&Config::work_inst().path,
324+
&config.work_inst().path,
325325
&path
326326
).unwrap();
327327
}
328328
}
329329

330-
Config::save();
330+
config.save();
331331
}

src/template.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fn ask_value(prompt: &str, default: &str, required: bool) -> String {
9494
}
9595
}
9696

97-
pub fn build_template(name: Option<String>, location: Option<PathBuf>) {
97+
pub fn build_template(config: &mut Config, name: Option<String>, location: Option<PathBuf>) {
9898
let name = match name {
9999
Some(s) => ask_value("Name", s.as_str(), true),
100100
None => ask_value("Name", "", true)
@@ -109,19 +109,19 @@ pub fn build_template(name: Option<String>, location: Option<PathBuf>) {
109109
let version = ask_value("Version", "v1.0.0", true);
110110
let developer = ask_value(
111111
"Developer",
112-
Config::get().default_developer.as_ref().unwrap_or(&String::new()),
112+
config.default_developer.as_ref().unwrap_or(&String::new()),
113113
true
114114
);
115115

116-
if Config::get().default_developer.is_none() {
116+
if config.default_developer.is_none() {
117117
println!("{}{}{}\n{}{}",
118118
"Using ".bright_cyan(),
119119
developer,
120120
" as default developer name for future projects.".bright_cyan(),
121121
"If this is undesirable, use ".bright_cyan(),
122122
"`geode config --dev <NAME>`".bright_yellow()
123123
);
124-
Config::get().default_developer = Some(developer.clone());
124+
config.default_developer = Some(developer.clone());
125125
}
126126

127127
let description = ask_value("Description", "", false);

0 commit comments

Comments
 (0)