Skip to content

Commit

Permalink
feat(CLI): Add serve command
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Jun 20, 2021
1 parent e39d3e2 commit 5faeb3b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
15 changes: 7 additions & 8 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,17 @@ pub enum Command {
Open(OpenCommand),
Close(CloseCommand),
Show(ShowCommand),
#[structopt(aliases = &["project"])]
Projects(projects::cli::Command),

// Module-specific commands defined in the `stencila` library
#[structopt(aliases = &["project"])]
Projects(projects::cli::Command),
#[structopt(aliases = &["document", "docs", "doc"])]
Documents(documents::cli::Command),

#[structopt(aliases = &["plugin"])]
Plugins(plugins::cli::Command),

Config(config::cli::Command),

Upgrade(upgrade::cli::Args),
Serve(serve::cli::Command),
}
#[derive(Debug)]
pub struct Context {
Expand Down Expand Up @@ -117,6 +115,7 @@ pub async fn run_command(
Command::Plugins(command) => plugins::cli::run(command).await,
Command::Config(command) => config::cli::run(command).await,
Command::Upgrade(command) => upgrade::cli::run(command).await,
Command::Serve(command) => command.run(documents).await,
};
render::render(context.interactive, formats, result?)
}
Expand Down Expand Up @@ -833,11 +832,11 @@ mod interact {
}
}
Err(ReadlineError::Interrupted) => {
tracing::info!("Ctrl-C pressed, interrupting current command");
// TODO
tracing::info!("Ctrl+C pressed, use Ctrl+D to end session");
// TODO Cancel the current tak, if it is cancellable
}
Err(ReadlineError::Eof) => {
tracing::info!("Ctrl-D pressed, ending session");
tracing::info!("Ctrl+D pressed, ending session");
break;
}
Err(error) => bail!(error),
Expand Down
48 changes: 25 additions & 23 deletions rust/src/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,8 @@ pub mod config {

#[cfg(feature = "cli")]
pub mod cli {
use crate::cli::display;

use super::*;
use structopt::StructOpt;

Expand All @@ -757,7 +759,7 @@ pub mod cli {
setting = structopt::clap::AppSettings::DeriveDisplayOrder,
setting = structopt::clap::AppSettings::ColoredHelp
)]
pub struct Args {
pub struct Command {
/// The URL to serve on (defaults to `ws://127.0.0.1:9000`)
#[structopt(env = "STENCILA_URL")]
url: Option<String>,
Expand All @@ -771,28 +773,28 @@ pub mod cli {
insecure: bool,
}

pub async fn run(
args: Args,
documents: &mut Documents,
config: &config::ServeConfig,
) -> Result<()> {
let Args { url, key, insecure } = args;

let url = url.or_else(|| Some(config.url.clone()));
let key = key.or_else(|| config.key.clone());
let insecure = insecure || config.insecure;

super::serve(
documents,
url,
if insecure {
Some("insecure".to_string())
} else {
key
},
)
.await?;
impl Command {
pub async fn run(self, documents: &mut Documents) -> display::Result {
let Command { url, key, insecure } = self;

let config = &crate::config::lock().await.serve;

let url = url.or_else(|| Some(config.url.clone()));
let key = key.or_else(|| config.key.clone());
let insecure = insecure || config.insecure;

Ok(())
super::serve(
documents,
url,
if insecure {
Some("insecure".to_string())
} else {
key
},
)
.await?;

display::nothing()
}
}
}

0 comments on commit 5faeb3b

Please sign in to comment.