-
-
Notifications
You must be signed in to change notification settings - Fork 253
/
version.rs
99 lines (81 loc) · 3.39 KB
/
version.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//LICENSE Portions Copyright 2019-2021 ZomboDB, LLC.
//LICENSE
//LICENSE Portions Copyright 2021-2023 Technology Concepts & Design, Inc.
//LICENSE
//LICENSE Portions Copyright 2023-2023 PgCentral Foundation, Inc. <[email protected]>
//LICENSE
//LICENSE All rights reserved.
//LICENSE
//LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file.
use pgrx_pg_config::{PgConfig, Pgrx, SUPPORTED_VERSIONS};
pub(crate) fn pgrx_default() -> eyre::Result<Pgrx> {
let mut pgrx = Pgrx::default();
rss::PostgreSQLVersionRss::new(&SUPPORTED_VERSIONS())?
.into_iter()
.for_each(|version| pgrx.push(PgConfig::from(version)));
Ok(pgrx)
}
mod rss {
use eyre::WrapErr;
use owo_colors::OwoColorize;
use pgrx_pg_config::{PgMinorVersion, PgVersion};
use serde::Deserialize;
use std::collections::BTreeMap;
use url::Url;
use crate::command::build_agent_for_url;
pub(super) struct PostgreSQLVersionRss;
impl PostgreSQLVersionRss {
pub(super) fn new(supported_versions: &[PgVersion]) -> eyre::Result<Vec<PgVersion>> {
static VERSIONS_RSS_URL: &str = "https://www.postgresql.org/versions.rss";
let http_client = build_agent_for_url(VERSIONS_RSS_URL)?;
let response = http_client
.get(VERSIONS_RSS_URL)
.call()
.wrap_err_with(|| format!("unable to retrieve {VERSIONS_RSS_URL}"))?;
let rss: Rss = match serde_xml_rs::from_str(&response.into_string()?) {
Ok(rss) => rss,
Err(e) => return Err(e.into()),
};
let mut versions: BTreeMap<u16, PgVersion> = BTreeMap::from_iter(
supported_versions.iter().map(|pgver| (pgver.major, pgver.clone())),
);
for item in rss.channel.item {
let title = item.title.trim();
let mut parts = title.split('.');
let major = parts.next();
let minor = parts.next();
// if we don't have major/minor versions or if they don't parse correctly
// we'll just assume zero for them and eventually skip them
let major = major.unwrap().parse::<u16>().unwrap_or_default();
let minor = minor.unwrap().parse::<u16>().unwrap_or_default();
if let Some(known_pgver) = versions.get_mut(&major) {
if matches!(known_pgver.minor, PgMinorVersion::Latest) {
// fill in the latest minor version number and its url
known_pgver.minor = PgMinorVersion::Release(minor);
known_pgver.url = Some(Url::parse(
&format!("https://ftp.postgresql.org/pub/source/v{major}.{minor}/postgresql-{major}.{minor}.tar.bz2")
)?);
}
}
}
println!(
"{} Postgres {}",
" Discovered".white().bold(),
versions.iter().map(|ver| format!("v{}", ver.1)).collect::<Vec<_>>().join(", ")
);
Ok(versions.into_values().collect())
}
}
#[derive(Deserialize)]
struct Rss {
channel: Channel,
}
#[derive(Deserialize)]
struct Channel {
item: Vec<Item>,
}
#[derive(Deserialize)]
struct Item {
title: String,
}
}