Added --version.

This commit is contained in:
Antoine POPINEAU 2020-07-02 08:24:35 +02:00
parent 35e5445060
commit e74d8c2b1d
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
4 changed files with 37 additions and 0 deletions

View File

@ -3,6 +3,7 @@ name = "tuigreet"
version = "0.1.0"
authors = ["Antoine POPINEAU <antoine.popineau@appscho.com>"]
edition = "2018"
build = "build.rs"
[dependencies]
getopts = "0.2.21"

View File

@ -9,6 +9,7 @@ Usage: tuigreet [OPTIONS]
Options:
-h, --help show this usage information
-v, --version print version information
-c, --cmd COMMAND command to run
-w, --width WIDTH width of the main prompt (default: 80)
-i, --issue show the host's issue file

24
build.rs Normal file
View File

@ -0,0 +1,24 @@
use std::{env, error::Error, process::Command};
fn main() {
let version = get_version().unwrap_or_else(|_| String::from("unknown"));
println!("cargo:rustc-env=VERSION={}", version);
println!("cargo:rustc-env=TARGET={}", env::var("TARGET").unwrap());
}
fn get_version() -> Result<String, Box<dyn Error>> {
let tag = Command::new("git").args(&["describe", "--tags", "--abbrev=0"]).output()?;
let tag = match tag.status.code() {
Some(0) => String::from_utf8(tag.stdout)?,
_ => "0.0.0".to_string(),
};
let count = String::from_utf8(Command::new("git").args(&["rev-list", "--count", "HEAD"]).output()?.stdout)?;
let commit = String::from_utf8(Command::new("git").args(&["rev-parse", "--short", "HEAD"]).output()?.stdout)?;
let version = format!("{}.r{}.{}", tag.trim(), count.trim(), commit.trim());
match version.as_str() {
"0.0.0.r." => Err("could not retrieve version".into()),
version => Ok(version.to_string()),
}
}

View File

@ -124,6 +124,7 @@ impl Greeter {
let mut opts = Options::new();
opts.optflag("h", "help", "show this usage information");
opts.optflag("v", "version", "print version information");
opts.optopt("c", "cmd", "command to run", "COMMAND");
opts.optopt("w", "width", "width of the main prompt (default: 80)", "WIDTH");
opts.optflag("i", "issue", "show the host's issue file");
@ -146,6 +147,10 @@ impl Greeter {
print_usage(opts);
std::process::exit(0);
}
if self.config().opt_present("version") {
print_version();
std::process::exit(0);
}
let socket = env::var("GREETD_SOCK");
if socket.is_err() {
@ -180,3 +185,9 @@ impl Greeter {
fn print_usage(opts: Options) {
eprint!("{}", opts.usage("Usage: tuigreet [OPTIONS]"));
}
fn print_version() {
println!("tuigreet {} ({})", env!("VERSION"), env!("TARGET"));
println!("Copyright (C) 2020 Antoine POPINEAU <https://github.com/apognu/tuigreet>.");
println!("Licensed under GPLv3 (GNU GPL version 3 or later).");
}