Skip to content

Commit 6a7741c

Browse files
committed
Update dependencies, fix config edits
1 parent fd07629 commit 6a7741c

File tree

10 files changed

+475
-415
lines changed

10 files changed

+475
-415
lines changed

Cargo.lock

Lines changed: 364 additions & 344 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,43 @@ edition = "2021"
1414
error-stack = "0.5.0"
1515
digest = "0.10.7"
1616
sha2 = "0.10.8"
17-
directories = "5.0.1"
17+
directories = "6.0.0"
1818
tracing = "0.1.41"
19-
toml = "0.8.19"
19+
toml = "0.8.20"
20+
toml_edit = "0.22.24"
2021
itertools = "0.14.0"
21-
thiserror = "2.0.9"
22-
console = "0.15.10"
23-
indicatif = "0.17.9"
22+
thiserror = "2.0.12"
23+
console = "0.15.11"
24+
indicatif = "0.17.11"
2425
hex = "0.4.3"
25-
tempfile = "3.15.0"
26-
tar = "0.4.43"
27-
flate2 = "1.0.35"
28-
zip = "2.2.2"
26+
tempfile = "3.19.1"
27+
tar = "0.4.44"
28+
flate2 = "1.1.0"
29+
zip = "2.5.0"
2930
enum_dispatch = "0.3.13"
3031

3132
[dependencies.url]
3233
version = "2.5.4"
3334
features = ["serde"]
3435

3536
[dependencies.ureq]
36-
version = "2.12.1"
37-
features = ["json", "proxy-from-env"]
37+
version = "3.0.10"
38+
features = ["json"]
3839

3940
[dependencies.owo-colors]
40-
version = "4.1.0"
41+
version = "4.2.0"
4142
features = ["supports-colors"]
4243

4344
[dependencies.derive_more]
44-
version = "1.0.0"
45+
version = "2.0.1"
4546
features = ["full"]
4647

4748
[dependencies.clap]
48-
version = "4.5.23"
49+
version = "4.5.34"
4950
features = ["derive"]
5051

5152
[dependencies.serde]
52-
version = "1.0.217"
53+
version = "1.0.219"
5354
features = ["derive"]
5455

5556
[dependencies.tracing-subscriber]

src/command/set_default.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,11 @@ impl JpreCommand for SetDefault {
3838
.get_jdk_path(&context.config, &self.jdk)
3939
.change_context(JpreError::Unexpected)
4040
.attach_printable_lazy(|| format!("Failed to get path for JDK {}", self.jdk))?;
41-
context.config.default_jdk = Some(self.jdk.clone());
42-
context
43-
.config
44-
.save()
45-
.change_context(JpreError::Unexpected)
46-
.attach_printable("Failed to save config")?;
41+
42+
context.config.edit_config(|doc| {
43+
doc["default_jdk"] = toml_edit::value(self.jdk.to_string());
44+
})?;
45+
4746
eprintln!(
4847
"Default JDK set to '{}'",
4948
self.jdk

src/command/set_distributions.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,17 @@ impl JpreCommand for SetDistributions {
5555
),
5656
}));
5757
}
58-
context.config.distributions = self.distributions.clone();
59-
context
60-
.config
61-
.save()
62-
.change_context(JpreError::Unexpected)
63-
.attach_printable("Failed to save config")?;
58+
59+
context.config.edit_config(|doc| {
60+
let mut distributions = toml_edit::Array::new();
61+
for distribution in &self.distributions {
62+
distributions.push(toml_edit::Value::from(distribution));
63+
}
64+
distributions.fmt();
65+
66+
doc["distributions"] = toml_edit::value(distributions);
67+
})?;
68+
6469
eprintln!("Distribution(s) set to '{}'", self.distributions.join(", "));
6570
Ok(())
6671
}

src/config.rs

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ use directories::ProjectDirs;
55
use error_stack::ResultExt;
66
use serde::{Deserialize, Serialize};
77
use std::path::PathBuf;
8+
use std::str::FromStr;
89
use std::sync::LazyLock;
9-
use tracing::{debug, trace};
10+
use toml::de::Error;
1011

1112
pub static PROJECT_DIRS: LazyLock<ProjectDirs> = LazyLock::new(|| {
1213
ProjectDirs::from("net", "octyl", "jpre").expect("Could not determine project directories")
@@ -54,12 +55,7 @@ impl JpreConfig {
5455
.attach_printable_lazy(|| {
5556
format!("Could not open config file at {:?}", *CONFIG_PATH)
5657
})?;
57-
let contents = std::fs::read_to_string(&*CONFIG_PATH)
58-
.change_context(JpreError::Unexpected)
59-
.attach_printable_lazy(|| {
60-
format!("Could not read config file at {:?}", *CONFIG_PATH)
61-
})?;
62-
let config = toml::from_str::<JpreConfig>(&contents);
58+
let (contents, config) = Self::read_config()?;
6359
match config {
6460
Ok(mut config) => {
6561
if let Some(distribution) = config.distribution {
@@ -90,19 +86,35 @@ impl JpreConfig {
9086
format!("Could not parse config file at {:?}", *CONFIG_PATH)
9187
});
9288
}
93-
// jpre 0.2 config format
94-
let new_config = JpreConfig {
95-
default_jdk: Some(VersionKey {
89+
90+
let mut new_config = toml_edit::DocumentMut::new();
91+
new_config["default_jdk"] = toml_edit::value(
92+
VersionKey {
9693
major: *major as u32,
9794
pre_release: PreRelease::None,
98-
}),
99-
distribution: None,
100-
distributions: default_distribution(),
101-
forced_architecture: None,
102-
forced_os: None,
103-
};
104-
new_config.save()?;
105-
return Ok(new_config);
95+
}
96+
.to_string(),
97+
);
98+
let mut distributions = toml_edit::Array::new();
99+
distributions.push("temurin");
100+
new_config["distributions"] = toml_edit::value(distributions);
101+
102+
// Ensure whatever is in the config is valid.
103+
toml::from_str::<JpreConfig>(&new_config.to_string())
104+
.expect("New config is invalid");
105+
106+
std::fs::write(&*CONFIG_PATH, new_config.to_string())
107+
.change_context(JpreError::Unexpected)
108+
.attach_printable_lazy(|| {
109+
format!("Could not write config file at {:?}", *CONFIG_PATH)
110+
})?;
111+
112+
return Self::read_config()?
113+
.1
114+
.change_context(JpreError::Unexpected)
115+
.attach_printable_lazy(|| {
116+
format!("Could not parse config file at {:?}", *CONFIG_PATH)
117+
});
106118
}
107119
Err(e)
108120
.change_context(JpreError::Unexpected)
@@ -113,16 +125,36 @@ impl JpreConfig {
113125
}
114126
}
115127

116-
pub fn save(&self) -> ESResult<(), JpreError> {
117-
let contents = toml::to_string(self)
128+
fn read_config() -> ESResult<(String, Result<JpreConfig, Error>), JpreError> {
129+
let contents = std::fs::read_to_string(&*CONFIG_PATH)
130+
.change_context(JpreError::Unexpected)
131+
.attach_printable_lazy(|| {
132+
format!("Could not read config file at {:?}", *CONFIG_PATH)
133+
})?;
134+
let config = toml::from_str::<JpreConfig>(&contents);
135+
Ok((contents, config))
136+
}
137+
138+
pub fn edit_config<F: FnOnce(&mut toml_edit::DocumentMut)>(
139+
&mut self,
140+
editor: F,
141+
) -> ESResult<(), JpreError> {
142+
let contents = Self::read_config()?.0;
143+
let mut config = toml_edit::DocumentMut::from_str(&contents)
118144
.change_context(JpreError::Unexpected)
119-
.attach_printable("Could not serialize config to TOML")?;
120-
debug!("Writing config to {:?}", *CONFIG_PATH);
121-
trace!("Config: {}", contents);
122-
std::fs::write(&*CONFIG_PATH, contents)
145+
.attach_printable_lazy(|| {
146+
format!("Could not parse config file at {:?}", *CONFIG_PATH)
147+
})?;
148+
editor(&mut config);
149+
150+
// Ensure whatever is in the config is valid, and update ourselves to it.
151+
*self = toml::from_str::<JpreConfig>(&config.to_string())
152+
.unwrap_or_else(|e| panic!("Edited config is invalid: {}", e));
153+
154+
std::fs::write(&*CONFIG_PATH, config.to_string())
123155
.change_context(JpreError::Unexpected)
124156
.attach_printable_lazy(|| {
125-
format!("Could not write config file to {:?}", *CONFIG_PATH)
157+
format!("Could not write config file at {:?}", *CONFIG_PATH)
126158
})?;
127159
Ok(())
128160
}

src/foojay.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,19 +208,21 @@ impl FoojayDiscoApi {
208208
.change_context(FoojayDiscoApiError::Api)?;
209209
let status_code = response.status();
210210
let data: FoojayResult<T> = response
211-
.into_json()
211+
.into_body()
212+
.read_json()
212213
.change_context(FoojayDiscoApiError::Api)?;
213214

214-
match status_code {
215-
200..=299 => Ok(data.result),
216-
_ => match data.message.as_str() {
215+
if status_code.is_success() {
216+
Ok(data.result)
217+
} else {
218+
match data.message.as_str() {
217219
"Requested distribution not found" => {
218220
Err(Report::new(FoojayDiscoApiError::InvalidDistribution))
219221
}
220222
_ => Err(Report::new(FoojayDiscoApiError::Api)
221223
.attach_printable(format!("Unknown message: {}", data.message)))
222224
.attach_printable(format!("Status code: {}", status_code)),
223-
},
225+
}
224226
}
225227
}
226228

@@ -241,10 +243,10 @@ fn try_fill_checksum(info: &mut FoojayPackageInfo) {
241243
let Ok(response) = ureq::get(&url).call() else {
242244
continue;
243245
};
244-
if !matches!(response.status(), 200..=299) {
246+
if !response.status().is_success() {
245247
continue;
246248
}
247-
let Ok(checksum) = response.into_string() else {
249+
let Ok(checksum) = response.into_body().read_to_string() else {
248250
continue;
249251
};
250252
let checksum = checksum.trim();

src/http_client.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
pub fn new_http_client() -> ureq::Agent {
2-
ureq::AgentBuilder::new()
3-
.timeout_connect(std::time::Duration::from_secs(5))
4-
.timeout_read(std::time::Duration::from_secs(30))
5-
.timeout_write(std::time::Duration::from_secs(30))
2+
ureq::Agent::config_builder()
3+
.timeout_connect(Some(std::time::Duration::from_secs(5)))
4+
.timeout_recv_response(Some(std::time::Duration::from_secs(10)))
5+
.timeout_recv_body(Some(std::time::Duration::from_secs(30)))
6+
.timeout_send_request(Some(std::time::Duration::from_secs(10)))
7+
.timeout_send_body(Some(std::time::Duration::from_secs(30)))
68
.user_agent(concat!(
79
env!("CARGO_PKG_NAME"),
810
"/",
@@ -13,4 +15,5 @@ pub fn new_http_client() -> ureq::Agent {
1315
))
1416
.https_only(true)
1517
.build()
18+
.new_agent()
1619
}

src/java_version.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use derive_more::Display;
77
use error_stack::{Context, Report, ResultExt};
88
use serde::Deserialize;
99
use std::cmp::Ordering;
10+
use std::fmt::Display;
1011
use std::str::{FromStr, Split};
1112

1213
#[derive(Debug, Display)]

src/jdk_manager.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ use std::str::FromStr;
1818
use std::sync::LazyLock;
1919
use tempfile::TempDir;
2020
use tracing::warn;
21-
use ureq::Response;
21+
use ureq::http::Response;
22+
use ureq::Body;
2223

2324
#[derive(Debug, Display)]
2425
pub struct JdkManagerError;
@@ -251,7 +252,7 @@ impl JdkManager {
251252
fn download_jdk_to_file(
252253
list_info: &FoojayPackageListInfo,
253254
info: &FoojayPackageInfo,
254-
response: Response,
255+
response: Response<Body>,
255256
download_path: &Path,
256257
) -> ESResult<(), JdkManagerError> {
257258
let mut file = std::fs::File::create(download_path)
@@ -276,17 +277,15 @@ impl JdkManager {
276277
&mut file,
277278
);
278279
let progress_bar = new_progress_bar(
279-
response
280-
.header("Content-Length")
281-
.and_then(|s| s.parse().ok()),
280+
response.body().content_length(),
282281
)
283282
.with_message(
284283
format!("Downloading JDK {}", list_info.java_version)
285284
.if_supports_color(Stream::Stderr, |s| s.green())
286285
.to_string(),
287286
);
288287
std::io::copy(
289-
&mut response.into_reader(),
288+
&mut response.into_body().into_reader(),
290289
&mut progress_bar.wrap_write(&mut checksum_verifier),
291290
)
292291
.change_context(JdkManagerError)

src/main.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@ fn main_with_result() -> ESResult<(), JpreError> {
130130
}
131131

132132
let config = JpreConfig::load()?;
133-
// re-save config to ensure it's up-to-date
134-
config.save()?;
135133

136134
let context = Context {
137135
config: config.clone(),

0 commit comments

Comments
 (0)