|
|
@@ -1,43 +1,39 @@ |
|
1 |
+use oxwm::errors::ConfigError; |
|
2 |
+use oxwm::errors::MainError; |
|
3 |
+use std::path::Path; |
| 1 |
4 |
use std::path::PathBuf; |
| 2 |
5 |
|
| 3 |
|
-fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 4 |
|
- let arguments: Vec<String> = std::env::args().collect(); |
|
6 |
+static CONFIG_FILE: &str = "config.lua"; |
|
7 |
+static TEMPLATE: &str = include_str!("../../templates/config.lua"); |
| 5 |
8 |
|
| 6 |
|
- let mut custom_config_path: Option<PathBuf> = None; |
|
9 |
+enum Args { |
|
10 |
+ Exit, |
|
11 |
+ Arguments(Vec<String>), |
|
12 |
+ Error(MainError), |
|
13 |
+} |
| 7 |
14 |
|
| 8 |
|
- match arguments.get(1).map(|string| string.as_str()) { |
| 9 |
|
- Some("--version") => { |
| 10 |
|
- println!("oxwm {}", env!("CARGO_PKG_VERSION")); |
| 11 |
|
- return Ok(()); |
| 12 |
|
- } |
| 13 |
|
- Some("--help") => { |
| 14 |
|
- print_help(); |
| 15 |
|
- return Ok(()); |
| 16 |
|
- } |
| 17 |
|
- Some("--init") => { |
| 18 |
|
- init_config()?; |
| 19 |
|
- return Ok(()); |
| 20 |
|
- } |
| 21 |
|
- Some("--config") => { |
| 22 |
|
- if let Some(path) = arguments.get(2) { |
| 23 |
|
- custom_config_path = Some(PathBuf::from(path)); |
| 24 |
|
- } else { |
| 25 |
|
- eprintln!("Error: --config requires a path argument"); |
| 26 |
|
- std::process::exit(1); |
| 27 |
|
- } |
| 28 |
|
- } |
| 29 |
|
- _ => {} |
| 30 |
|
- } |
|
15 |
+fn main() -> Result<(), MainError> { |
|
16 |
+ let arguments = match process_args() { |
|
17 |
+ Args::Exit => return Ok(()), |
|
18 |
+ Args::Arguments(v) => v, |
|
19 |
+ Args::Error(e) => return Err(e), |
|
20 |
+ }; |
| 31 |
21 |
|
| 32 |
|
- let (config, config_error) = load_config(custom_config_path)?; |
|
22 |
+ let (config, config_warning) = load_config(arguments.get(2))?; |
| 33 |
23 |
|
| 34 |
|
- let mut window_manager = oxwm::window_manager::WindowManager::new(config)?; |
|
24 |
+ let mut window_manager = match oxwm::window_manager::WindowManager::new(config) { |
|
25 |
+ Ok(wm) => wm, |
|
26 |
+ Err(e) => return Err(MainError::CouldNotStartWm(e)), |
|
27 |
+ }; |
| 35 |
28 |
|
| 36 |
|
- if let Some(error) = config_error { |
| 37 |
|
- window_manager.show_startup_config_error(&error); |
|
29 |
+ if let Some(warning) = config_warning { |
|
30 |
+ window_manager.show_startup_config_error(warning); |
| 38 |
31 |
} |
| 39 |
32 |
|
| 40 |
|
- let should_restart = window_manager.run()?; |
|
33 |
+ let should_restart = match window_manager.run() { |
|
34 |
+ Ok(sr) => sr, |
|
35 |
+ Err(e) => return Err(MainError::BadRestartStatus(e)), |
|
36 |
+ }; |
| 41 |
37 |
|
| 42 |
38 |
drop(window_manager); |
| 43 |
39 |
|
|
|
@@ -46,70 +42,57 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 46 |
42 |
let error = std::process::Command::new(&arguments[0]) |
| 47 |
43 |
.args(&arguments[1..]) |
| 48 |
44 |
.exec(); |
| 49 |
|
- eprintln!("Failed to restart: {}", error); |
|
45 |
+ eprintln!("Error: Failed to restart: {}", error); |
| 50 |
46 |
} |
| 51 |
|
- |
| 52 |
47 |
Ok(()) |
| 53 |
48 |
} |
| 54 |
49 |
|
| 55 |
50 |
fn load_config( |
| 56 |
|
- custom_path: Option<PathBuf>, |
| 57 |
|
-) -> Result<(oxwm::Config, Option<String>), Box<dyn std::error::Error>> { |
| 58 |
|
- let config_path = if let Some(path) = custom_path { |
| 59 |
|
- path |
| 60 |
|
- } else { |
| 61 |
|
- let config_directory = get_config_path(); |
| 62 |
|
- let lua_path = config_directory.join("config.lua"); |
| 63 |
|
- |
| 64 |
|
- if !lua_path.exists() { |
| 65 |
|
- let ron_path = config_directory.join("config.ron"); |
| 66 |
|
- let had_ron_config = ron_path.exists(); |
| 67 |
|
- |
| 68 |
|
- println!("No config found at {:?}", config_directory); |
| 69 |
|
- println!("Creating default Lua config..."); |
| 70 |
|
- init_config()?; |
| 71 |
|
- |
| 72 |
|
- if had_ron_config { |
| 73 |
|
- println!("\n NOTICE: OXWM has migrated to Lua configuration."); |
| 74 |
|
- println!(" Your old config.ron has been preserved, but is no longer used."); |
| 75 |
|
- println!(" Your settings have been reset to defaults."); |
| 76 |
|
- println!(" Please manually port your configuration to the new Lua format."); |
| 77 |
|
- println!(" See the new config.lua template for examples.\n"); |
| 78 |
|
- } |
|
51 |
+ config_path: Option<&String>, |
|
52 |
+) -> Result<(oxwm::Config, Option<ConfigError>), MainError> { |
|
53 |
+ let path = match config_path { |
|
54 |
+ None => { |
|
55 |
+ let config_dir = get_config_path()?; |
|
56 |
+ let config_path = config_dir.join(CONFIG_FILE); |
|
57 |
+ check_convert(&config_path)?; |
|
58 |
+ config_path |
| 79 |
59 |
} |
| 80 |
|
- |
| 81 |
|
- lua_path |
|
60 |
+ Some(p) => PathBuf::from(p), |
| 82 |
61 |
}; |
| 83 |
62 |
|
| 84 |
|
- let config_string = std::fs::read_to_string(&config_path) |
| 85 |
|
- .map_err(|error| format!("Failed to read config file: {}", error))?; |
|
63 |
+ let config_string = match std::fs::read_to_string(&path) { |
|
64 |
+ Ok(c) => c, |
|
65 |
+ Err(e) => return Err(MainError::FailedReadConfig(e)), |
|
66 |
+ }; |
| 86 |
67 |
|
| 87 |
|
- let config_directory = config_path.parent(); |
|
68 |
+ let config_directory = path.parent(); |
| 88 |
69 |
|
| 89 |
|
- let (mut config, config_error) = |
|
70 |
+ let (mut config, config_warning) = |
| 90 |
71 |
match oxwm::config::parse_lua_config(&config_string, config_directory) { |
| 91 |
72 |
Ok(config) => (config, None), |
| 92 |
|
- Err(error) => { |
| 93 |
|
- let template = include_str!("../../templates/config.lua"); |
| 94 |
|
- let config = oxwm::config::parse_lua_config(template, None).map_err(|error| { |
| 95 |
|
- format!("Failed to parse default template config: {}", error) |
| 96 |
|
- })?; |
| 97 |
|
- (config, Some(format!("{}", error))) |
|
73 |
+ Err(warning) => { |
|
74 |
+ let config = match oxwm::config::parse_lua_config(TEMPLATE, None) { |
|
75 |
+ Ok(c) => c, |
|
76 |
+ Err(e) => return Err(MainError::FailedReadConfigTemplate(e)), |
|
77 |
+ }; |
|
78 |
+ (config, Some(warning)) |
| 98 |
79 |
} |
| 99 |
80 |
}; |
| 100 |
|
- |
| 101 |
|
- config.path = Some(config_path); |
| 102 |
|
- |
| 103 |
|
- Ok((config, config_error)) |
|
81 |
+ config.path = Some(path); |
|
82 |
+ Ok((config, config_warning)) |
| 104 |
83 |
} |
| 105 |
84 |
|
| 106 |
|
-fn init_config() -> Result<(), Box<dyn std::error::Error>> { |
| 107 |
|
- let config_directory = get_config_path(); |
| 108 |
|
- std::fs::create_dir_all(&config_directory)?; |
|
85 |
+fn init_config() -> Result<(), MainError> { |
|
86 |
+ let config_directory = get_config_path()?; |
|
87 |
+ if let Err(e) = std::fs::create_dir_all(&config_directory) { |
|
88 |
+ return Err(MainError::CouldNotCreateConfigDir(e)); |
|
89 |
+ } |
| 109 |
90 |
|
| 110 |
|
- let config_template = include_str!("../../templates/config.lua"); |
| 111 |
|
- let config_path = config_directory.join("config.lua"); |
| 112 |
|
- std::fs::write(&config_path, config_template)?; |
|
91 |
+ let config_template = TEMPLATE; |
|
92 |
+ let config_path = config_directory.join(CONFIG_FILE); |
|
93 |
+ if let Err(e) = std::fs::write(&config_path, config_template) { |
|
94 |
+ return Err(MainError::CouldNotWriteConfig(e)); |
|
95 |
+ } |
| 113 |
96 |
|
| 114 |
97 |
println!("✓ Config created at {:?}", config_path); |
| 115 |
98 |
println!(" Edit the file and reload with Mod+Shift+R"); |
|
|
@@ -118,10 +101,11 @@ fn init_config() -> Result<(), Box<dyn std::error::Error>> { |
| 118 |
101 |
Ok(()) |
| 119 |
102 |
} |
| 120 |
103 |
|
| 121 |
|
-fn get_config_path() -> PathBuf { |
| 122 |
|
- dirs::config_dir() |
| 123 |
|
- .expect("Could not find config directory") |
| 124 |
|
- .join("oxwm") |
|
104 |
+fn get_config_path() -> Result<PathBuf, MainError> { |
|
105 |
+ match dirs::config_dir() { |
|
106 |
+ Some(p) => Ok(p.join("oxwm")), |
|
107 |
+ None => Err(MainError::NoConfigDir), |
|
108 |
+ } |
| 125 |
109 |
} |
| 126 |
110 |
|
| 127 |
111 |
fn print_help() { |
|
|
@@ -142,3 +126,77 @@ fn print_help() { |
| 142 |
126 |
println!(" Run 'oxwm --init' to create a config file"); |
| 143 |
127 |
println!(" Or just start oxwm and it will create one automatically\n"); |
| 144 |
128 |
} |
|
129 |
+ |
|
130 |
+fn process_args() -> Args { |
|
131 |
+ let mut args = std::env::args(); |
|
132 |
+ let name = match args.next() { |
|
133 |
+ Some(n) => n, |
|
134 |
+ None => return Args::Error(MainError::NoProgramName), |
|
135 |
+ }; |
|
136 |
+ let switch = args.next(); |
|
137 |
+ let path = args.next(); |
|
138 |
+ |
|
139 |
+ let switch = match switch { |
|
140 |
+ Some(s) => s, |
|
141 |
+ None => return Args::Arguments(vec![name]), |
|
142 |
+ }; |
|
143 |
+ |
|
144 |
+ match switch.as_str() { |
|
145 |
+ "--version" => { |
|
146 |
+ println!("{name} {}", env!("CARGO_PKG_VERSION")); |
|
147 |
+ Args::Exit |
|
148 |
+ } |
|
149 |
+ "--help" => { |
|
150 |
+ print_help(); |
|
151 |
+ Args::Exit |
|
152 |
+ } |
|
153 |
+ "--init" => match init_config() { |
|
154 |
+ Ok(_) => Args::Exit, |
|
155 |
+ Err(e) => Args::Error(e), |
|
156 |
+ }, |
|
157 |
+ "--config" => match check_custom_config(path) { |
|
158 |
+ Ok(p) => Args::Arguments(vec![name, switch, p]), |
|
159 |
+ Err(e) => Args::Error(e), |
|
160 |
+ }, |
|
161 |
+ _ => Args::Error(MainError::InvalidArguments), |
|
162 |
+ } |
|
163 |
+} |
|
164 |
+ |
|
165 |
+fn check_custom_config(path: Option<String>) -> Result<String, MainError> { |
|
166 |
+ let path = match path { |
|
167 |
+ Some(p) => p, |
|
168 |
+ None => { |
|
169 |
+ return Err(MainError::NoConfigPath); |
|
170 |
+ } |
|
171 |
+ }; |
|
172 |
+ |
|
173 |
+ match std::fs::exists(&path) { |
|
174 |
+ Ok(b) => match b { |
|
175 |
+ true => Ok(path), |
|
176 |
+ false => Err(MainError::BadConfigPath), |
|
177 |
+ }, |
|
178 |
+ Err(e) => Err(MainError::FailedCheckExist(e)), |
|
179 |
+ } |
|
180 |
+} |
|
181 |
+ |
|
182 |
+fn check_convert(path: &Path) -> Result<(), MainError> { |
|
183 |
+ let config_directory = get_config_path()?; |
|
184 |
+ |
|
185 |
+ if !path.exists() { |
|
186 |
+ let ron_path = config_directory.join("config.ron"); |
|
187 |
+ let had_ron_config = ron_path.exists(); |
|
188 |
+ |
|
189 |
+ println!("No config found at {:?}", config_directory); |
|
190 |
+ println!("Creating default Lua config..."); |
|
191 |
+ init_config()?; |
|
192 |
+ |
|
193 |
+ if had_ron_config { |
|
194 |
+ println!("\n NOTICE: OXWM has migrated to Lua configuration."); |
|
195 |
+ println!(" Your old config.ron has been preserved, but is no longer used."); |
|
196 |
+ println!(" Your settings have been reset to defaults."); |
|
197 |
+ println!(" Please manually port your configuration to the new Lua format."); |
|
198 |
+ println!(" See the new config.lua template for examples.\n"); |
|
199 |
+ } |
|
200 |
+ } |
|
201 |
+ Ok(()) |
|
202 |
+} |