Merge pull request 'Fix peach-config manifest' (#103) from fix-manifest into main

Reviewed-on: #103
This commit is contained in:
notplants 2022-04-20 18:27:43 +00:00
commit e2ac5de6e4
3 changed files with 18 additions and 26 deletions

2
Cargo.lock generated
View File

@ -2381,7 +2381,7 @@ dependencies = [
[[package]] [[package]]
name = "peach-config" name = "peach-config"
version = "0.1.21" version = "0.1.23"
dependencies = [ dependencies = [
"clap", "clap",
"env_logger 0.6.2", "env_logger 0.6.2",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "peach-config" name = "peach-config"
version = "0.1.21" version = "0.1.23"
authors = ["Andrew Reid <gnomad@cryptolab.net>", "Max Fowler <max@mfowler.info>"] authors = ["Andrew Reid <gnomad@cryptolab.net>", "Max Fowler <max@mfowler.info>"]
edition = "2018" edition = "2018"
description = "Command line tool for installing, updating and configuring PeachCloud" description = "Command line tool for installing, updating and configuring PeachCloud"

View File

@ -1,40 +1,32 @@
use regex::Regex;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use snafu::ResultExt; use snafu::ResultExt;
use std::collections::HashMap; use std::collections::HashMap;
use std::fs; use std::fs;
use crate::constants::HARDWARE_CONFIG_FILE; use crate::constants::{HARDWARE_CONFIG_FILE, SERVICES};
use crate::error::{FileReadError, FileWriteError, PeachConfigError}; use crate::error::{FileReadError, FileWriteError, PeachConfigError};
use crate::utils::get_output; use crate::utils::get_output;
use crate::RtcOption; use crate::RtcOption;
/// Helper function which returns the version of a package currently installed,
/// as an Ok(String) if found, and as an Err if not found
pub fn get_package_version_number(package: &str) -> Result<String, PeachConfigError> {
let version = get_output(&["dpkg-query", "--showformat=${Version}", "--show", package])?;
Ok(version)
}
/// Returns a HashMap<String, String> of all the peach-packages which are currently installed /// Returns a HashMap<String, String> of all the peach-packages which are currently installed
/// mapped to their version number e.g. { "peach-probe": "1.2.0", "peach-network": "1.4.0" } /// mapped to their version number e.g. { "peach-probe": "1.2.0", "peach-network": "1.4.0" }
pub fn get_currently_installed_microservices() -> Result<HashMap<String, String>, PeachConfigError> pub fn get_currently_installed_microservices() -> Result<HashMap<String, String>, PeachConfigError>
{ {
// gets a list of all packages currently installed with dpkg // gets a list of all packages currently installed with dpkg-query
let packages = get_output(&["dpkg", "-l"])?; let peach_packages: HashMap<String, String> = SERVICES
.iter()
// this regex matches packages which contain the word peach in them .filter_map(|service| {
// and has two match groups let version = get_package_version_number(service);
// 1. the first match group gets the package name match version {
// 2. the second match group gets the version number of the package Ok(v) => Some((service.to_string(), v)),
let re: Regex = Regex::new(r"\S+\s+(\S*peach\S+)\s+(\S+).*\n").unwrap(); Err(_) => None,
// the following iterator, iterates through the captures matched via the regex
// and for each capture, creates a value in the hash map,
// which maps the name of the package, to its version number
// e.g. { "peach-probe": "1.2.0", "peach-network": "1.4.0" }
let peach_packages: HashMap<String, String> = re
.captures_iter(&packages)
.filter_map(|cap| {
let groups = (cap.get(1), cap.get(2));
match groups {
(Some(package), Some(version)) => {
Some((package.as_str().to_string(), version.as_str().to_string()))
}
_ => None,
} }
}) })
.collect(); .collect();