Skip to content

Commit

Permalink
Add "bundle serve" command
Browse files Browse the repository at this point in the history
  • Loading branch information
let-def committed May 24, 2024
1 parent 9cd8325 commit 000e277
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/bin/tectonic/v2cli/commands/bundle.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use clap::{Parser, Subcommand};
use std::io::Write;
use tectonic::{
config::PersistentConfig,
docmodel::{DocumentExt, DocumentSetupOptions},
errors::Result,
io::{InputFeatures, OpenResult},
tt_note,
};
use tectonic_bundles::Bundle;
Expand Down Expand Up @@ -60,20 +62,26 @@ enum BundleCommands {
#[command(name = "search")]
/// Filter the list of filenames contained in the bundle
Search(BundleSearchCommand),

#[command(name = "serve")]
/// Dump the contents of files requested on standard input
Serve(BundleServeCommand),
}

impl TectonicCommand for BundleCommand {
fn customize(&self, cc: &mut CommandCustomizations) {
match &self.command {
BundleCommands::Cat(c) => c.customize(cc),
BundleCommands::Search(c) => c.customize(cc),
BundleCommands::Serve(c) => c.customize(cc),

Check warning on line 76 in src/bin/tectonic/v2cli/commands/bundle.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/tectonic/v2cli/commands/bundle.rs#L76

Added line #L76 was not covered by tests
}
}

fn execute(self, config: PersistentConfig, status: &mut dyn StatusBackend) -> Result<i32> {
match self.command {
BundleCommands::Cat(c) => c.execute(config, status),
BundleCommands::Search(c) => c.execute(config, status),
BundleCommands::Serve(c) => c.execute(config, status),

Check warning on line 84 in src/bin/tectonic/v2cli/commands/bundle.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/tectonic/v2cli/commands/bundle.rs#L84

Added line #L84 was not covered by tests
}
}
}
Expand Down Expand Up @@ -138,3 +146,69 @@ impl BundleSearchCommand {
Ok(0)
}
}

#[derive(Debug, Eq, PartialEq, Parser)]
struct BundleServeCommand {
/// Use only resource files cached locally
#[arg(short = 'C', long)]
only_cached: bool,
}

impl BundleServeCommand {
fn customize(&self, cc: &mut CommandCustomizations) {
cc.always_stderr = true;
}

Check warning on line 160 in src/bin/tectonic/v2cli/commands/bundle.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/tectonic/v2cli/commands/bundle.rs#L158-L160

Added lines #L158 - L160 were not covered by tests

fn execute(self, config: PersistentConfig, status: &mut dyn StatusBackend) -> Result<i32> {
let mut bundle = get_a_bundle(config, self.only_cached, status)?;
let mut filename = String::new();
let stdin = std::io::stdin();
let mut stdout = std::io::stdout();

Check warning on line 166 in src/bin/tectonic/v2cli/commands/bundle.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/tectonic/v2cli/commands/bundle.rs#L162-L166

Added lines #L162 - L166 were not covered by tests
loop {
stdout.flush()?;
filename.clear();
match stdin.read_line(&mut filename) {
Err(error) => {
eprintln!("error: {error}");
return Ok(1);
}
Ok(0) => return Ok(0),

Check warning on line 175 in src/bin/tectonic/v2cli/commands/bundle.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/tectonic/v2cli/commands/bundle.rs#L168-L175

Added lines #L168 - L175 were not covered by tests
Ok(_) => {
let name = filename.trim_end();
let error = match bundle.input_path(name, status) {
OpenResult::Err(e) => e,
OpenResult::Ok(path) => {
let mut path = path.as_os_str().as_encoded_bytes();
let size = path.len() as u64;
stdout.write_all(&[b'P'])?;
stdout.write_all(&size.to_le_bytes())?;
let copied = std::io::copy(&mut path, &mut stdout)?;
assert!(size == copied);

Check warning on line 186 in src/bin/tectonic/v2cli/commands/bundle.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/tectonic/v2cli/commands/bundle.rs#L177-L186

Added lines #L177 - L186 were not covered by tests
continue;
}

Check warning on line 188 in src/bin/tectonic/v2cli/commands/bundle.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/tectonic/v2cli/commands/bundle.rs#L188

Added line #L188 was not covered by tests
OpenResult::NotAvailable => {
match bundle.input_open_name(name, status).must_exist() {
Ok(mut t) => {
let size = t.get_size()? as u64;
stdout.write_all(&[b'C'])?;
stdout.write_all(&size.to_le_bytes())?;
let copied = std::io::copy(&mut t, &mut stdout)?;
assert!(size == copied);

Check warning on line 196 in src/bin/tectonic/v2cli/commands/bundle.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/tectonic/v2cli/commands/bundle.rs#L190-L196

Added lines #L190 - L196 were not covered by tests
continue;
}
Err(e) => e,

Check warning on line 199 in src/bin/tectonic/v2cli/commands/bundle.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/tectonic/v2cli/commands/bundle.rs#L198-L199

Added lines #L198 - L199 were not covered by tests
}
}

Check warning on line 201 in src/bin/tectonic/v2cli/commands/bundle.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/tectonic/v2cli/commands/bundle.rs#L201

Added line #L201 was not covered by tests
};
let text = error.to_string();
let bytes = text.as_bytes();
let size = bytes.len() as u64;
stdout.write_all(&[b'E'])?;
stdout.write_all(&size.to_le_bytes())?;
stdout.write_all(&bytes)?;
}

Check warning on line 209 in src/bin/tectonic/v2cli/commands/bundle.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/tectonic/v2cli/commands/bundle.rs#L203-L209

Added lines #L203 - L209 were not covered by tests
};
stdout.flush().unwrap();

Check warning on line 211 in src/bin/tectonic/v2cli/commands/bundle.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/tectonic/v2cli/commands/bundle.rs#L211

Added line #L211 was not covered by tests
}
}

Check warning on line 213 in src/bin/tectonic/v2cli/commands/bundle.rs

View check run for this annotation

Codecov / codecov/patch

src/bin/tectonic/v2cli/commands/bundle.rs#L213

Added line #L213 was not covered by tests
}

0 comments on commit 000e277

Please sign in to comment.