Skip to content

Commit

Permalink
Add the jj api command
Browse files Browse the repository at this point in the history
  • Loading branch information
matts1 committed Apr 30, 2024
1 parent 701c7fd commit f994626
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
62 changes: 62 additions & 0 deletions cli/src/commands/api.rs
Original file line number Diff line number Diff line change
@@ -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))
}
4 changes: 4 additions & 0 deletions cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

mod abandon;
mod api;
mod backout;
#[cfg(feature = "bench")]
mod bench;
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit f994626

Please sign in to comment.