From 28d1588e73e4ae66cfb9104fd8bf1b1b1de00147 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa?= Date: Sun, 10 Feb 2019 15:46:51 +0100 Subject: [PATCH] Show version in admin panel --- build.rs | 24 +++++++++++++++++++++--- src/api/admin.rs | 9 ++++++--- src/static/templates/admin/base.hbs | 19 +++++++++++-------- 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/build.rs b/build.rs index 1a77b27b..e8b4c9a7 100644 --- a/build.rs +++ b/build.rs @@ -6,6 +6,10 @@ fn main() { fn run(args: &[&str]) -> Result { let out = Command::new(args[0]).args(&args[1..]).output()?; + if !out.status.success() { + use std::io::{Error, ErrorKind}; + return Err(Error::new(ErrorKind::Other, "Command not successful")); + } Ok(String::from_utf8(out.stdout).unwrap().trim().to_string()) } @@ -13,8 +17,10 @@ fn run(args: &[&str]) -> Result { fn read_git_info() -> Result<(), std::io::Error> { // The exact tag for the current commit, can be empty when // the current commit doesn't have an associated tag - let exact_tag = run(&["git", "describe", "--abbrev=0", "--tags", "--exact-match"])?; - println!("cargo:rustc-env=GIT_EXACT_TAG={}", exact_tag); + let exact_tag = run(&["git", "describe", "--abbrev=0", "--tags", "--exact-match"]).ok(); + if let Some(ref exact) = exact_tag { + println!("cargo:rustc-env=GIT_EXACT_TAG={}", exact); + } // The last available tag, equal to exact_tag when // the current commit is tagged @@ -27,13 +33,25 @@ fn read_git_info() -> Result<(), std::io::Error> { // The current git commit hash let rev = run(&["git", "rev-parse", "HEAD"])?; - let rev_short = rev.get(..12).unwrap_or_default(); + let rev_short = rev.get(..8).unwrap_or_default(); println!("cargo:rustc-env=GIT_REV={}", rev_short); + // Combined version + let version = if let Some(exact) = exact_tag { + exact + } else if &branch != "master" { + format!("{}-{} ({})", last_tag, rev_short, branch) + } else { + format!("{}-{}", last_tag, rev_short) + }; + println!("cargo:rustc-env=GIT_VERSION={}", version); + // To access these values, use: // env!("GIT_EXACT_TAG") // env!("GIT_LAST_TAG") // env!("GIT_BRANCH") // env!("GIT_REV") + // env!("GIT_VERSION") + Ok(()) } diff --git a/src/api/admin.rs b/src/api/admin.rs index ae1f3dfa..1e7acdda 100644 --- a/src/api/admin.rs +++ b/src/api/admin.rs @@ -40,12 +40,13 @@ const COOKIE_NAME: &str = "BWRS_ADMIN"; const ADMIN_PATH: &str = "/admin"; const BASE_TEMPLATE: &str = "admin/base"; +const VERSION: Option<&str> = option_env!("GIT_VERSION"); #[get("/", rank = 2)] fn admin_login(flash: Option) -> ApiResult> { // If there is an error, show it let msg = flash.map(|msg| format!("{}: {}", msg.name(), msg.msg())); - let json = json!({"page_content": "admin/login", "error": msg}); + let json = json!({"page_content": "admin/login", "version": VERSION, "error": msg}); // Return the page let text = CONFIG.render_template(BASE_TEMPLATE, &json)?; @@ -94,16 +95,18 @@ fn _validate_token(token: &str) -> bool { #[derive(Serialize)] struct AdminTemplateData { - users: Vec, page_content: String, + version: Option<&'static str>, + users: Vec, config: Value, } impl AdminTemplateData { fn new(users: Vec) -> Self { Self { - users, page_content: String::from("admin/page"), + version: VERSION, + users, config: CONFIG.prepare_json(), } } diff --git a/src/static/templates/admin/base.hbs b/src/static/templates/admin/base.hbs index a718ed34..9d0e9162 100644 --- a/src/static/templates/admin/base.hbs +++ b/src/static/templates/admin/base.hbs @@ -8,14 +8,14 @@ - - - - + + + +