From f99462622fd9b0fd043c8bd9e09311bed36c1412 Mon Sep 17 00:00:00 2001 From: Matt Stark Date: Mon, 29 Apr 2024 11:12:43 +1000 Subject: [PATCH] Add the jj api command --- Cargo.lock | 1 + cli/Cargo.toml | 1 + cli/src/commands/api.rs | 62 +++++++++++++++++++++++++++++++++++++++++ cli/src/commands/mod.rs | 4 +++ 4 files changed, 68 insertions(+) create mode 100644 cli/src/commands/api.rs diff --git a/Cargo.lock b/Cargo.lock index 989b73ffa2..1bded5786d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1888,6 +1888,7 @@ dependencies = [ "indoc", "insta", "itertools 0.12.1", + "jj-api", "jj-cli", "jj-lib", "libc", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 8d2b41c514..bd08b7b9f0 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -63,6 +63,7 @@ gix = { workspace = true } hex = { workspace = true } indexmap = { workspace = true } itertools = { workspace = true } +jj-api = { workspace = true } jj-lib = { workspace = true } maplit = { workspace = true } minus = { workspace = true } diff --git a/cli/src/commands/api.rs b/cli/src/commands/api.rs new file mode 100644 index 0000000000..9608d49f3d --- /dev/null +++ b/cli/src/commands/api.rs @@ -0,0 +1,62 @@ +// Copyright 2020 The Jujutsu Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use clap::arg; +use jj_lib::api::server::{start_api, GrpcOptions, StartupOptions}; +use jj_lib::api::servicer::Servicer; +use std::fmt::Debug; + + + + +use tracing::instrument; + +use crate::command_error::{CommandError, CommandErrorKind}; +use crate::commands::CommandHelper; +use crate::ui::Ui; + +#[derive(clap::Subcommand, Clone, Debug)] +pub enum ApiCommand { + Grpc(GrpcArgs), +} + +#[derive(clap::Args, Clone, Debug)] +pub struct GrpcArgs { + #[arg(long)] + port: u16, + + #[arg(long)] + web: bool, +} + +#[instrument(skip_all)] +pub(crate) fn cmd_api( + _ui: &mut Ui, + command: &CommandHelper, + subcommand: &ApiCommand, +) -> Result<(), CommandError> { + let startup_options = match subcommand { + ApiCommand::Grpc(args) => StartupOptions::Grpc(GrpcOptions { + port: args.port, + web: args.web, + }), + }; + // Running jj api from a non-jj repository is still valid, as the user can provide the repository path in each individual request. + let default_workspace_loader = command.workspace_loader().ok(); + start_api( + startup_options, + Servicer::new(default_workspace_loader.cloned()), + ) + .map_err(|e| CommandError::new(CommandErrorKind::Internal, e)) +} diff --git a/cli/src/commands/mod.rs b/cli/src/commands/mod.rs index 4007ca8259..ad6c1993f8 100644 --- a/cli/src/commands/mod.rs +++ b/cli/src/commands/mod.rs @@ -13,6 +13,7 @@ // limitations under the License. mod abandon; +mod api; mod backout; #[cfg(feature = "bench")] mod bench; @@ -69,6 +70,8 @@ use crate::ui::Ui; #[derive(clap::Parser, Clone, Debug)] enum Command { + #[command(subcommand)] + Api(api::ApiCommand), Abandon(abandon::AbandonArgs), Backout(backout::BackoutArgs), #[cfg(feature = "bench")] @@ -203,6 +206,7 @@ pub fn run_command(ui: &mut Ui, command_helper: &CommandHelper) -> Result<(), Co Command::Branch(sub_args) => branch::cmd_branch(ui, command_helper, sub_args), Command::Undo(sub_args) => operation::cmd_op_undo(ui, command_helper, sub_args), Command::Operation(sub_args) => operation::cmd_operation(ui, command_helper, sub_args), + Command::Api(sub_args) => api::cmd_api(ui, command_helper, sub_args), Command::Workspace(sub_args) => workspace::cmd_workspace(ui, command_helper, sub_args), Command::Sparse(sub_args) => sparse::cmd_sparse(ui, command_helper, sub_args), Command::Tag(sub_args) => tag::cmd_tag(ui, command_helper, sub_args),