Add --version switch

Add --version switch, which prints a short hash of the current
commit. Fix #612
This commit is contained in:
Jan Likar 2015-11-16 04:37:08 +01:00
parent f88345c431
commit 6e063a1430

View file

@ -23,6 +23,7 @@ use rustfmt::config::Config;
use std::env;
use std::fs::{self, File};
use std::io::{self, Read, Write};
use std::process::Command;
use std::path::{Path, PathBuf};
use getopts::{Matches, Options};
@ -33,6 +34,8 @@ enum Operation {
Format(Vec<PathBuf>, WriteMode),
/// Print the help message.
Help,
// Print version information
Version,
/// Print detailed configuration help.
ConfigHelp,
/// Invalid program input, including reason.
@ -82,6 +85,7 @@ fn update_config(config: &mut Config, matches: &Matches) {
fn execute() -> i32 {
let mut opts = Options::new();
opts.optflag("h", "help", "show this message");
opts.optflag("", "version", "show version information");
opts.optflag("v", "verbose", "show progress");
opts.optopt("",
"write-mode",
@ -111,6 +115,10 @@ fn execute() -> i32 {
print_usage(&opts, "");
0
}
Operation::Version => {
print_version();
0
}
Operation::ConfigHelp => {
Config::print_docs();
0
@ -168,6 +176,18 @@ fn print_usage(opts: &Options, reason: &str) {
println!("{}", opts.usage(&reason));
}
fn print_version() {
let cmd = Command::new("git")
.arg("rev-parse")
.arg("--short")
.arg("HEAD")
.output();
match cmd {
Ok(output) => print!("{}", String::from_utf8(output.stdout).unwrap()),
Err(e) => panic!("Unable te get version: {}", e),
}
}
fn determine_operation(matches: &Matches) -> Operation {
if matches.opt_present("h") {
return Operation::Help;
@ -177,6 +197,10 @@ fn determine_operation(matches: &Matches) -> Operation {
return Operation::ConfigHelp;
}
if matches.opt_present("version") {
return Operation::Version;
}
// if no file argument is supplied, read from stdin
if matches.free.len() == 0 {