Skip to content

Commit

Permalink
use explicit imports for macros
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleades committed Apr 16, 2021
1 parent 7be2122 commit 7e98f4c
Show file tree
Hide file tree
Showing 24 changed files with 490 additions and 140 deletions.
2 changes: 1 addition & 1 deletion gtk/src/events/background/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn run(
) {
let send: &dyn Fn(UiEvent) = &send;
if let Ok(ref mut client) = Client::new() {
info!("Checking for updates to daemon");
log::info!("Checking for updates to daemon");
if client.update_and_restart().unwrap_or(false) {
send(UiEvent::Updating);
let file = std::path::Path::new(pop_upgrade::RESTART_SCHEDULED);
Expand Down
4 changes: 2 additions & 2 deletions gtk/src/events/background/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use pop_upgrade::{
};

pub fn download(client: &Client, send: &dyn Fn(UiEvent), info: ReleaseInfo) {
info!("downloading updates for {}", info.next);
log::info!("downloading updates for {}", info.next);
if !update(client, send) {
return;
}
Expand Down Expand Up @@ -110,7 +110,7 @@ pub fn download(client: &Client, send: &dyn Fn(UiEvent), info: ReleaseInfo) {
}

pub fn update(client: &Client, send: &dyn Fn(UiEvent)) -> bool {
info!("checking if updates are required");
log::info!("checking if updates are required");
let updates = match client.fetch_updates(Vec::new(), false) {
Ok(updates) => updates,
Err(why) => {
Expand Down
4 changes: 2 additions & 2 deletions gtk/src/events/background/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn scan(client: &Client, send: &dyn Fn(UiEvent)) {
Ok(info) => {
is_lts = info.is_lts;
if devel || info.build >= 0 {
info!("upgrade from {} to {} is available", info.current, info.next);
log::info!("upgrade from {} to {} is available", info.current, info.next);

let upgrade_text = Cow::Owned(if reboot_ready {
format!("Pop!_OS is ready to upgrade to {}", info.next)
Expand All @@ -54,7 +54,7 @@ pub fn scan(client: &Client, send: &dyn Fn(UiEvent)) {
}
Err(why) => {
status_failed = true;
error!("failed to check for updates: {}", why);
log::error!("failed to check for updates: {}", why);
Cow::Borrowed("Failed to check for updates")
}
}
Expand Down
10 changes: 5 additions & 5 deletions gtk/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ pub fn attach(gui_receiver: glib::Receiver<UiEvent>, widgets: EventWidgets, mut
}

UiEvent::Completed(CompletedEvent::Recovery) => {
info!("successfully upgraded recovery partition");
log::info!("successfully upgraded recovery partition");
}

UiEvent::Completed(CompletedEvent::Refresh) => reboot(),
Expand Down Expand Up @@ -201,14 +201,14 @@ pub fn attach(gui_receiver: glib::Receiver<UiEvent>, widgets: EventWidgets, mut
UiEvent::ReleaseUpgradeDialog => release_upgrade_dialog(&mut state, &widgets),

UiEvent::Dismissed(dismissed) => {
info!("{} release", if dismissed { "dismissed" } else { "un-dismissed" });
log::info!("{} release", if dismissed { "dismissed" } else { "un-dismissed" });
if let Some(dismisser) = state.dismisser.as_mut() {
dismisser.set_dismissed(dismissed);
}
}

UiEvent::StatusChanged(from, to, why) => {
warn!("status changed from {} to {}: {}", from, to, why);
log::warn!("status changed from {} to {}: {}", from, to, why);
let _ = state.sender.send(BackgroundEvent::GetStatus(from));
}

Expand Down Expand Up @@ -267,7 +267,7 @@ fn connect_upgrade(state: &mut State, widgets: &EventWidgets, is_lts: bool, rebo
}
}
Err(why) => {
error!("failed to fetch EOL date: {}", why);
log::error!("failed to fetch EOL date: {}", why);
None
}
};
Expand Down Expand Up @@ -375,7 +375,7 @@ fn error(state: &mut State, widgets: &EventWidgets, why: UiError) {
.as_str(),
);

error!("{}", error_message);
log::error!("{}", error_message);

if let UiError::Dismiss(dismissed, _) = why {
if let Some(dismisser) = state.dismisser.as_mut() {
Expand Down
2 changes: 1 addition & 1 deletion src/checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub enum ValidateError {
}

pub async fn validate_checksum(file: &mut File, checksum: &str) -> Result<(), ValidateError> {
info!("validating checksum of downloaded ISO");
log::info!("validating checksum of downloaded ISO");
let expected = <[u8; 32]>::from_hex(checksum)
.map(GenericArray::from)
.map_err(|_| ValidateError::InvalidInput)?;
Expand Down
25 changes: 14 additions & 11 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ mod colors;

use self::colors::*;
use crate::notify::notify;

use apt_cmd::AptUpgradeEvent;
use chrono::{TimeZone, Utc};
use clap::ArgMatches;
use fomat_macros::{fomat, pintln};
use num_traits::FromPrimitive;
use pop_upgrade::{
client,
Expand Down Expand Up @@ -38,7 +38,7 @@ const UPGRADE_RESULT_STR: &str = "Release upgrade status";
const UPGRADE_RESULT_SUCCESS: &str = "systems are go for launch: reboot now";
const UPGRADE_RESULT_ERROR: &str = "release upgrade aborted";

#[derive(Shrinkwrap)]
#[derive(shrinkwraprs::Shrinkwrap)]
pub struct Client(client::Client);

impl Client {
Expand Down Expand Up @@ -235,7 +235,7 @@ impl Client {
/// Check if the release has been dismissed by timestamp, or can be.
fn dismiss_by_timestamp(&self, next: &str) -> Result<bool, client::Error> {
if !Path::new(INSTALL_DATE).exists() && installed_after_release(next) {
info!("dismissing notification for the latest release automatically");
log::info!("dismissing notification for the latest release automatically");
let _ = self.dismiss_notification(DismissEvent::ByTimestamp)?;
Ok(true)
} else {
Expand Down Expand Up @@ -294,7 +294,7 @@ impl Client {
if let Ok(event) = AptUpgradeEvent::from_dbus_map(event.into_iter()) {
write_apt_event(event);
} else {
error!("failed to unpack the upgrade event");
log::error!("failed to unpack the upgrade event");
}
}
_ => (),
Expand Down Expand Up @@ -412,7 +412,9 @@ impl Client {
client::Signal::PackageUpgrade(event) => {
match AptUpgradeEvent::from_dbus_map(event.clone().into_iter()) {
Ok(event) => write_apt_event(event),
Err(()) => error!("failed to unpack the upgrade event: {:?}", event),
Err(()) => {
log::error!("failed to unpack the upgrade event: {:?}", event)
}
}
}
client::Signal::ReleaseResult(status) => {
Expand Down Expand Up @@ -481,18 +483,19 @@ fn installed_after_release(next: &str) -> bool {
Ok(codename) => {
return codename.release_timestamp() < install_time as u64
}
Err(()) => error!("version {} is invalid", next),
Err(()) => log::error!("version {} is invalid", next),
}
}
_ => error!(
_ => log::error!(
"major ({}) and minor({}) version failed to parse as u8",
major, minor
major,
minor
),
}
}
None => error!("version {} is invalid", next),
None => log::error!("version {} is invalid", next),
},
Err(why) => error!("failed to get install time: {}", why),
Err(why) => log::error!("failed to get install time: {}", why),
}

false
Expand Down Expand Up @@ -525,7 +528,7 @@ fn notification_message(current: &str, next: &str) -> (String, String) {
}
EolStatus::Ok => (),
},
Err(why) => error!("failed to fetch EOL date: {}", why),
Err(why) => log::error!("failed to fetch EOL date: {}", why),
}

("Upgrade Available".into(), fomat!("Pop!_OS " (next) " is available to download"))
Expand Down
142 changes: 142 additions & 0 deletions src/cli2/release.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
use crate::{notify::notify, Error};
use chrono::{offset::TimeZone, Utc};
use pop_upgrade::{
client::{Client, Error as ClientError, ReleaseInfo},
daemon::{DismissEvent, DISMISSED, INSTALL_DATE},
misc,
release::eol::{EolDate, EolStatus},
};
use std::{convert::TryFrom, fs, path::Path};
use structopt::StructOpt;
use ubuntu_version::{Codename, Version as UbuntuVersion};

mod check;
mod update;
use update::Update;
mod dismiss;

/// check for new distribution releases, or upgrade to a new release
#[derive(Debug, StructOpt)]
pub enum Release {
/// check for a new distribution release
Check,

/// dismiss the current release notification (LTS only)
Dismiss,

Update(Update),

Refresh(Refresh),

/// search for issues in the system, and repair them
Repair {
/// Attempt to upgrade to the next release, even if it is not released
#[structopt(short, long)]
force_next: bool,
},

/// update the system, and fetch the packages for the next release
Upgrade,
}

impl Release {
pub fn run(&self, client: &Client) -> Result<(), Error> {
match self {
Self::Check => check::run(client)?,
Self::Dismiss => dismiss::run(client)?,
Self::Update(update) => update.run(client)?,
_ => todo!(),
};

Ok(())
}
}

/// refresh the existing OS (requires recovery partition)
#[derive(Debug, StructOpt)]
pub enum Refresh {
Enable,
Disable,
}

/// Check if the release has been dismissed by timestamp, or can be.
fn dismiss_by_timestamp(client: &Client, next: &str) -> Result<bool, ClientError> {
if !Path::new(INSTALL_DATE).exists() && installed_after_release(next) {
log::info!("dismissing notification for the latest release automatically");
let _ = client.dismiss_notification(DismissEvent::ByTimestamp)?;
Ok(true)
} else {
Ok(false)
}
}

fn notification_message(current: &str, next: &str) -> (String, String) {
match EolDate::fetch() {
Ok(eol) => match eol.status() {
EolStatus::Exceeded => {
return (
fomat!("Support for Pop!_OS " (current) " has ended"),
fomat!(
"Security and application updates are no longer provided for Pop!_OS "
(current) ". Upgrade to Pop!_OS " (next) " to keep your computer secure."
),
);
}
EolStatus::Imminent => {
let (y, m, d) = eol.ymd;
return (
fomat!(
"Support for Pop!_OS " (current) " ends "
(Utc.ymd(y as i32, m, d).format("%B %-d, %Y"))
),
fomat!(
"This computer will soon stop receiving updates"
". Upgrade to Pop!_OS " (next) " to keep your computer secure."
),
);
}
EolStatus::Ok => (),
},
Err(why) => log::error!("failed to fetch EOL date: {}", why),
}

("Upgrade Available".into(), fomat!("Pop!_OS " (next) " is available to download"))
}

/// If the next release's timestamp is less than the install time.
fn installed_after_release(next: &str) -> bool {
match pop_upgrade::install::time() {
Ok(install_time) => match next.find('.') {
Some(pos) => {
let (major, mut minor) = next.split_at(pos);
minor = &minor[1..];

match (major.parse::<u8>(), minor.parse::<u8>()) {
(Ok(major), Ok(minor)) => {
match Codename::try_from(UbuntuVersion { major, minor, patch: 0 }) {
Ok(codename) => {
return codename.release_timestamp() < install_time as u64
}
Err(()) => log::error!("version {} is invalid", next),
}
}
_ => log::error!(
"major ({}) and minor({}) version failed to parse as u8",
major, minor
),
}
}
None => log::error!("version {} is invalid", next),
},
Err(why) => log::error!("failed to get install time: {}", why),
}

false
}

/// Check if this release has already been dismissed
fn dismissed(next: &str) -> bool {
Path::new(DISMISSED).exists() && {
fs::read_to_string(DISMISSED).map(|dismissed| dismissed.as_str() == next).unwrap_or(false)
}
}
74 changes: 74 additions & 0 deletions src/cli2/release/check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use crate::{notify::notify, Error};
use chrono::{offset::TimeZone, Utc};
use pop_upgrade::{
client::{Client, Error as ClientError, ReleaseInfo},
daemon::{DismissEvent, DISMISSED, INSTALL_DATE},
misc,
release::eol::{EolDate, EolStatus},
};
use std::{convert::TryFrom, fs, path::Path};
use structopt::StructOpt;
use ubuntu_version::{Codename, Version as UbuntuVersion};

pub fn run(client: &Client) -> Result<(), ClientError> {
let mut buffer = String::new();
let info = client.release_check(false)?;

if atty::is(atty::Stream::Stdout) {
println!(
" Current Release: {}\n Next Release: {}\nNew Release Available: {}",
info.current,
info.next,
misc::format_build_number(info.build, &mut buffer)
);
} else if info.build >= 0 {
if info.is_lts
&& (super::dismissed(&info.next) || super::dismiss_by_timestamp(client, &info.next)?)
{
return Ok(());
}

let (summary, body) = notification_message(&info.current, &info.next);

let upgrade_panel = if &*info.current == "18.04" { "info-overview" } else { "upgrade" };

notify(&summary, &body, || {
let _ = exec::Command::new("gnome-control-center").arg(upgrade_panel).exec();
});
}

Ok(())
}

fn notification_message(current: &str, next: &str) -> (String, String) {
match EolDate::fetch() {
Ok(eol) => match eol.status() {
EolStatus::Exceeded => {
return (
fomat!("Support for Pop!_OS " (current) " has ended"),
fomat!(
"Security and application updates are no longer provided for Pop!_OS "
(current) ". Upgrade to Pop!_OS " (next) " to keep your computer secure."
),
);
}
EolStatus::Imminent => {
let (y, m, d) = eol.ymd;
return (
fomat!(
"Support for Pop!_OS " (current) " ends "
(Utc.ymd(y as i32, m, d).format("%B %-d, %Y"))
),
fomat!(
"This computer will soon stop receiving updates"
". Upgrade to Pop!_OS " (next) " to keep your computer secure."
),
);
}
EolStatus::Ok => (),
},
Err(why) => log::error!("failed to fetch EOL date: {}", why),
}

("Upgrade Available".into(), fomat!("Pop!_OS " (next) " is available to download"))
}
Loading

0 comments on commit 7e98f4c

Please sign in to comment.