nicer output for `fif -V` and `fif --version`

This commit is contained in:
Lynne Megido 2021-06-14 16:29:41 +10:00
parent 95956f8e30
commit f8dcdf8a3c
Signed by: lynnesbian
GPG Key ID: F0A184B5213D9F90
5 changed files with 43 additions and 5 deletions

3
.gitignore vendored
View File

@ -8,4 +8,5 @@ fif_*
!clippy.sh
cargo-timing*.html
todo.txt
/pkg/*
/pkg/*
/out

View File

@ -10,6 +10,7 @@
<excludeFolder url="file://$MODULE_DIR$/awful" />
<excludeFolder url="file://$MODULE_DIR$/.mypy_cache" />
<excludeFolder url="file://$MODULE_DIR$/pkg" />
<excludeFolder url="file://$MODULE_DIR$/out" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />

View File

@ -2,6 +2,13 @@
Dates are given in YYYY-MM-DD format.
## v0.3
### v0.3.2 (2021-mm-dd)
#### Bugfixes
- Fixed PowerShell output regression introduced in v0.2.13, whoops
#### Other
- Nicer version output: `fif -V` reports "fif v0.3.2" (instead of just "fif 0.3.2" without the "v"), and `fif --version`
reports `fif v0.3.2 (XDG-Mime backend)`, or whatever backend you're using.
### v0.3.1 (2021-07-06)
#### Features
- Added JSON output support via `-o json`

View File

@ -10,6 +10,7 @@ use cfg_if::cfg_if;
use snailquote::escape;
use crate::findings::ScanError;
use crate::parameters::VERSION;
use crate::{Findings, BACKEND};
use itertools::Itertools;
@ -49,9 +50,6 @@ macro_rules! writablesln {
};
}
/// The current version of fif, as defined in Cargo.toml.
const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");
#[doc(hidden)]
type Entries<'a> = [Result<Findings<'a>, ScanError<'a>>];

View File

@ -1,8 +1,10 @@
//! [Clap] struct used to parse command line arguments.
use crate::string_type::String as StringType;
use crate::BACKEND;
use cfg_if::cfg_if;
use clap::{AppSettings, Clap};
use once_cell::sync::OnceCell;
use std::collections::BTreeSet;
use std::path::PathBuf;
@ -14,6 +16,34 @@ cfg_if! {
}
}
/// The current version of fif, as defined in Cargo.toml.
pub const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");
// the version and long_version given to clap need to be a &str, but we want to use format!, which returns a String.
// we can't just do something like `version = format!(...).as_str()`, because clap needs to know that the version will
// live for a given lifetime, which we need to satisfy by making our String static. of course, you can't use format!
// statically, so we need to use a OnceCell or similar to get around this.
static CLAP_VERSION: OnceCell<String> = OnceCell::new();
static CLAP_LONG_VERSION: OnceCell<String> = OnceCell::new();
/// Sets [`CLAP_VERSION`] to be the version defined in Cargo.toml, prefixed with a v (e.g. "v0.3.1"), then returns it as
/// a String.
fn clap_version() -> &'static str {
CLAP_VERSION
.set(format!("v{}", VERSION.unwrap_or("???")))
.unwrap_or_default(); // it doesn't matter if CLAP_VERSION has already been set
CLAP_VERSION.get().unwrap().as_str()
}
/// Sets [`CLAP_LONG_VERSION`] to be similar to [`CLAP_VERSION`], followed by the chosen backend in parentheses (e.g.
/// "v0.3.1 (XDG-Mime backend)"), then returns it as a String.
fn clap_long_version() -> &'static str {
CLAP_LONG_VERSION
.set(format!("v{} ({} backend)", VERSION.unwrap_or("???"), BACKEND))
.unwrap_or_default();
CLAP_LONG_VERSION.get().unwrap().as_str()
}
#[derive(Clap, PartialEq, Debug)]
pub enum OutputFormat {
/// A Bourne shell compatible script.
@ -33,7 +63,8 @@ pub enum OutputFormat {
#[derive(Clap, Debug)]
#[clap(
version = option_env!("CARGO_PKG_VERSION").unwrap_or("???"),
version = clap_version(),
long_version = clap_long_version(),
author = option_env!("CARGO_PKG_AUTHORS").unwrap_or("Lynnesbian"),
about = option_env!("CARGO_PKG_DESCRIPTION").unwrap_or("File Info Fixer"),
before_help = "Copyright © 2021 Lynnesbian under the GPL3 (or later) License.",