-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
logging system, more self-update work
- Loading branch information
1 parent
32df21a
commit 0ac0710
Showing
10 changed files
with
341 additions
and
60 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use httpkit::{Client, Method}; | ||
use serde::Deserialize; | ||
use std::path::PathBuf; | ||
|
||
const BROTH_BASE_URL: &str = "https://broth.itch.ovh"; | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum Error { | ||
#[error("httpkit error: {0}")] | ||
HttpKitError(#[from] httpkit::Error), | ||
#[error("i/o error: {0}")] | ||
IOError(#[from] std::io::Error), | ||
} | ||
|
||
pub struct Settings { | ||
pub components_dir: PathBuf, | ||
} | ||
|
||
pub struct ComponentSpec<'a> { | ||
pub settings: &'a Settings, | ||
pub component: String, | ||
pub channel: String, | ||
} | ||
|
||
impl ComponentSpec<'_> { | ||
pub fn channel_url(&self) -> String { | ||
format!("{}/{}/{}", BROTH_BASE_URL, self.component, self.channel) | ||
} | ||
|
||
pub async fn get_latest_version(&self, client: &Client) -> Result<Option<String>, Error> { | ||
let version_url = format!("{}/versions", self.channel_url()); | ||
let req = client.request(Method::GET, &version_url).build()?; | ||
|
||
let versions_list: VersionsList = client.execute(req).await?.json().await?; | ||
Ok(versions_list.versions.into_iter().last()) | ||
} | ||
|
||
pub fn get_present_versions(&self) -> Result<Vec<String>, Error> { | ||
let versions_dir = self | ||
.settings | ||
.components_dir | ||
.join(&self.component) | ||
.join("versions"); | ||
log::debug!( | ||
"Looking for {} versions in {}", | ||
self.component, | ||
versions_dir.display() | ||
); | ||
|
||
let mut res: Vec<String> = Default::default(); | ||
|
||
if !versions_dir.exists() { | ||
std::fs::create_dir_all(&versions_dir)?; | ||
} | ||
|
||
for entry in std::fs::read_dir(&versions_dir)? { | ||
let entry = entry?; | ||
if entry.file_type()?.is_dir() { | ||
if let Some(name) = entry.path().file_name() { | ||
res.push(name.to_string_lossy().into()) | ||
} | ||
} | ||
} | ||
|
||
Ok(res) | ||
} | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
pub struct VersionsList { | ||
pub versions: Vec<String>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,52 @@ | ||
mod platform; | ||
pub mod broth; | ||
pub mod platform; | ||
|
||
use httpkit::{Client, Method}; | ||
use serde::Deserialize; | ||
use httpkit::Client; | ||
use std::path::PathBuf; | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum Error { | ||
#[error("dummy error")] | ||
DummyError, | ||
#[error("request error: {0}")] | ||
RequestError(String), | ||
#[error("broth error: {0}")] | ||
BrothError(#[from] broth::Error), | ||
#[error("platform error: {0}")] | ||
PlatformError(#[from] platform::Error), | ||
} | ||
|
||
impl From<httpkit::Error> for Error { | ||
fn from(err: httpkit::Error) -> Self { | ||
Self::RequestError(err.to_string()) | ||
} | ||
#[error("httpkit error: {0}")] | ||
HttpKitError(#[from] httpkit::Error), | ||
#[error("no latest version of itch-setup")] | ||
NoLatestVersion, | ||
} | ||
|
||
pub struct Settings { | ||
pub components_dir: PathBuf, | ||
pub is_canary: bool, | ||
} | ||
|
||
const BROTH_BASE_URL: &str = "https://broth.itch.ovh"; | ||
|
||
#[derive(Deserialize, Debug)] | ||
struct VersionsList { | ||
versions: Vec<String>, | ||
} | ||
|
||
pub async fn check(settings: &Settings) -> Result<String, Error> { | ||
log::info!("Checking for update..."); | ||
log::info!("Checking for itch-setup updates..."); | ||
|
||
let channel = platform::get_channel_name(settings)?; | ||
log::info!("For channel {}", channel); | ||
let channel_url = format!("{}/itch-setup/{}", BROTH_BASE_URL, channel); | ||
let spec = broth::ComponentSpec { | ||
settings: &broth::Settings { | ||
components_dir: settings.components_dir.clone(), | ||
}, | ||
component: "itch-setup".into(), | ||
channel: platform::get_channel_name(settings)?, | ||
}; | ||
|
||
let client = Client::new()?; | ||
let version_url = format!("{}/versions", channel_url); | ||
let req = client.request(Method::GET, &version_url).build()?; | ||
|
||
let versions_list: VersionsList = client | ||
.execute(req) | ||
.await | ||
.map_err(|e| Error::RequestError(e.to_string()))? | ||
.json() | ||
.await?; | ||
let latest_version = spec | ||
.get_latest_version(&client) | ||
.await? | ||
.ok_or(Error::NoLatestVersion)?; | ||
let present_versions = spec.get_present_versions()?; | ||
|
||
let latest_version = versions_list.versions.first(); | ||
log::info!("found {} present versions: ", present_versions.len()); | ||
for v in &present_versions { | ||
log::info!("- {}", v); | ||
} | ||
log::info!("latest version: {}", latest_version); | ||
|
||
let res = format!("latest version of itch-setup is {:?}", latest_version); | ||
Ok(res) | ||
Ok("stub!".into()) | ||
} |
Oops, something went wrong.