Skip to content

Commit

Permalink
Offer a build feature to support legacy quickemu bash configuration f…
Browse files Browse the repository at this point in the history
…iles
  • Loading branch information
lj3954 committed May 23, 2024
1 parent 4aec7cf commit a053ff4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ default = [

check_smartcard = []
get_qemu_ver = []
support_bash_conf = []
37 changes: 20 additions & 17 deletions src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,9 @@ fn snapshot_command(qemu_img: &Path, arg: &str, tag: &str, disk_img: &Path) -> R
}
}

pub fn migrate_config(config: Vec<String>) -> Result<String> {
if config.len() != 2 {
bail!("Invalid arguments for migrate-config. Usage: `quickemu-rs --migrate-config <config.conf> <config.toml>`")
}
let (legacy_conf, toml_conf) = (PathBuf::from(&config[0]), PathBuf::from(&config[1]));
if legacy_conf.extension().unwrap_or_default() != "conf" || !legacy_conf.exists() {
bail!("Invalid legacy config file. Please provide a valid .conf file.");
} else if toml_conf.extension().unwrap_or_default() != "toml" {
bail!("The configuration file must be migrated to a .toml file.");
} else if toml_conf.exists() {
bail!("The target configuration file already exists. Please delete it or provide a new file name.");
}

let conf = read_to_string(&legacy_conf).map_err(|e| anyhow!("Could not read legacy configuration file {}: {}", legacy_conf.display(), e))?;
pub fn read_legacy_conf(config: &Path) -> Result<ConfigFile> {
let conf = read_to_string(config).map_err(|e| anyhow!("Could not read legacy configuration file {}: {}", config.display(), e))?;
log::debug!("Legacy configuration: {}", conf);

let mut conf: HashMap<String, String> = conf.lines().filter_map(|line| {
log::debug!("Parsing line: {}", line);
if line.starts_with('#') || !line.contains('=') {
Expand Down Expand Up @@ -142,12 +129,28 @@ pub fn migrate_config(config: Vec<String>) -> Result<String> {
log::warn!("Ignoring values: {:?}", conf);
}

let config = ConfigFile {
Ok(ConfigFile {
guest_os, arch, boot_type, cpu_cores, display, disk_images, accelerated, image_files, network, port_forwards, public_dir, ram, tpm, keyboard, keyboard_layout, monitor, serial, soundcard, mouse, resolution, usb_controller, spice_port, ssh_port, usb_devices
};
})
}

pub fn migrate_config(config: Vec<String>) -> Result<String> {
if config.len() != 2 {
bail!("Invalid arguments for migrate-config. Usage: `quickemu-rs --migrate-config <config.conf> <config.toml>`")
}
let (legacy_conf, toml_conf) = (PathBuf::from(&config[0]), PathBuf::from(&config[1]));
if legacy_conf.extension().unwrap_or_default() != "conf" || !legacy_conf.exists() {
bail!("Invalid legacy config file. Please provide a valid .conf file.");
} else if toml_conf.extension().unwrap_or_default() != "toml" {
bail!("The configuration file must be migrated to a .toml file.");
} else if toml_conf.exists() {
bail!("The target configuration file already exists. Please delete it or provide a new file name.");
}

log::debug!("Migrated configuration: {:?}", config);
let executable = "#!".to_string() + &std::env::current_exe().unwrap_or_default().to_string_lossy() + " --vm\n";
let config = read_legacy_conf(&legacy_conf)?;

let toml = executable + &toml::to_string_pretty(&config)
.map_err(|e| anyhow!("Could not serialize configuration to TOML: {}", e))?;
log::debug!("TOML: {}", toml);
Expand Down
14 changes: 10 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,22 @@ fn parse_conf(conf_file: Vec<String>) -> Result<(String, ConfigFile)> {
}
},
None => {
let pkg = env!("CARGO_PKG_NAME");
match conf_file.into_iter().find_map(|arg| {
let arg = if arg.ends_with(".conf") { arg } else { arg + ".conf" };
if PathBuf::from(&arg).exists() {
Some(arg)
let conf_path = PathBuf::from(&arg);
if conf_path.exists() {
Some((arg, conf_path))
} else {
None
}
}) {
Some(conf) => bail!("{} no longer supports '.conf' configuration files.\nPlease convert your configuration file to the TOML format using `{} --migrate-config {} {}`.", pkg, pkg, conf, conf.replace(".conf", ".toml")),
#[cfg(not(feature = "support_bash_conf"))]
Some((conf, _)) => {
let pkg = env!("CARGO_PKG_NAME");
bail!("{} no longer supports '.conf' configuration files.\nPlease convert your configuration file to the TOML format using `{} --migrate-config {} {}`.", pkg, pkg, conf, conf.replace(".conf", ".toml"))
},
#[cfg(feature = "support_bash_conf")]
Some((arg, conf)) => return Ok((arg, actions::read_legacy_conf(&conf)?)),
None => bail!("You are required to input a valid configuration file."),
}
},
Expand Down

0 comments on commit a053ff4

Please sign in to comment.