@@ -6,18 +6,17 @@ use std::vec::Vec;
66use std:: process:: exit;
77use std:: fs;
88use serde:: { Deserialize , Serialize } ;
9+ use serde_json:: Value ;
910use colored:: Colorize ;
1011use std:: collections:: HashMap ;
1112
12- type Other = HashMap < String , serde_json:: Value > ;
13-
1413#[ derive( Serialize , Deserialize , Clone ) ]
1514#[ serde( rename_all = "kebab-case" ) ]
1615pub 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-
4233impl 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- "\n You 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- "\n You 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+ "\n You 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+ "\n You 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}
0 commit comments