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

cli: add native-query create subcommand to create native query configuration #105

Merged
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
109 changes: 108 additions & 1 deletion Cargo.lock

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

9 changes: 7 additions & 2 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ name = "mongodb-cli-plugin"
edition = "2021"
version.workspace = true

[features]
native-query-subcommand = []

[dependencies]
configuration = { path = "../configuration" }
mongodb-agent-common = { path = "../mongodb-agent-common" }
Expand All @@ -11,16 +14,18 @@ mongodb-support = { path = "../mongodb-support" }

anyhow = "1.0.80"
clap = { version = "4.5.1", features = ["derive", "env"] }
deriving_via = "^1.6.1"
futures-util = "0.3.28"
indexmap = { workspace = true }
itertools = { workspace = true }
ndc-models = { workspace = true }
nom = "^7.1.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0.113", features = ["raw_value"] }
thiserror = "1.0.57"
tokio = { version = "1.36.0", features = ["full"] }

[dev-dependencies]
test-helpers = { path = "../test-helpers" }

pretty_assertions = "1"
proptest = "1"
test-helpers = { path = "../test-helpers" }
18 changes: 18 additions & 0 deletions crates/cli/src/exit_codes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ExitCode {
CouldNotReadAggregationPipeline,
CouldNotReadConfiguration,
ErrorWriting,
RefusedToOverwrite,
}

impl From<ExitCode> for i32 {
fn from(value: ExitCode) -> Self {
match value {
ExitCode::CouldNotReadAggregationPipeline => 201,
ExitCode::CouldNotReadConfiguration => 202,
ExitCode::ErrorWriting => 204,
ExitCode::RefusedToOverwrite => 203,
}
}
}
2 changes: 1 addition & 1 deletion crates/cli/src/introspection/sampling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ async fn sample_schema_from_collection(
}
}

fn make_object_type(
pub fn make_object_type(
object_type_name: &ndc_models::ObjectTypeName,
document: &Document,
is_collection_type: bool,
Expand Down
23 changes: 19 additions & 4 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
//! The interpretation of the commands that the CLI can handle.

mod exit_codes;
mod introspection;
mod logging;

#[cfg(feature = "native-query-subcommand")]
mod native_query;

use std::path::PathBuf;

use clap::{Parser, Subcommand};

// Exported for use in tests
pub use introspection::type_from_bson;
use mongodb_agent_common::state::ConnectorState;
use mongodb_agent_common::state::try_init_state_from_uri;
#[cfg(feature = "native-query-subcommand")]
pub use native_query::native_query_from_pipeline;

#[derive(Debug, Clone, Parser)]
pub struct UpdateArgs {
Expand All @@ -28,23 +34,32 @@ pub struct UpdateArgs {
pub enum Command {
/// Update the configuration by introspecting the database, using the configuration options.
Update(UpdateArgs),

#[cfg(feature = "native-query-subcommand")]
#[command(subcommand)]
NativeQuery(native_query::Command),
}

pub struct Context {
pub path: PathBuf,
pub connector_state: ConnectorState,
pub connection_uri: Option<String>,
}

/// Run a command in a given directory.
pub async fn run(command: Command, context: &Context) -> anyhow::Result<()> {
match command {
Command::Update(args) => update(context, &args).await?,

#[cfg(feature = "native-query-subcommand")]
Command::NativeQuery(command) => native_query::run(context, command).await?,
};
Ok(())
}

/// Update the configuration in the current directory by introspecting the database.
async fn update(context: &Context, args: &UpdateArgs) -> anyhow::Result<()> {
let connector_state = try_init_state_from_uri(context.connection_uri.as_ref()).await?;

let configuration_options =
configuration::parse_configuration_options_file(&context.path).await;
// Prefer arguments passed to cli, and fallback to the configuration file
Expand Down Expand Up @@ -72,7 +87,7 @@ async fn update(context: &Context, args: &UpdateArgs) -> anyhow::Result<()> {

if !no_validator_schema {
let schemas_from_json_validation =
introspection::get_metadata_from_validation_schema(&context.connector_state).await?;
introspection::get_metadata_from_validation_schema(&connector_state).await?;
configuration::write_schema_directory(&context.path, schemas_from_json_validation).await?;
}

Expand All @@ -81,7 +96,7 @@ async fn update(context: &Context, args: &UpdateArgs) -> anyhow::Result<()> {
sample_size,
all_schema_nullable,
config_file_changed,
&context.connector_state,
&connector_state,
&existing_schemas,
)
.await?;
Expand Down
13 changes: 3 additions & 10 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
//! This is intended to be automatically downloaded and invoked via the Hasura CLI, as a plugin.
//! It is unlikely that end-users will use it directly.

use anyhow::anyhow;
use std::env;
use std::path::PathBuf;

use clap::{Parser, ValueHint};
use mongodb_agent_common::state::{try_init_state_from_uri, DATABASE_URI_ENV_VAR};
use mongodb_agent_common::state::DATABASE_URI_ENV_VAR;
use mongodb_cli_plugin::{run, Command, Context};

/// The command-line arguments.
Expand All @@ -17,6 +16,7 @@ pub struct Args {
/// The path to the configuration. Defaults to the current directory.
#[arg(
long = "context-path",
short = 'p',
env = "HASURA_PLUGIN_CONNECTOR_CONTEXT_PATH",
value_name = "DIRECTORY",
value_hint = ValueHint::DirPath
Expand Down Expand Up @@ -46,16 +46,9 @@ pub async fn main() -> anyhow::Result<()> {
Some(path) => path,
None => env::current_dir()?,
};
let connection_uri = args.connection_uri.ok_or(anyhow!(
"Missing environment variable {}",
DATABASE_URI_ENV_VAR
))?;
let connector_state = try_init_state_from_uri(&connection_uri)
.await
.map_err(|e| anyhow!("Error initializing MongoDB state {}", e))?;
let context = Context {
path,
connector_state,
connection_uri: args.connection_uri,
};
run(args.subcommand, &context).await?;
Ok(())
Expand Down
Loading
Loading