Skip to content
Snippets Groups Projects
Commit 1f4bbece authored by bjorn3's avatar bjorn3
Browse files

Introduce Config::from_file for parsing the config toml

parent 5cdec468
No related branches found
No related tags found
1 merge request!29Support including other config files from a config file
...@@ -3,13 +3,13 @@ extern crate redox_installer; ...@@ -3,13 +3,13 @@ extern crate redox_installer;
extern crate serde; extern crate serde;
extern crate toml; extern crate toml;
use std::io::{Read, Write}; use std::io::Write;
use std::path::Path; use std::path::Path;
use std::{env, fs, io, process}; use std::{env, fs, io, process};
use arg_parser::ArgParser; use arg_parser::ArgParser;
use redox_installer::PackageConfig; use redox_installer::{Config, PackageConfig};
fn main() { fn main() {
let stderr = io::stderr(); let stderr = io::stderr();
...@@ -27,24 +27,11 @@ fn main() { ...@@ -27,24 +27,11 @@ fn main() {
// If not set on the command line or the filesystem config, then build packages from source. // If not set on the command line or the filesystem config, then build packages from source.
let repo_binary = parser.found("repo-binary"); let repo_binary = parser.found("repo-binary");
let mut config_data = String::new();
let mut config = if let Some(path) = parser.get_opt("config") { let mut config = if let Some(path) = parser.get_opt("config") {
match fs::File::open(&path) { match Config::from_file(Path::new(&path)) {
Ok(mut config_file) => match config_file.read_to_string(&mut config_data) { Ok(config) => config,
Ok(_) => match toml::from_str(&config_data) {
Ok(config) => config,
Err(err) => {
writeln!(stderr, "installer: {}: failed to decode: {}", path, err).unwrap();
process::exit(1);
}
},
Err(err) => {
writeln!(stderr, "installer: {}: failed to read: {}", path, err).unwrap();
process::exit(1);
}
},
Err(err) => { Err(err) => {
writeln!(stderr, "installer: {}: failed to open: {}", path, err).unwrap(); writeln!(stderr, "installer: {err}").unwrap();
process::exit(1); process::exit(1);
} }
} }
......
...@@ -309,24 +309,7 @@ fn main() { ...@@ -309,24 +309,7 @@ fn main() {
efi_partition_size: None, efi_partition_size: None,
}; };
let res = with_whole_disk(&disk_path, &disk_option, |mount_path| -> Result<(), failure::Error> { let res = with_whole_disk(&disk_path, &disk_option, |mount_path| -> Result<(), failure::Error> {
let mut config: Config = { let mut config: Config = Config::from_file(&root_path.join("filesystem.toml"))?;
let path = root_path.join("filesystem.toml");
match fs::read_to_string(&path) {
Ok(config_data) => {
match toml::from_str(&config_data) {
Ok(config) => {
config
},
Err(err) => {
return Err(format_err!("{}: failed to decode: {}", path.display(), err));
}
}
},
Err(err) => {
return Err(format_err!("{}: failed to read: {}", path.display(), err));
}
}
};
// Copy filesystem.toml, which is not packaged // Copy filesystem.toml, which is not packaged
let mut files = vec![ let mut files = vec![
......
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::fs;
use std::path::Path;
pub mod general;
pub mod file; pub mod file;
pub mod general;
pub mod package; pub mod package;
pub mod user; pub mod user;
...@@ -15,3 +17,20 @@ pub struct Config { ...@@ -15,3 +17,20 @@ pub struct Config {
#[serde(default)] #[serde(default)]
pub users: BTreeMap<String, user::UserConfig>, pub users: BTreeMap<String, user::UserConfig>,
} }
impl Config {
pub fn from_file(path: &Path) -> Result<Self, failure::Error> {
let config = match fs::read_to_string(&path) {
Ok(config_data) => match toml::from_str(&config_data) {
Ok(config) => config,
Err(err) => {
return Err(format_err!("{}: failed to decode: {}", path.display(), err));
}
},
Err(err) => {
return Err(format_err!("{}: failed to read: {}", path.display(), err));
}
};
Ok(config)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment