Skip to content

Commit 9489cf1

Browse files
lib: store the commit information inside the enum
This semplify the code to report the upgrade status by storing the commit information inside the enum itself. Changelog-Added: lib: store the commit information inside the enum Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent 13df654 commit 9489cf1

File tree

4 files changed

+14
-15
lines changed

4 files changed

+14
-15
lines changed

coffee_cmd/src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ async fn main() -> Result<(), CoffeeError> {
6767
Ok(res) => {
6868
spinner.and_then(|splinner| Some(splinner.finish()));
6969
match res.status {
70-
UpgradeStatus::UpToDate => {
70+
UpgradeStatus::UpToDate(_, _) => {
7171
term::info!("Remote repository `{}` is already up to date!", res.repo)
7272
}
73-
UpgradeStatus::Updated => {
73+
UpgradeStatus::Updated(_, _) => {
7474
term::success!(
7575
"Remote repository `{}` was successfully upgraded!",
7676
res.repo

coffee_github/src/repository.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::any::Any;
22

33
use async_trait::async_trait;
4-
use chrono::{TimeZone, Utc};
54
use coffee_lib::types::response::CoffeeUpgrade;
5+
use coffee_lib::types::response::UpgradeStatus;
66
use git2;
77
use log::debug;
88
use tokio::fs::File;
@@ -254,13 +254,12 @@ impl Repository for Github {
254254
}
255255
// pull the changes from the repository
256256
let status = git_upgrade(&self.url.path_string, &self.branch).await?;
257-
// update the git information
258-
// (git HEAD and date of the last commit)
259-
let repo = git2::Repository::open(&self.url.path_string)
260-
.map_err(|err| error!("{}", err.message()))?;
261-
let (commit, date) = get_repo_info!(repo);
262-
self.git_head = Some(commit.clone());
263-
self.last_activity = Some(date.clone());
257+
// FIXME: add this code inside a method of the enum, to be able to call this as `status.date()`
258+
let (_, date) = match status {
259+
UpgradeStatus::UpToDate(ref commit, ref date) => (commit, date.clone()),
260+
UpgradeStatus::Updated(ref commit, ref date) => (commit, date.clone()),
261+
};
262+
self.last_activity = Some(date);
264263
Ok(CoffeeUpgrade {
265264
repo: self.name(),
266265
status,

coffee_github/src/utils.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ pub async fn git_upgrade(path: &str, branch: &str) -> Result<UpgradeStatus, Coff
3535
let cmd = format!("git pull origin/{branch}");
3636
sh!(path, cmd);
3737

38-
let (upstream_commit, _) = get_repo_info!(repo);
38+
let (upstream_commit, date) = get_repo_info!(repo);
3939

4040
if local_commit == upstream_commit {
41-
Ok(UpgradeStatus::UpToDate)
41+
Ok(UpgradeStatus::UpToDate(upstream_commit, date))
4242
} else {
43-
Ok(UpgradeStatus::Updated)
43+
Ok(UpgradeStatus::Updated(upstream_commit, date))
4444
}
4545
}

coffee_lib/src/types/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ pub mod response {
8989

9090
#[derive(Debug, Serialize, Deserialize)]
9191
pub enum UpgradeStatus {
92-
UpToDate,
93-
Updated,
92+
UpToDate(String, String),
93+
Updated(String, String),
9494
}
9595

9696
#[derive(Debug, Serialize, Deserialize)]

0 commit comments

Comments
 (0)