Skip to content

Commit

Permalink
Add tokio as async runtime
Browse files Browse the repository at this point in the history
- converting methods to be async
- remove `Cmd` trait, because it is tedious with async and it was not
  so much useful anyways
  • Loading branch information
eikek committed Jun 28, 2024
1 parent 5aaa0ca commit c36dd67
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 46 deletions.
129 changes: 129 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ reqwest = { version = "0.12.5", default-features = false, features = ["json", "b
serde = { version = "1.0.203", features = ["derive"] }
serde_json = "1.0.118"
snafu = { version = "0.8.3" }
tokio = { version = "1", features = ["full"] }
futures = { version = "0.3" }

[features]
default = ["reqwest/default-tls"] # link against system library
Expand Down
10 changes: 5 additions & 5 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ pub mod cmd;
pub mod opts;
pub mod sink;

use self::cmd::{Cmd, CmdError, Context};
use self::cmd::{CmdError, Context};
use self::opts::{MainOpts, SubCommand};
use clap::CommandFactory;
use serde::Serialize;
use std::fmt;

pub fn execute_cmd(opts: MainOpts) -> Result<(), CmdError> {
pub async fn execute_cmd(opts: MainOpts) -> Result<(), CmdError> {
let ctx = Context::new(&opts.common_opts)?;

log::info!("Running command: {:?}", opts.subcmd);
match &opts.subcmd {
SubCommand::Version(input) => input.exec(&ctx)?,
SubCommand::Version(input) => input.exec(&ctx).await?,
SubCommand::ShellCompletion(input) => {
let mut app = MainOpts::command();
input.print_completions(&mut app);
input.print_completions(&mut app).await;
}
SubCommand::Project(input) => input.exec(&ctx)?,
SubCommand::Project(input) => input.exec(&ctx).await?,
};
Ok(())
}
Expand Down
12 changes: 3 additions & 9 deletions src/cli/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,14 @@ use crate::httpclient::{self, proxy, Client};
use serde::Serialize;
use snafu::{ResultExt, Snafu};

pub trait Cmd {
type CmdError;

fn exec(&self, args: &Context) -> Result<(), Self::CmdError>;
}
const RENKULAB_IO: &str = "https://renkulab.io";

pub struct Context<'a> {
pub opts: &'a CommonOpts,
pub client: Client,
pub renku_url: String,
}

const RENKULABIO: &str = "https://renkulab.io";

impl Context<'_> {
pub fn new(opts: &CommonOpts) -> Result<Context, CmdError> {
let base_url = get_renku_url(opts);
Expand All @@ -35,7 +29,7 @@ impl Context<'_> {
}

/// A short hand for `Sink::write(self.format(), value)`
fn write_result<A: Sink + Serialize>(&self, value: A) -> Result<(), SinkError> {
async fn write_result<A: Sink + Serialize>(&self, value: A) -> Result<(), SinkError> {
let fmt = self.format();
Sink::write(fmt, &value)
}
Expand All @@ -58,7 +52,7 @@ fn get_renku_url(opts: &CommonOpts) -> String {
}
None => {
log::debug!("Use renku url: https://renkulab.io");
RENKULABIO.to_string()
RENKULAB_IO.to_string()
}
},
}
Expand Down
10 changes: 4 additions & 6 deletions src/cli/cmd/project.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod clone;

use super::{Cmd, Context};
use super::Context;
use clap::Parser;
use snafu::{ResultExt, Snafu};

Expand All @@ -16,12 +16,10 @@ pub struct Input {
pub subcmd: ProjectCommand,
}

impl Cmd for Input {
type CmdError = Error;

fn exec(&self, ctx: &Context) -> Result<(), Error> {
impl Input {
pub async fn exec<'a>(&self, ctx: &Context<'a>) -> Result<(), Error> {
match &self.subcmd {
ProjectCommand::Clone(input) => input.exec(ctx).context(CloneSnafu),
ProjectCommand::Clone(input) => input.exec(ctx).await.context(CloneSnafu),
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/cli/cmd/project/clone.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Cmd, Context};
use super::Context;

use clap::Parser;
use snafu::Snafu;
Expand All @@ -10,10 +10,8 @@ pub struct Input {
pub slug: String,
}

impl Cmd for Input {
type CmdError = Error;

fn exec(&self, _ctx: &Context) -> Result<(), Error> {
impl Input {
pub async fn exec<'a>(&self, _ctx: &Context<'a>) -> Result<(), Error> {
Ok(())
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli/cmd/shell_completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub enum GeneratorChoice {
}

impl Input {
pub fn print_completions(&self, app: &mut Command) {
pub async fn print_completions(&self, app: &mut Command) {
let binary = &self.binary;
match &self.shell {
GeneratorChoice::Bash => generate_completions(Shell::Bash, binary, app),
Expand Down
11 changes: 5 additions & 6 deletions src/cli/cmd/version.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Cmd, Context};
use super::Context;
use crate::cli::sink::Error as SinkError;
use crate::cli::sink::Sink;
use crate::cli::BuildInfo;
Expand All @@ -25,16 +25,15 @@ pub enum Error {
WriteResult { source: SinkError },
}

impl Cmd for Input {
type CmdError = Error;

fn exec(&self, ctx: &Context) -> Result<(), Error> {
impl Input {
pub async fn exec<'a>(&self, ctx: &Context<'a>) -> Result<(), Error> {
let result = ctx
.client
.version(ctx.opts.verbose > 1)
.await
.context(HttpClientSnafu)?;
let vinfo = Versions::create(result, &ctx.renku_url);
ctx.write_result(vinfo).context(WriteResultSnafu)?;
ctx.write_result(vinfo).await.context(WriteResultSnafu)?;
Ok(())
}
}
Expand Down
Loading

0 comments on commit c36dd67

Please sign in to comment.