Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A couple of debugging experience fixes #11

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion clients/rust/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ impl ClientBuilder {
self
}

#[cfg(feature = "admin")]
/// If the secret_token is an admin key, the client will act on behalf of
/// the project passed here.
/// This method is for cronback admin use only. For normal users, the
Expand Down
8 changes: 3 additions & 5 deletions cronback-cli/src/admin/api_keys/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use async_trait::async_trait;
use clap::Parser;
use cronback_api_model::admin::APIKeyMetaData;

use crate::admin::{AdminOptions, RunAdminCommand};
use crate::args::CommonOptions;
use crate::emitln;
use crate::{emitln, Command};

#[derive(Clone, Debug, Parser)]
pub struct Create {
Expand All @@ -14,7 +13,7 @@ pub struct Create {
}

#[async_trait]
impl RunAdminCommand for Create {
impl Command for Create {
async fn run<
A: tokio::io::AsyncWrite + Send + Sync + Unpin,
B: tokio::io::AsyncWrite + Send + Sync + Unpin,
Expand All @@ -23,9 +22,8 @@ impl RunAdminCommand for Create {
out: &mut tokio::io::BufWriter<A>,
_err: &mut tokio::io::BufWriter<B>,
common_options: &CommonOptions,
admin_options: &AdminOptions,
) -> Result<()> {
let client = admin_options.new_admin_client(common_options)?;
let client = common_options.new_client()?;

let response = cronback_client::api_keys::gen(
&client,
Expand Down
8 changes: 3 additions & 5 deletions cronback-cli/src/admin/api_keys/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ use async_trait::async_trait;
use clap::Parser;
use prettytable::{row, Table};

use crate::admin::{AdminOptions, RunAdminCommand};
use crate::args::CommonOptions;
use crate::emitln;
use crate::{emitln, Command};

#[derive(Clone, Debug, Parser)]
pub struct List {}

#[async_trait]
impl RunAdminCommand for List {
impl Command for List {
async fn run<
A: tokio::io::AsyncWrite + Send + Sync + Unpin,
B: tokio::io::AsyncWrite + Send + Sync + Unpin,
Expand All @@ -20,9 +19,8 @@ impl RunAdminCommand for List {
out: &mut tokio::io::BufWriter<A>,
_err: &mut tokio::io::BufWriter<B>,
common_options: &CommonOptions,
admin_options: &AdminOptions,
) -> Result<()> {
let client = admin_options.new_admin_client(common_options)?;
let client = common_options.new_client()?;

let response = cronback_client::api_keys::list(&client).await?;

Expand Down
13 changes: 5 additions & 8 deletions cronback-cli/src/admin/api_keys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use anyhow::Result;
use async_trait::async_trait;
use clap::clap_derive::Parser;

use super::{AdminOptions, RunAdminCommand};
use crate::args::CommonOptions;
use crate::Command;

mod create;
mod list;
Expand All @@ -21,7 +21,7 @@ pub enum ApiKeysCommand {
}

#[async_trait]
impl RunAdminCommand for ApiKeysCommand {
impl Command for ApiKeysCommand {
async fn run<
A: tokio::io::AsyncWrite + Send + Sync + Unpin,
B: tokio::io::AsyncWrite + Send + Sync + Unpin,
Expand All @@ -30,17 +30,14 @@ impl RunAdminCommand for ApiKeysCommand {
out: &mut tokio::io::BufWriter<A>,
err: &mut tokio::io::BufWriter<B>,
common_options: &CommonOptions,
admin_options: &AdminOptions,
) -> Result<()> {
match self {
| ApiKeysCommand::List(c) => {
c.run(out, err, common_options, admin_options).await
}
| ApiKeysCommand::List(c) => c.run(out, err, common_options).await,
| ApiKeysCommand::Create(c) => {
c.run(out, err, common_options, admin_options).await
c.run(out, err, common_options).await
}
| ApiKeysCommand::Revoke(c) => {
c.run(out, err, common_options, admin_options).await
c.run(out, err, common_options).await
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions cronback-cli/src/admin/api_keys/revoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use anyhow::Result;
use async_trait::async_trait;
use clap::Parser;

use crate::admin::{AdminOptions, RunAdminCommand};
use crate::args::CommonOptions;
use crate::confirm::confirm_or_abort;
use crate::emitln;
use crate::{emitln, Command};

#[derive(Clone, Debug, Parser)]
pub struct Revoke {
Expand All @@ -18,7 +17,7 @@ pub struct Revoke {
}

#[async_trait]
impl RunAdminCommand for Revoke {
impl Command for Revoke {
async fn run<
A: tokio::io::AsyncWrite + Send + Sync + Unpin,
B: tokio::io::AsyncWrite + Send + Sync + Unpin,
Expand All @@ -27,15 +26,14 @@ impl RunAdminCommand for Revoke {
out: &mut tokio::io::BufWriter<A>,
_err: &mut tokio::io::BufWriter<B>,
common_options: &CommonOptions,
admin_options: &AdminOptions,
) -> Result<()> {
confirm_or_abort!(
self,
"Are you sure you want to revoke the key '{}'? All API calls with \
this key will start failing.",
self.id
);
let client = admin_options.new_admin_client(common_options)?;
let client = common_options.new_client()?;

let response =
cronback_client::api_keys::revoke(&client, &self.id).await?;
Expand Down
56 changes: 5 additions & 51 deletions cronback-cli/src/admin/mod.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1,13 @@
use anyhow::{Context, Result};
use anyhow::Result;
use async_trait::async_trait;
use clap::clap_derive::Parser;
use cronback_client::ClientBuilder;

use crate::args::CommonOptions;
use crate::client::WrappedClient;
use crate::Command;

mod api_keys;
mod projects;

const CRONBACK_PROJECT_ID_VAR: &str = "CRONBACK_PROJECT_ID";

#[async_trait]
pub trait RunAdminCommand {
async fn run<
A: tokio::io::AsyncWrite + Send + Sync + Unpin,
B: tokio::io::AsyncWrite + Send + Sync + Unpin,
>(
&self,
out: &mut tokio::io::BufWriter<A>,
err: &mut tokio::io::BufWriter<B>,
common_options: &CommonOptions,
admin_options: &AdminOptions,
) -> Result<()>;
}

#[derive(Parser, Debug, Clone)]
pub struct AdminOptions {
// Unfortunately, we can't make this required **and** global at the same
// time. See [https://github.com/clap-rs/clap/issues/1546]
#[arg(long, value_name = "PROJECT_ID", env(CRONBACK_PROJECT_ID_VAR))]
/// The client will act on behalf of this project
project_id: String,
}

impl AdminOptions {
pub fn new_admin_client(
&self,
opts: &CommonOptions,
) -> Result<WrappedClient> {
let base_url = opts.base_url();
let inner = ClientBuilder::new()
.base_url(base_url.clone())
.context("Error while parsing base url")?
.secret_token(opts.secret_token.clone())
.on_behalf_of(self.project_id.clone())
.build()?;
Ok(WrappedClient {
common_options: opts.clone(),
inner,
})
}
}

#[derive(Parser, Debug, Clone)]
pub enum AdminCommand {
/// Commands for api key management. This subcommand requires admin
Expand All @@ -69,7 +24,7 @@ pub enum AdminCommand {
}

#[async_trait]
impl RunAdminCommand for AdminCommand {
impl Command for AdminCommand {
async fn run<
A: tokio::io::AsyncWrite + Send + Sync + Unpin,
B: tokio::io::AsyncWrite + Send + Sync + Unpin,
Expand All @@ -78,14 +33,13 @@ impl RunAdminCommand for AdminCommand {
out: &mut tokio::io::BufWriter<A>,
err: &mut tokio::io::BufWriter<B>,
common_options: &CommonOptions,
admin_options: &AdminOptions,
) -> Result<()> {
match self {
| AdminCommand::ApiKeys { command } => {
command.run(out, err, common_options, admin_options).await
command.run(out, err, common_options).await
}
| AdminCommand::Projects { command } => {
command.run(out, err, common_options, admin_options).await
command.run(out, err, common_options).await
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions cronback-cli/src/admin/projects/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ use anyhow::Result;
use async_trait::async_trait;
use clap::Parser;

use crate::admin::{AdminOptions, RunAdminCommand};
use crate::args::CommonOptions;
use crate::emitln;
use crate::{emitln, Command};

#[derive(Clone, Debug, Parser)]
pub struct Create;

#[async_trait]
impl RunAdminCommand for Create {
impl Command for Create {
async fn run<
A: tokio::io::AsyncWrite + Send + Sync + Unpin,
B: tokio::io::AsyncWrite + Send + Sync + Unpin,
Expand All @@ -19,9 +18,8 @@ impl RunAdminCommand for Create {
out: &mut tokio::io::BufWriter<A>,
_err: &mut tokio::io::BufWriter<B>,
common_options: &CommonOptions,
admin_options: &AdminOptions,
) -> Result<()> {
let client = admin_options.new_admin_client(common_options)?;
let client = common_options.new_client()?;

let response = cronback_client::projects::create(&client).await?;

Expand Down
7 changes: 3 additions & 4 deletions cronback-cli/src/admin/projects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use anyhow::Result;
use async_trait::async_trait;
use clap::clap_derive::Parser;

use super::{AdminOptions, RunAdminCommand};
use crate::args::CommonOptions;
use crate::Command;

mod create;

Expand All @@ -14,7 +14,7 @@ pub enum ProjectsCommand {
}

#[async_trait]
impl RunAdminCommand for ProjectsCommand {
impl Command for ProjectsCommand {
async fn run<
A: tokio::io::AsyncWrite + Send + Sync + Unpin,
B: tokio::io::AsyncWrite + Send + Sync + Unpin,
Expand All @@ -23,11 +23,10 @@ impl RunAdminCommand for ProjectsCommand {
out: &mut tokio::io::BufWriter<A>,
err: &mut tokio::io::BufWriter<B>,
common_options: &CommonOptions,
admin_options: &AdminOptions,
) -> Result<()> {
match self {
| ProjectsCommand::Create(c) => {
c.run(out, err, common_options, admin_options).await
c.run(out, err, common_options).await
}
}
}
Expand Down
31 changes: 20 additions & 11 deletions cronback-cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ use url::Url;

#[cfg(feature = "admin")]
use crate::admin;
#[cfg(feature = "admin")]
use crate::admin::RunAdminCommand;
use crate::client::WrappedClient;
use crate::ui::FancyToString;
use crate::{runs, triggers, whoami, Command};

const CRONBACK_SECRET_TOKEN_VAR: &str = "CRONBACK_SECRET_TOKEN";
#[cfg(feature = "admin")]
const CRONBACK_PROJECT_ID_VAR: &str = "CRONBACK_PROJECT_ID";

#[derive(Parser, Debug, Clone)]
/// Command-line utility to manage cronback projects
Expand Down Expand Up @@ -51,6 +51,11 @@ pub struct CommonOptions {
/// variable is not set
pub secret_token: String,

#[cfg(feature = "admin")]
#[arg(long, value_name = "PROJECT_ID", env(CRONBACK_PROJECT_ID_VAR))]
/// The client will act on behalf of this project
project_id: Option<String>,

#[arg(long, global = true)]
/// Displays a table with meta information about the response
pub show_meta: bool,
Expand Down Expand Up @@ -78,8 +83,6 @@ pub enum CliCommand {
/// Set of commands that require admin privillages.
#[cfg(feature = "admin")]
Admin {
#[clap(flatten)]
admin_options: admin::AdminOptions,
#[command(subcommand)]
command: admin::AdminCommand,
},
Expand Down Expand Up @@ -129,11 +132,18 @@ impl CommonOptions {

pub fn new_client(&self) -> Result<WrappedClient> {
let base_url = self.base_url();
let inner = ClientBuilder::new()

#[allow(unused_mut)]
let mut builder = ClientBuilder::new()
.base_url(base_url.clone())
.context("Error while parsing base url")?
.secret_token(self.secret_token.clone())
.build()?;
.secret_token(self.secret_token.clone());

#[cfg(feature = "admin")]
if let Some(project_id) = &self.project_id {
builder = builder.on_behalf_of(project_id.clone());
}
MohamedBassem marked this conversation as resolved.
Show resolved Hide resolved
let inner = builder.build()?;
Ok(WrappedClient {
common_options: self.clone(),
inner,
Expand Down Expand Up @@ -188,10 +198,9 @@ impl Command for CliCommand {
command.run(out, err, common_options).await
}
#[cfg(feature = "admin")]
| CliCommand::Admin {
admin_options,
command,
} => command.run(out, err, common_options, admin_options).await,
| CliCommand::Admin { command } => {
command.run(out, err, common_options).await
}
| CliCommand::WhoAmI(c) => c.run(out, err, common_options).await,
}
}
Expand Down
2 changes: 2 additions & 0 deletions services/cronback-api-srv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ tonic = { workspace = true }
tower-http = { workspace = true, features = ["cors"] }
tracing = { workspace = true }
ulid = { workspace = true }
once_cell = { workspace = true }

# Unique Dependencies
axum-extra = { version = "0.7", features = ["query"] }
Expand All @@ -47,3 +48,4 @@ sha2 = "0.10.6"
uuid = { version = "1.2.2", features = ["v4"] }
validator = { version = "0.16.0", features = ["derive"] }
hyper = "0.14.26"
regex = "1.9.1"
Loading
Loading