Skip to content

Commit 3f55fb7

Browse files
committed
feat: separate versions.json into individual files
1 parent ee50aca commit 3f55fb7

File tree

5 files changed

+61
-61
lines changed

5 files changed

+61
-61
lines changed

src/files/index.rs

+8-14
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@ use crate::files::variables::AppConfig;
22
use crate::serialization::parse_json;
33
use log::debug;
44
use serde::{Deserialize, Serialize};
5-
use std::collections::HashMap;
65
use std::error::Error;
76
use std::path::Path;
87

9-
#[derive(Serialize, Deserialize, Debug)]
10-
pub struct Root {
11-
pub packages: HashMap<String, Package>,
12-
}
13-
148
#[derive(Serialize, Deserialize, Debug, Clone)]
159
pub struct Package {
1610
pub image: String,
@@ -66,23 +60,23 @@ fn load_package(
6660

6761
if override_file.exists() {
6862
debug!("Using override config at {:?}", &override_file);
69-
parse_json(&override_file)
63+
Ok(parse_json(&override_file)?)
64+
} else if index_file.exists() {
65+
debug!("Using index config at {:?}", &index_file);
66+
Ok(parse_json(&index_file)?)
7067
} else {
71-
debug!("Using normal config at {:?}", &index_file);
72-
parse_json(&index_file)
68+
debug!("No config file found for {}, using default values", &name);
69+
Ok(Package::new(name))
7370
}
7471
}
7572

76-
pub fn parse(name: String) -> Result<Root, Box<dyn Error>> {
73+
pub fn parse(name: String) -> Result<Package, Box<dyn Error>> {
7774
let config = AppConfig::new();
7875
let index_path = config.index_path().to_owned();
7976
let package_dir = Path::new(&index_path);
8077
let overrides_path = config.overrides_path().to_owned();
8178
let overrides_dir = Path::new(&overrides_path);
82-
let mut packages = HashMap::new();
83-
8479
let package = load_package(&name, package_dir, overrides_dir)?;
85-
packages.insert(name, package);
8680

87-
Ok(Root { packages })
81+
Ok(package)
8882
}

src/files/variables.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl AppConfig {
3636
}
3737

3838
pub fn versions_path(&self) -> PathBuf {
39-
self.base_dir.join("versions.json")
39+
self.base_dir.join("versions")
4040
}
4141

4242
pub fn shims_path(&self) -> PathBuf {

src/files/versions.rs

+16-13
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ use crate::serialization::{parse_json, save_json};
33
use log::debug;
44
use serde::{Deserialize, Serialize};
55
use std::error::Error;
6-
7-
#[derive(Serialize, Deserialize, Debug)]
8-
pub struct Root {
9-
pub packages: std::collections::HashMap<String, Package>,
10-
}
6+
use std::fs::remove_file;
117

128
#[derive(Serialize, Deserialize, Debug, Clone)]
139
pub struct Package {
@@ -24,25 +20,32 @@ impl Package {
2420
}
2521
}
2622

27-
pub fn parse() -> Result<Root, Box<dyn Error>> {
23+
pub fn parse(name: String) -> Result<Option<Package>, Box<dyn Error>> {
2824
let config = AppConfig::new();
29-
parse_json(&config.versions_path())
25+
let version_file = config.versions_path().join(format!("{}.json", name));
26+
if version_file.exists() {
27+
Ok(parse_json(&version_file)?)
28+
} else {
29+
Ok(None)
30+
}
3031
}
3132

3233
pub fn upsert(name: &str, package: crate::packages::Package) -> Result<(), Box<dyn Error>> {
3334
debug!("Adding/Updating package '{}'", name);
3435
let config = AppConfig::new();
35-
let mut root = parse()?;
36-
root.packages.insert(String::from(name), package.versions);
37-
save_json(&root, &config.versions_path())?;
36+
let version_file = config.versions_path().join(format!("{}.json", name));
37+
debug!("Saving file {:?}", &version_file);
38+
save_json(&package.versions, &version_file)?;
3839
Ok(())
3940
}
4041

4142
pub fn remove(name: &str) -> Result<(), Box<dyn Error>> {
4243
debug!("Removing package: '{}'", name);
4344
let config = AppConfig::new();
44-
let mut root = parse()?;
45-
root.packages.remove(name);
46-
save_json(&root, &config.versions_path())?;
45+
let version_file = config.versions_path().join(format!("{}.json", name));
46+
if version_file.exists() {
47+
debug!("Removing file {:?}", &version_file);
48+
remove_file(&version_file)?
49+
}
4750
Ok(())
4851
}

src/packages.rs

+33-33
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use crate::files::index::Package as IndexPackage;
2+
use crate::files::variables::AppConfig;
23
use crate::files::versions::Package as VersionsPackage;
3-
use log::info;
4+
use log::{debug, info};
45
use std::error::Error;
6+
use std::fs;
57

68
#[derive(Debug, Clone)]
79
pub struct Package {
@@ -13,12 +15,7 @@ pub struct Package {
1315
// Public API
1416
impl Package {
1517
pub fn new(name: &str, versions_package: VersionsPackage) -> Result<Self, Box<dyn Error>> {
16-
let index_packages = crate::files::index::parse(name.to_owned())?;
17-
let index_package = index_packages
18-
.packages
19-
.get(name)
20-
.map(|pkg| pkg.to_owned())
21-
.unwrap_or_else(|| IndexPackage::new(name));
18+
let index_package = crate::files::index::parse(name.to_owned())?;
2219
Ok(Self {
2320
name: String::from(name),
2421
index: index_package,
@@ -27,23 +24,34 @@ impl Package {
2724
}
2825

2926
pub fn load(name: &str) -> Result<Option<Self>, Box<dyn Error>> {
30-
let index_package = crate::files::index::parse(name.to_owned())?
31-
.packages
32-
.remove(name);
33-
let versions_package = crate::files::versions::parse()?.packages.remove(name);
34-
Self::make_from(name, index_package, versions_package)
27+
if let Some(versions_package) = crate::files::versions::parse(name.to_owned())? {
28+
let index_package = crate::files::index::parse(name.to_owned())?;
29+
let package = Self::make_from(name, index_package, versions_package)?;
30+
Ok(Some(package))
31+
} else {
32+
Ok(None)
33+
}
3534
}
3635

3736
pub fn load_all() -> Result<Vec<Self>, Box<dyn Error>> {
38-
let versions_config = crate::files::versions::parse()?;
3937
let mut packages: Vec<Self> = Vec::new();
38+
let config = AppConfig::new();
39+
for entry in fs::read_dir(config.versions_path())? {
40+
let entry = entry?;
41+
let path = entry.path();
42+
if path.is_file() && path.extension().and_then(|s| s.to_str()) == Some("json") {
43+
let name = path
44+
.file_stem()
45+
.and_then(|s| s.to_str())
46+
.ok_or("Invalid package name")?
47+
.to_string();
4048

41-
for name in versions_config.packages.keys() {
42-
if let Some(package) = Self::load(name)? {
43-
packages.push(package);
49+
if let Some(package) = Self::load(&name)? {
50+
debug!("Loading package {}", name);
51+
packages.push(package);
52+
}
4453
}
4554
}
46-
4755
Ok(packages)
4856
}
4957

@@ -93,21 +101,13 @@ impl Package {
93101
impl Package {
94102
fn make_from(
95103
name: &str,
96-
index_package: Option<IndexPackage>,
97-
versions_package: Option<VersionsPackage>,
98-
) -> Result<Option<Self>, Box<dyn Error>> {
99-
match (index_package, versions_package) {
100-
(Some(index_package), Some(versions_package)) => Ok(Some(Self {
101-
name: String::from(name),
102-
index: index_package,
103-
versions: versions_package,
104-
})),
105-
(None, Some(versions_package)) => Ok(Some(Self {
106-
name: String::from(name),
107-
index: IndexPackage::new(name),
108-
versions: versions_package,
109-
})),
110-
(_, None) => Ok(None),
111-
}
104+
index_package: IndexPackage,
105+
versions_package: VersionsPackage,
106+
) -> Result<Self, Box<dyn Error>> {
107+
Ok(Self {
108+
name: String::from(name),
109+
index: index_package,
110+
versions: versions_package,
111+
})
112112
}
113113
}

src/shims.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::files::variables::AppConfig;
2+
use log::debug;
23
use std::fs::{self, File};
34
use std::io::Write;
45
use std::path::PathBuf;
@@ -10,6 +11,7 @@ pub fn add_shim(name: &str, binary: Option<&str>) -> std::io::Result<()> {
1011

1112
if !shims_file_path.exists() {
1213
fs::create_dir_all(shims_file_path.parent().unwrap())?;
14+
debug!("Creating shim {:?}", &shims_file_path);
1315
let mut shim_file = File::create(&shims_file_path)?;
1416

1517
let command = match binary {
@@ -43,6 +45,7 @@ pub fn remove_shim(name: &str) -> std::io::Result<()> {
4345
let shims_file_path = get_shims_path(name, config);
4446

4547
if shims_file_path.exists() {
48+
debug!("Removing shim {:?}", &shims_file_path);
4649
fs::remove_file(shims_file_path)?;
4750
}
4851
Ok(())

0 commit comments

Comments
 (0)