Skip to content

Commit

Permalink
fix: winget commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ripytide committed Nov 14, 2024
1 parent 5c55b1f commit 415fbc4
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions src/backends/winget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use std::io::Read;

use color_eyre::Result;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::cmd::{command_found, run_command, run_command_for_stdout};
use crate::cmd::{command_found, run_command};
use crate::prelude::*;

#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, derive_more::Display)]
Expand All @@ -29,29 +30,33 @@ impl Backend for WinGet {

fn query_installed_packages(_: &Config) -> Result<BTreeMap<String, Self::QueryInfo>> {
if !command_found("winget") {
log::warn!("winget command not found");
return Ok(BTreeMap::new());
}

let mut tempfile = tempfile::NamedTempFile::new()?;

//TODO: refactor if https://github.com/microsoft/winget-cli/issues/184 or https://github.com/microsoft/winget-cli/issues/4267 are ever fixed
let explicit = run_command(
let mut tempfile = tempfile::NamedTempFile::new()?;
run_command(
["winget", "export", tempfile.path().to_str().unwrap()],
Perms::Same,
)?;

let mut string = String::new();
tempfile.read_to_string(&mut string)?;
dbg!(string);

return Ok(BTreeMap::new());


// Ok(explicit
// .lines()
// .map(|x| (x.to_string(), WinGetQueryInfo {}))
// .collect())
let mut export = String::new();
tempfile.read_to_string(&mut export)?;

let export: Value = serde_json::from_str(&export)?;

Ok(export["Sources"]
.as_array()
.unwrap()
.iter()
.flat_map(|x| x["Packages"].as_array().unwrap())
.map(|x| {
(
x["PackageIdentifier"].as_str().unwrap().to_string(),
WinGetQueryInfo {},
)
})
.collect())
}

fn install_packages(
Expand All @@ -61,7 +66,7 @@ impl Backend for WinGet {
) -> Result<()> {
if !packages.is_empty() {
run_command(
["winget", "install", "--id", "--exact"]
["winget", "install"]
.into_iter()
.chain(packages.keys().map(String::as_str)),
Perms::Same,
Expand All @@ -74,7 +79,7 @@ impl Backend for WinGet {
fn remove_packages(packages: &BTreeSet<String>, _: bool, _: &Config) -> Result<()> {
if !packages.is_empty() {
run_command(
["winget", "uninstall", "--id", "--exact"]
["winget", "uninstall"]
.into_iter()
.chain(packages.iter().map(String::as_str)),
Perms::Same,
Expand Down

0 comments on commit 415fbc4

Please sign in to comment.