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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use ansi_term::Color::{Blue, Green, Red, Yellow};
use chrono::{DateTime, Utc};
use configstore::{AppUI, Configstore};
use reqwest::blocking::Client;
use reqwest::header;
use serde_derive::{Deserialize, Serialize};
use std::time::Duration;

const REGISTRY_URL: &str = "https://crates.io";

#[cfg(test)]
use mockito;

fn get_base_url() -> String {
    #[cfg(not(test))]
    let url = format!("{}/api/v1/crates", REGISTRY_URL);
    #[cfg(test)]
    let url = format!("{}/api/v1/crates", mockito::server_url());
    url
}

#[derive(Debug, thiserror::Error)]
pub enum ErrorKind {
    #[error("Unable to find crate")]
    CrateDoesNotExist,
    #[error("Versions do not exist on the registry")]
    VersionDoesNotExistCratesIO,
    #[error("Error while parsing json: {0}")]
    UnableToParseJson(String),
    #[error("Error received from registry: {0}")]
    RegistryError(String),
    #[error("Unable to find config")]
    UnableToFindConfig,
}

#[derive(Deserialize, Debug, Clone)]
pub struct VersionResponse {
    versions: Option<Vec<Version>>,
    errors: Option<Vec<JsonError>>,
}

#[derive(Deserialize, Debug, Clone)]
struct JsonError {
    detail: String,
}

#[derive(Deserialize, Debug, Clone)]
struct Version {
    num: String,
}

fn get_latest_from_json(
    resp: &VersionResponse,
) -> std::result::Result<String, Box<dyn std::error::Error>> {
    if let Some(versions) = &resp.versions {
        match versions.first() {
            Some(version) => Ok(version.num.clone()),
            None => Err(ErrorKind::UnableToParseJson("Versions array is empty".to_string()).into()),
        }
    } else if let Some(errors) = &resp.errors {
        match errors.first() {
            Some(error) => Err(ErrorKind::RegistryError(error.detail.clone()).into()),
            None => Err(
                ErrorKind::UnableToParseJson("No errors in the errors array".to_string()).into(),
            ),
        }
    } else {
        Err(ErrorKind::UnableToParseJson(
            "Invalid json response, does not have versions or errors".to_string(),
        )
        .into())
    }
}

pub fn get_latest_version(
    crate_name: &str,
) -> std::result::Result<String, Box<dyn std::error::Error>> {
    let mut headers = header::HeaderMap::new();
    headers.insert(
        header::USER_AGENT,
        header::HeaderValue::from_static("Update-notifier (teshaq@mozilla.com)"),
    );
    let client = Client::builder().default_headers(headers).build()?;
    let base_url = get_base_url();
    let url = format!("{}/{}/versions", base_url, crate_name);
    let json_resp = match client.get(&url).send()?.json() {
        Ok(resp) => resp,
        Err(e) => return Err(ErrorKind::UnableToParseJson(e.to_string()).into()),
    };
    get_latest_from_json(&json_resp)
}

pub fn generate_notice(name: &str, current_version: &str, latest_version: &str) -> String {
    let line_1 = format!(
        "A new version of {} is available! {} → {}",
        Green.bold().paint(name),
        Red.bold().paint(current_version),
        Green.bold().paint(latest_version)
    );
    let line_2 = format!(
        "Use `{}` to install version {}",
        Blue.bold().paint(format!("cargo install {}", name)),
        Green.bold().paint(latest_version)
    );
    let line_3 = format!(
        "Check {} for more details",
        Yellow.paint(format!("{}/crates/{}", REGISTRY_URL, name))
    );
    format!(
        "\n───────────────────────────────────────────────────────\n
    {}
    {}
    {}
    \n───────────────────────────────────────────────────────\n",
        line_1, line_2, line_3
    )
}

fn print_notice(name: &str, current_version: &str, latest_version: &str) {
    print!("{}", generate_notice(name, current_version, latest_version));
}

#[derive(Deserialize, Serialize)]
struct Config {
    last_checked: DateTime<Utc>,
}

fn get_app_name(name: &str) -> String {
    let mut app_name: String = String::from(name);
    app_name.push_str("-update-notifier");
    #[cfg(test)]
    app_name.push_str("-test");
    app_name
}

fn update_time(date_time: DateTime<Utc>, name: &str) -> Result<(), Box<dyn std::error::Error>> {
    let config = Config {
        last_checked: date_time,
    };
    let config_store = Configstore::new(&get_app_name(name), AppUI::CommandLine)?;
    config_store.set("config", config)?;
    Ok(())
}

fn compare_with_latest(
    name: &str,
    current_version: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    let latest_version = get_latest_version(name)?;
    if latest_version != current_version {
        print_notice(name, current_version, &latest_version);
    }
    let date_time = Utc::now();
    update_time(date_time, name)
}

/// Validates current version of the crate
/// Takes the current name and version
/// Takes a std::time::Duration to determine the interval
///         So that it does not notify the user every run
/// Prints directly to stdout (Will probably change to be more asynchrounos)
pub fn check_version(
    name: &str,
    current_version: &str,
    interval: Duration,
) -> Result<(), Box<dyn std::error::Error>> {
    let date_time_now = Utc::now();
    let config_store = Configstore::new(&get_app_name(name), AppUI::CommandLine)?;
    match config_store.get::<Config>("config") {
        Ok(config) => {
            let prev_time = config.last_checked;
            let duration_interval = chrono::Duration::from_std(interval)?;
            if date_time_now.signed_duration_since(prev_time) >= duration_interval {
                compare_with_latest(name, current_version)
            } else {
                Ok(())
            }
        }
        Err(_) => compare_with_latest(name, current_version),
    }
}

#[cfg(test)]
mod tests;