Skip to content

Commit

Permalink
✅ adding a check and inform about used CLI version
Browse files Browse the repository at this point in the history
  • Loading branch information
gerardcl committed Jun 12, 2023
1 parent 18773cf commit cb4daa5
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 6 deletions.
50 changes: 49 additions & 1 deletion Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rodalies-cli"
version = "1.0.3"
version = "1.1.0"
edition = "2021"
authors = ["Gerard C.L. <[email protected]>"]
license = "BSD-3-Clause"
Expand All @@ -18,7 +18,9 @@ tokio = { version = "1.28", features = ["full"] }
scraper = "0.16"
chrono = { version = "0.4", features = []}
prettytable-rs = "0.10"
serde_json = "1.0"

[dev-dependencies]
assert_cmd = "2.0"
predicates = "3.0"
tokio-test = "0.4"
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ After a release a github action updates it with x86_64 built binaries for:
Run the following command to get the binary into your bin folder or tune it as you like:

```bash
curl -LO "https://github.com/gerardcl/rodalies-cli/releases/download/1.0.3/rodalies-cli-linux-amd64" && \
curl -LO "https://github.com/gerardcl/rodalies-cli/releases/download/1.1.0/rodalies-cli-linux-amd64" && \
chmod +x rodalies-cli-linux-amd64 && mv rodalies-cli-linux-amd64 /usr/local/bin/rodalies-cli
```

Expand All @@ -28,7 +28,7 @@ chmod +x rodalies-cli-linux-amd64 && mv rodalies-cli-linux-amd64 /usr/local/bin/
Run the following command to get the binary into your bin folder or tune it as you like:

```bash
curl -LO "https://github.com/gerardcl/rodalies-cli/releases/download/1.0.3/rodalies-cli-darwin-amd64" && \
curl -LO "https://github.com/gerardcl/rodalies-cli/releases/download/1.1.0/rodalies-cli-darwin-amd64" && \
chmod +x rodalies-cli-darwin-amd64 && mv rodalies-cli-darwin-amd64 /usr/local/bin/rodalies-cli
```

Expand All @@ -39,7 +39,7 @@ chmod +x rodalies-cli-darwin-amd64 && mv rodalies-cli-darwin-amd64 /usr/local/bi
Run the following command to get the binary into your bin folder or tune it as you like:

```bash
curl -LO "https://github.com/gerardcl/rodalies-cli/releases/download/1.0.3/rodalies-cli-windows-amd64.exe" && \
curl -LO "https://github.com/gerardcl/rodalies-cli/releases/download/1.1.0/rodalies-cli-windows-amd64.exe" && \
chmod +x rodalies-cli-windows-amd64.exe && mv rodalies-cli-windows-amd64.exe /mingw64/bin/rodalies-cli.exe
```

Expand Down Expand Up @@ -131,6 +131,7 @@ $ rodalies-cli -d 9 -m 9
```bash
$ rodalies-cli -s gir
🚂 Rodalies CLI 📅 Today's date is 02/08/2022
✅ You are running the latest rodalies-cli, version 1.1.0! yayy
✨ Interactive mode enabled: 'false'
🔍 Searching stations that contain the text: 'gir'
+--------------+------------+
Expand All @@ -141,6 +142,7 @@ $ rodalies-cli -s gir
$ rodalies-cli -s si
🚂 Rodalies CLI 📅 Today's date is 02/08/2022
✅ You are running the latest rodalies-cli, version 1.1.0! yayy
✨ Interactive mode enabled: 'false'
🔍 Searching stations that contain the text: 'si'
+------------------------+------------+
Expand All @@ -157,6 +159,7 @@ $ rodalies-cli -s si
```bash
$ rodalies-cli -f 79300 -t 79202
🚂 Rodalies CLI 📅 Today's date is 02/08/2022
✅ You are running the latest rodalies-cli, version 1.1.0! yayy
✨ Interactive mode enabled: 'false'
🔍 Searching timetable for date 02/08/2022
📆 Listing timetable with 0 transfers
Expand Down Expand Up @@ -204,6 +207,7 @@ If the timetable requires a transfer you will also see it:
```bash
$ rodalies-cli -f 79300 -t 71701
🚂 Rodalies CLI 📅 Today's date is 02/08/2022
✅ You are running the latest rodalies-cli, version 1.1.0! yayy
✨ Interactive mode enabled: 'false'
🔍 Searching timetable for date 02/08/2022
📆 Listing timetable with 1 transfers
Expand Down Expand Up @@ -243,6 +247,7 @@ $ rodalies-cli -f 79300 -t 71701
```bash
$ rodalies-cli -f 79300 -t 72503
🚂 Rodalies CLI 📅 Today's date is 02/08/2022
✅ You are running the latest rodalies-cli, version 1.1.0! yayy
✨ Interactive mode enabled: 'false'
🔍 Searching timetable for date 02/08/2022
📆 Listing timetable with 2 transfers
Expand Down
73 changes: 73 additions & 0 deletions src/config/check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use clap::crate_version;
use serde_json::Value;
use std::{error::Error, time::Duration};
use surf::{Client, Config, Url};

struct IndexResponse {
name: String,
version: String,
}

pub async fn check_rodalies_version() {
let online_crate_state = get_last_crate_index_response().await;
match online_crate_state {
Ok(state) => match state.version.as_str() {
crate_version!() => println!("✅ You are running the latest {}, version {}! yayy", state.name, state.version),
_ => println!("🤷 You are using an outdated version of rodalies-cli ({}), please upgrade to latest ({})", crate_version!(), state.version)
},
_ => {
println!("🕵️ Could not check if using latest version...dismissing check, but if you keep seeing this message please open an issue.")
},
};
}

/// Returns the HTML from`https://raw.githubusercontent.com/rust-lang/crates.io-index/master/ro/da/rodalies-cli`, if possible.
async fn get_last_crate_index_response() -> Result<IndexResponse, Box<dyn Error>> {
let raw_githubcontent_base_url = "https://raw.githubusercontent.com";

let client: Client = Config::new()
.set_base_url(Url::parse(raw_githubcontent_base_url)?)
.set_timeout(Some(Duration::from_secs(5)))
.try_into()?;

let mut response = client
.get("/rust-lang/crates.io-index/master/ro/da/rodalies-cli")
.header(
"User-Agent",
format!(
"rodalies-cli/{} (github.com/gerardcl/rodalies-cli)",
crate_version!()
),
)
.await?;

let body_response = response.body_string().await?;

let last_line = body_response.lines().last().unwrap();

let index_response: Value = serde_json::from_str(last_line)?;

Ok(IndexResponse {
name: index_response["name"].to_string().replace('\"', ""),
version: index_response["vers"].to_string().replace('\"', ""),
})
}

#[cfg(test)]
mod tests {
use super::*;

macro_rules! run_async {
($e:expr) => {
tokio_test::block_on($e)
};
}

#[test]
fn test_get_index_returns_index_response() {
let response = run_async!(get_last_crate_index_response());
let index = response.unwrap();
assert_eq!(index.name, "rodalies-cli");
assert!(!index.version.is_empty());
}
}
3 changes: 3 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
/// `cli` provides the methods to initialize the CLI input (args) and output (table).
pub mod cli;

/// `check` provides the methods to check and inform about latest published online version of the `rodalies-cli` and the one being used by the user.
pub mod check;
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::error::Error;

use chrono::{Datelike, Local};
use rodalies_cli::config::check::check_rodalies_version;
use rodalies_cli::config::cli::{init_cli, interactive_mode};
use rodalies_cli::rodalies::client::init_client;
use rodalies_cli::rodalies::interactive::search_interactive;
Expand All @@ -19,6 +20,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
dt.year()
);

check_rodalies_version().await;

if !interactive_mode(&args).unwrap() {
if args.contains_id("search") {
// search station
Expand Down
19 changes: 18 additions & 1 deletion src/rodalies/client.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use clap::crate_version;
use scraper::Html;
use std::{error::Error, time::Duration};
use surf::{Client, Config, Response, StatusCode, Url};
Expand All @@ -15,7 +16,16 @@ pub fn init_client() -> Client {

/// Returns the HTML body parsed of the main search page.
pub async fn get_search_page(client: &Client) -> Result<Html, Box<dyn Error>> {
let mut response = client.get("/en/horaris").await?;
let mut response = client
.get("/en/horaris")
.header(
"User-Agent",
format!(
"rodalies-cli/{} (github.com/gerardcl/rodalies-cli)",
crate_version!()
),
)
.await?;

let body_response = get_page_body(&mut response).await?;

Expand All @@ -31,6 +41,13 @@ pub async fn get_timetable_page(
) -> Result<Html, Box<dyn Error>> {
let mut response = client
.post("/en/horaris")
.header(
"User-Agent",
format!(
"rodalies-cli/{} (github.com/gerardcl/rodalies-cli)",
crate_version!()
),
)
.content_type("application/x-www-form-urlencoded")
.body_string(format!(
"origen={}&desti={}&dataViatge={}&horaIni=00&lang=en&cercaRodalies=true&tornada=false",
Expand Down

0 comments on commit cb4daa5

Please sign in to comment.