Skip to content

Commit

Permalink
feat: Updated CLI syntax to use subcommands
Browse files Browse the repository at this point in the history
This is being done to support migrations directly in the binary.

BREAKING CHANGE: Changing CLI syntax to use subcommands.
  • Loading branch information
Isawan committed Feb 19, 2024
1 parent 8107be6 commit 66cf17d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/src/starting-terrashine.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ A TLS terminating reverse proxy hosted is on `example.com` in this setup.
Note that the `/mirror/v1/` path is required in the URL to allow the backend server to serve up redirects correctly.

```
AWS_REGION=eu-west-1 AWS_ACCESS_KEY_ID=xxx AWS_SECRET_ACCESS_KEY=xxx RUST_LOG=info ./terrashine --s3-bucket-name terrashine-example-test-1111 --http-redirect-url https://example.com/mirror/v1/
AWS_REGION=eu-west-1 AWS_ACCESS_KEY_ID=xxx AWS_SECRET_ACCESS_KEY=xxx RUST_LOG=info ./terrashine server --s3-bucket-name terrashine-example-test-1111 --http-redirect-url https://example.com/mirror/v1/
```
10 changes: 5 additions & 5 deletions integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::{
use crate::util::copy_dir;
use reqwest::StatusCode;
use sqlx::{pool::PoolOptions, postgres::PgConnectOptions, Postgres};
use terrashine::{self, config::Args};
use terrashine::{self, config::ServerArgs};
use tokio::select;
use tracing_test::traced_test;
use url::Url;
Expand All @@ -22,7 +22,7 @@ use uuid::Uuid;
#[sqlx::test]
fn test_server_startup(_: PoolOptions<Postgres>, db_options: PgConnectOptions) {
let prefix = format!("{}/", Uuid::new_v4());
let config = Args {
let config = ServerArgs {
database_url: db_options,
database_pool: 3,
s3_bucket_name: "terrashine".to_string(),
Expand All @@ -35,7 +35,7 @@ fn test_server_startup(_: PoolOptions<Postgres>, db_options: PgConnectOptions) {
};
let cancellation_token = tokio_util::sync::CancellationToken::new();
let (tx, rx) = tokio::sync::oneshot::channel();
let handle = tokio::spawn(terrashine::run(
let handle = tokio::spawn(terrashine::run_server(
config,
None,
cancellation_token.child_token(),
Expand All @@ -61,7 +61,7 @@ fn test_server_startup(_: PoolOptions<Postgres>, db_options: PgConnectOptions) {
#[sqlx::test]
fn test_end_to_end_terraform_flow(_: PoolOptions<Postgres>, db_options: PgConnectOptions) {
let prefix = format!("{}/", Uuid::new_v4());
let config = Args {
let config = ServerArgs {
database_url: db_options,
database_pool: 3,
s3_bucket_name: "terrashine".to_string(),
Expand All @@ -74,7 +74,7 @@ fn test_end_to_end_terraform_flow(_: PoolOptions<Postgres>, db_options: PgConnec
};
let cancellation_token = tokio_util::sync::CancellationToken::new();
let (tx, rx) = tokio::sync::oneshot::channel();
let handle = tokio::spawn(terrashine::run(
let handle = tokio::spawn(terrashine::run_server(
config,
None,
cancellation_token.child_token(),
Expand Down
6 changes: 3 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use tracing::Level;

use crate::http::api::APIState;
use crate::{
config::Args,
config::ServerArgs,
credhelper::{database::DatabaseCredentials, CredentialHelper},
http::artifacts::artifacts_handler,
http::healthcheck::healthcheck_handler,
Expand All @@ -28,14 +28,14 @@ pub(crate) struct AppState<C> {
pub(crate) http_client: reqwest::Client,
pub(crate) db_client: Pool<Postgres>,
pub(crate) registry_client: RegistryClient<DatabaseCredentials>,
pub(crate) config: Args,
pub(crate) config: ServerArgs,
pub(crate) refresher_tx: mpsc::Sender<RefreshRequest>,
pub(crate) credentials: C,
}

impl<C> AppState<C> {
pub(crate) fn new(
config: Args,
config: ServerArgs,
s3: aws_sdk_s3::Client,
db: Pool<Postgres>,
http: reqwest::Client,
Expand Down
9 changes: 8 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ fn parse_humantime(s: &str) -> Result<Duration, anyhow::Error> {

#[derive(Parser, Debug, Clone)]
#[command(author, version, about, long_about = None)]
pub struct Args {
pub enum Args {
Server(ServerArgs),
}

#[derive(clap::Args, Debug, Clone)]
#[command(author, version, about, long_about = None)]
pub struct ServerArgs {
/// Socket to listen on
///
/// The host and port to bind the HTTP service
Expand Down Expand Up @@ -122,6 +128,7 @@ mod tests {
async fn test_clap_cli_parsing() {
let _ = Args::try_parse_from([
"./terrashine",
"server",
"--http-redirect-url",
"https://example.com/",
"--s3-bucket-name",
Expand Down
13 changes: 12 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use app::AppState;
use aws_config::BehaviorVersion;
use axum::extract::Request;
use axum_prometheus::metrics_exporter_prometheus::PrometheusHandle;
use config::Args;
use config::{Args, ServerArgs};
use hyper::body::Incoming;
use hyper_util::{
rt::{TokioExecutor, TokioIo},
Expand Down Expand Up @@ -74,6 +74,17 @@ pub async fn run(
metric_handle: Option<PrometheusHandle>,
cancel: CancellationToken,
startup: Sender<StartUpNotify>,
) -> Result<(), ()> {
match config {
Args::Server(args) => run_server(args, metric_handle, cancel, startup).await,

Check warning on line 79 in src/lib.rs

View check run for this annotation

Codecov / codecov/patch

src/lib.rs#L77-L79

Added lines #L77 - L79 were not covered by tests
}
}

Check warning on line 81 in src/lib.rs

View check run for this annotation

Codecov / codecov/patch

src/lib.rs#L81

Added line #L81 was not covered by tests

pub async fn run_server(
config: ServerArgs,
metric_handle: Option<PrometheusHandle>,
cancel: CancellationToken,
startup: Sender<StartUpNotify>,
) -> Result<(), ()> {
let (tx, rx) = mpsc::channel(10000);

Expand Down

0 comments on commit 66cf17d

Please sign in to comment.