Skip to content

Commit

Permalink
logging system, more self-update work
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Jul 27, 2020
1 parent 32df21a commit 0ac0710
Show file tree
Hide file tree
Showing 10 changed files with 341 additions and 60 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ tokio = { version = "0.2.21", features = ["sync", "rt-core", "rt-util", "rt-thre
selfupdate = { path = "./selfupdate" }
futures = "0.3.5"
once_cell = "1.4.0"
flume = "0.7.2"

[target.'cfg(windows)'.dependencies]
napi_stub = { path = "./napi_stub" }
Expand Down
3 changes: 3 additions & 0 deletions libbutler/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ github.com/efarrer/iothrottler v0.0.1/go.mod h1:zGWF5N0NKSCskcPFytDAFwI121DdU/Nf
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
github.com/getlantern/context v0.0.0-20190109183933-c447772a6520 h1:NRUJuo3v3WGC/g5YiyF790gut6oQr5f3FBI88Wv0dx4=
github.com/getlantern/context v0.0.0-20190109183933-c447772a6520/go.mod h1:L+mq6/vvYHKjCX2oez0CgEAJmbq1fbb/oNJIWQkBybY=
Expand Down Expand Up @@ -236,10 +237,12 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/russross/blackfriday v2.0.0+incompatible h1:cBXrhZNUf9C+La9/YpS+UHpUT8YD6Td9ZMSU9APFcsk=
github.com/russross/blackfriday v2.0.0+incompatible/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/scjalliance/comshim v0.0.0-20190308082608-cf06d2532c4e h1:+/AzLkOdIXEPrAQtwAeWOBnPQ0BnYlBW0aCZmSb47u4=
github.com/scjalliance/comshim v0.0.0-20190308082608-cf06d2532c4e/go.mod h1:9Tc1SKnfACJb9N7cw2eyuI6xzy845G7uZONBsi5uPEA=
github.com/shirou/gopsutil v2.20.4+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA=
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog=
Expand Down
21 changes: 20 additions & 1 deletion napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,25 @@ pub struct JMethodInfo<T, D> {
pub data: *mut D,
}

/// Template to build a JavaScript Error, given a `JsEnv`.
pub struct ErrorTemplate<T: ToNapi> {
pub code: Option<T>,
pub msg: T,
}

impl<T: ToNapi> ToNapi for ErrorTemplate<T> {
fn to_napi(&self, env: &JsEnv) -> JsResult<RawValue> {
let mut res = ptr::null_mut();
let code = match self.code.as_ref() {
Some(code) => code.to_napi(env)?,
None => ptr::null_mut(),
};
let msg = self.msg.to_napi(env)?;
unsafe { napi_create_error(env.0, code, msg, &mut res) }.check()?;
Ok(res)
}
}

#[allow(dead_code)]
impl JsEnv {
pub fn new(e: napi_env) -> Self {
Expand All @@ -498,7 +517,7 @@ impl JsEnv {
ptr::null_mut(),
async_resource_name,
0,
2,
1,
ptr::null_mut(),
None,
context,
Expand Down
72 changes: 72 additions & 0 deletions selfupdate/src/broth/mod.rs
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>,
}
61 changes: 28 additions & 33 deletions selfupdate/src/lib.rs
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())
}
Loading

0 comments on commit 0ac0710

Please sign in to comment.