Skip to content

Commit

Permalink
feat!: introduce subcommand into the cli of cdviz-collector
Browse files Browse the repository at this point in the history
  • Loading branch information
davidB committed Dec 4, 2024
1 parent f9544a2 commit 9ba42ec
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
4 changes: 3 additions & 1 deletion cdviz-collector/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@ ENV \
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT="http://127.0.0.1:4317" \
OTEL_TRACES_SAMPLER="always_off"
HEALTHCHECK NONE
CMD ["/cdviz-collector"]
#see https://stackoverflow.com/questions/21553353/what-is-the-difference-between-cmd-and-entrypoint-in-a-dockerfile
ENTRYPOINT ["/cdviz-collector"]
CMD ["connect"]
37 changes: 28 additions & 9 deletions cdviz-collector/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod sources;
use std::path::PathBuf;

use cdevents_sdk::CDEvent;
use clap::Parser;
use clap::{Args, Parser, Subcommand};
use clap_verbosity_flag::Verbosity;
use errors::{Error, Result};
use futures::future::TryJoinAll;
Expand All @@ -22,17 +22,32 @@ static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;

// TODO add options (or subcommand) to `check-configuration` (regardless of enabled), `configuration-dump` (after consolidation (with filter or not enabled) and exit or not),
// TODO add options to overide config from cli arguments (like from env)
#[derive(Debug, Clone, clap::Parser)]
#[derive(Debug, Clone, Parser)]
pub(crate) struct Cli {
#[command(flatten)]
verbose: clap_verbosity_flag::Verbosity,

#[command(subcommand)]
command: Commands,
}

#[derive(Debug, Clone, Subcommand)]
enum Commands {
/// connect sources to sinks
#[command(arg_required_else_help = true)]
Connect(ConnectArgs),
}

#[derive(Debug, Clone, Args)]
#[command(args_conflicts_with_subcommands = true)]
#[command(flatten_help = true)]
struct ConnectArgs {
/// The configuration file to use.
#[clap(long = "config", env("CDVIZ_COLLECTOR_CONFIG"))]
config: Option<PathBuf>,
/// The directory to use as the working directory.
#[clap(short = 'C', long = "directory")]
directory: Option<PathBuf>,

#[command(flatten)]
verbose: clap_verbosity_flag::Verbosity,
}

type Sender<T> = tokio::sync::broadcast::Sender<T>;
Expand Down Expand Up @@ -69,8 +84,6 @@ fn init_log(verbose: &Verbosity) -> Result<TracingGuard> {
//TODO add garcefull shutdown
//TODO use logfmt
//TODO use verbosity to configure tracing & log, but allow override and finer control with RUST_LOG & CDVIZ_COLLECTOR_LOG (higher priority)
//TODO add a `enabled: bool` field as part of the config of each sources & sinks
//TODO provide default config, and default values for some config fields
//TODO document the architecture and the configuration
//TODO add transformers ( eg file/event info, into cdevents) for sources
//TODO integrations with cloudevents (sources & sink)
Expand All @@ -79,9 +92,15 @@ fn init_log(verbose: &Verbosity) -> Result<TracingGuard> {
async fn main() -> Result<()> {
let cli = Cli::parse();
let _guard = init_log(&cli.verbose)?;
let config = config::Config::from_file(cli.config)?;
match cli.command {
Commands::Connect(args) => connect(args).await,
}
}

async fn connect(args: ConnectArgs) -> Result<()> {
let config = config::Config::from_file(args.config)?;

if let Some(dir) = cli.directory {
if let Some(dir) = args.directory {
std::env::set_current_dir(dir)?;
}

Expand Down
2 changes: 1 addition & 1 deletion cdviz-collector/taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ tasks:

run:
cmds:
- CDVIZ_COLLECTOR__SINKS__DEBUG__ENABLED=true cargo run -- -vv --config ./examples/assets/cdviz-collector.toml --directory ./examples/assets
- CDVIZ_COLLECTOR__SINKS__DEBUG__ENABLED=true cargo run -- connect -vv --config ./examples/assets/cdviz-collector.toml --directory ./examples/assets

ci:
desc: set of tasks run by CI
Expand Down
2 changes: 1 addition & 1 deletion demos/taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ tasks:
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "http://127.0.0.1:4317"
CDVIZ_COLLECTOR__SINKS__DEBUG__ENABLED: "false"
cmds:
- ../../cdviz-collector/target/debug/cdviz-collector -vv --config ./cdviz-collector.toml
- ../../cdviz-collector/target/debug/cdviz-collector connect -vv --config ./cdviz-collector.toml

0 comments on commit 9ba42ec

Please sign in to comment.