Skip to content

Commit

Permalink
Use tokio
Browse files Browse the repository at this point in the history
  • Loading branch information
catuhana committed Jul 3, 2024
1 parent 92b7bdf commit c92f32b
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 8 deletions.
30 changes: 30 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,3 +20,4 @@ node-semver = { git = "https://github.com/catuhana/node-semver-rs", features = [
] }
reqwest = { version = "0.12.5", features = ["blocking", "json"] }
serde = { version = "1.0.203", features = ["derive"] }
tokio = { version = "1.38.0", features = ["rt-multi-thread", "sync", "macros"] }
5 changes: 3 additions & 2 deletions src/cli/install.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use anyhow::Context;
use clap::Args;
use reqwest::blocking as reqwest;

use super::NueCommand;

Expand All @@ -24,16 +23,18 @@ pub struct CommandArguments {
impl NueCommand for CommandArguments {
type Arguments = Self;

fn run(&self) -> anyhow::Result<()> {
async fn run(&self) -> anyhow::Result<()> {
let progress_bar = indicatif::ProgressBar::new_spinner();
progress_bar.enable_steady_tick(std::time::Duration::from_millis(120));

progress_bar.set_message("Fetching releases...");
let releases_json: Vec<types::node::Release> = reqwest::get(
"https://nodejs.org/download/release/index.json",
)
.await
.context("Failed to fetch releases from `https://nodejs.org/download/release/index.json`")?
.json()
.await
.context("Failed to parse releases JSON")?;

progress_bar.set_message("Filtering releases based on input...");
Expand Down
5 changes: 3 additions & 2 deletions src/cli/list.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use anyhow::Context;
use clap::Args;
use inquire::Select;
use reqwest::blocking as reqwest;

use super::NueCommand;

Expand Down Expand Up @@ -29,16 +28,18 @@ pub struct CommandArguments {
impl NueCommand for CommandArguments {
type Arguments = Self;

fn run(&self) -> anyhow::Result<()> {
async fn run(&self) -> anyhow::Result<()> {
let progress_bar = indicatif::ProgressBar::new_spinner();
progress_bar.enable_steady_tick(std::time::Duration::from_millis(120));

progress_bar.set_message("Fetching releases...");
let releases_json: Vec<types::node::Release> = reqwest::get(
"https://nodejs.org/download/release/index.json",
)
.await
.context("Failed to fetch releases from `https://nodejs.org/download/release/index.json`")?
.json()
.await
.context("Failed to parse releases JSON")?;

progress_bar.set_message("Filtering releases...");
Expand Down
2 changes: 1 addition & 1 deletion src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod list;
pub trait NueCommand {
type Arguments: Args;

fn run(&self) -> anyhow::Result<()>;
async fn run(&self) -> anyhow::Result<()>;
}

#[derive(Parser, Debug)]
Expand Down
7 changes: 4 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ mod cli;
mod exts;
mod types;

fn main() -> anyhow::Result<()> {
#[tokio::main]
async fn main() -> anyhow::Result<()> {
match cli::Cli::parse().subcommand {
cli::Subcommands::Install(install) => install.run(),
cli::Subcommands::List(list) => list.run(),
cli::Subcommands::Install(install) => install.run().await,
cli::Subcommands::List(list) => list.run().await,
}?;

Ok(())
Expand Down

0 comments on commit c92f32b

Please sign in to comment.