From bd99fbc4c479300bb44db345ec34d9b60949dc40 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Fri, 25 Aug 2023 02:30:52 -0400 Subject: [PATCH] Get the versions of emacs and org-mode and write them to stdout. --- Cargo.toml | 5 +++++ docker/Dockerfile | 4 ++-- src/main.rs | 16 ++++++++++++---- src/parse.rs | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 680c09b..286c3b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,8 @@ serde = { version = "1.0.183", features = ["derive"] } tokio = { version = "1.30.0", default-features = false, features = ["macros", "process", "rt", "rt-multi-thread"] } tower = "0.4.13" tower-http = { version = "0.4.3", features = ["fs", "set-header"] } + +[profile.release-lto] +inherits = "release" +lto = true +strip = "symbols" diff --git a/docker/Dockerfile b/docker/Dockerfile index 7d91bec..997661f 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -30,14 +30,14 @@ RUN apk add --no-cache musl-dev RUN mkdir /root/org-investigation WORKDIR /root/org-investigation COPY . . -RUN CARGO_TARGET_DIR=/target cargo build --release +RUN CARGO_TARGET_DIR=/target cargo build --profile release-lto FROM alpine:3.17 AS run RUN apk add --no-cache ncurses gnutls COPY --from=build-emacs /root/dist/ / COPY --from=build-org-mode /root/dist/ / -COPY --from=build-org-investigation /target/release/org_ownership_investigation /usr/bin/ +COPY --from=build-org-investigation /target/release-lto/org_ownership_investigation /usr/bin/ COPY static /opt/org-investigation/static WORKDIR /opt/org-investigation CMD ["/usr/bin/org_ownership_investigation"] diff --git a/src/main.rs b/src/main.rs index 85a5ef0..da62709 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,11 +4,13 @@ use axum::http::HeaderValue; use axum::response::IntoResponse; use axum::{http::StatusCode, routing::post, Json, Router}; use owner_tree::build_owner_tree; -use parse::emacs_parse_org_document; +use parse::{emacs_parse_org_document, get_emacs_version}; use tower::ServiceBuilder; use tower_http::services::{ServeDir, ServeFile}; use tower_http::set_header::SetResponseHeaderLayer; +use crate::parse::get_org_mode_version; + mod error; mod owner_tree; mod parse; @@ -16,7 +18,7 @@ mod rtrim_iterator; mod sexp; #[tokio::main] -async fn main() { +async fn main() -> Result<(), Box> { let static_files_service = { let serve_dir = ServeDir::new("static").not_found_service(ServeFile::new("static/index.html")); @@ -32,9 +34,15 @@ async fn main() { .route("/parse", post(parse_org_mode)) .fallback_service(static_files_service); - let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); + let emacs_version = get_emacs_version().await?; + let org_mode_version = get_org_mode_version().await?; + println!("Using emacs version: {}", emacs_version.trim()); + println!("Using org-mode version: {}", org_mode_version.trim()); + + let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await?; println!("Listening on port 3000. Pop open your browser to http://127.0.0.1:3000/ ."); - axum::serve(listener, app).await.unwrap(); + axum::serve(listener, app).await?; + Ok(()) } async fn parse_org_mode(body: String) -> Result { diff --git a/src/parse.rs b/src/parse.rs index bdf3d25..8b61dc9 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -51,3 +51,40 @@ where } output } + +pub async fn get_emacs_version() -> Result> { + let elisp_script = r#"(progn + (message "%s" (version)) +)"#; + let mut cmd = Command::new("emacs"); + let proc = cmd + .arg("-q") + .arg("--no-site-file") + .arg("--no-splash") + .arg("--batch") + .arg("--eval") + .arg(elisp_script); + + let out = proc.output().await?; + out.status.exit_ok()?; + Ok(String::from_utf8(out.stderr)?) +} + +pub async fn get_org_mode_version() -> Result> { + let elisp_script = r#"(progn + (org-mode) + (message "%s" (org-version nil t nil)) +)"#; + let mut cmd = Command::new("emacs"); + let proc = cmd + .arg("-q") + .arg("--no-site-file") + .arg("--no-splash") + .arg("--batch") + .arg("--eval") + .arg(elisp_script); + + let out = proc.output().await?; + out.status.exit_ok()?; + Ok(String::from_utf8(out.stderr)?) +}