Skip to content

Commit

Permalink
some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
gerdreiss committed Oct 25, 2021
1 parent 6b8213c commit 5aa555e
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 290 deletions.
1 change: 0 additions & 1 deletion api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod local;
pub mod model;
pub mod remote;
mod util;
58 changes: 37 additions & 21 deletions api/src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,71 @@ use std::fs;
use std::io::Error;
use std::io::ErrorKind;

use crate::model::*;
#[derive(Debug, Clone)]
pub struct LocalCandidate {
binary_name: String,
versions: HashMap<String, bool>,
}

impl LocalCandidate {
pub fn new(binary_name: String, versions: HashMap<String, bool>) -> Self {
Self {
binary_name,
versions,
}
}
pub fn binary_name(&self) -> &String {
&self.binary_name
}
pub fn versions(&self) -> &HashMap<String, bool> {
&self.versions
}
}

pub fn retrieve_local_candidates() -> std::io::Result<Vec<LocalCandidate>> {
match env::var("SDKMAN_CANDIDATES_DIR") {
Err(e) => Err(Error::new(ErrorKind::NotFound, e)),
Ok(candidates_dir) => {
let mut local_versions: Vec<LocalCandidate> = Vec::new();
let mut local_candidates: Vec<LocalCandidate> = Vec::new();

for candidate_entry in fs::read_dir(candidates_dir)? {
let candidate_path = candidate_entry?.path();
if candidate_path.is_file() {
continue;
}
let binary_name = String::from(
candidate_path
.file_name()
.unwrap_or_default()
.to_string_lossy(),
);
let binary_name = candidate_path
.file_name()
.unwrap_or_default()
.to_string_lossy()
.to_string();

let mut installed_current_map: HashMap<String, bool> = HashMap::new();
let mut local_versions: HashMap<String, bool> = HashMap::new();

for version_dir in fs::read_dir(candidate_path)? {
let version_path = version_dir?.path();

if version_path.is_file() {
continue;
}

let version_id = version_path
.canonicalize()? // using canonicalize() follows a symlink and creates a canonized path
.file_name()
.unwrap_or_default()
.to_string_lossy()
.to_string();

let current = installed_current_map.contains_key(&version_id); // since we followed the symlink, one of the versions would be processed twice
installed_current_map.insert(version_id, current);
// since we followed the symlink,
// one of the versions would be processed twice,
// and that version is the currently used one
let current = local_versions.contains_key(&version_id);
local_versions.insert(version_id, current);
}

local_versions.push(LocalCandidate::new(
binary_name.to_string(),
installed_current_map
.iter()
.map(|(k, v)| {
CandidateVersion::new_local(Version::OtherVersion(k.to_string()), *v)
})
.collect(),
));
local_candidates.push(LocalCandidate::new(binary_name, local_versions));
}

Ok(local_versions)
Ok(local_candidates)
}
}
}
237 changes: 0 additions & 237 deletions api/src/model.rs

This file was deleted.

Loading

0 comments on commit 5aa555e

Please sign in to comment.