Skip to content

Commit

Permalink
update clap to 4 and git2 to 0.14
Browse files Browse the repository at this point in the history
```rust
    let matches = Command::new("pretty-git-prompt")
        .version(version)
```

this didn't work, the lifetime of Command is weird

Signed-off-by: Tomas Tomecek <[email protected]>
  • Loading branch information
TomasTomecek committed May 22, 2023
1 parent 35871d7 commit 1185771
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 69 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ name = "pretty-git-prompt"
yaml-rust = "0.4.2"

[dependencies.clap]
version = "3.0"
version = "4.*"
default-features = false
features = ["std"]
features = ["std", "cargo"]

[dependencies.git2]
version = "0.13"
version = "0.14.*"
default-features = false
features = []
4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ exec-stable-build: target/release/pretty-git-prompt

exec-nightly-build: target/debug/pretty-git-prompt

export GIT_REPO_IS_DIRTY := $(shell if git diff-index --quiet HEAD -- ; then echo "no"; else echo "yes"; fi)
ifeq ($(origin TRAVIS_COMMIT), undefined)
export TRAVIS_COMMIT := $(shell git rev-parse --short HEAD)
endif
target/release/pretty-git-prompt: $(DEPS)
LIBZ_SYS_STATIC=1 cargo build --release
target/debug/pretty-git-prompt: $(DEPS)
Expand Down
93 changes: 31 additions & 62 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use constants::*;
use models::{DisplayMaster};

use git2::Repository;
use clap::{App,Arg};
use clap::{Arg, ArgAction, Command};

// util mod def needs to be first b/c of macro definitions and usage in other modules
#[macro_use]
Expand All @@ -23,69 +23,49 @@ mod conf;
mod constants;
mod models;


fn get_version_str() -> String {
let version: Option<&'static str> = option_env!("CARGO_PKG_VERSION");
let commit: Option<&'static str> = option_env!("TRAVIS_COMMIT");
let is_dirty: Option<&'static str> = option_env!("GIT_REPO_IS_DIRTY");
format!(
"{} ({}{})",
match version {
Some(v) => v,
None => "<version undefined>",
},
match commit {
Some(v) => v,
None => "<commit unknown>"
},
match is_dirty {
Some(_) => ", dirty",
None => ""
}
)
}

fn run_app() -> Result<(), String> {
let version = get_version_str();
let version_ref: &str = version.as_str();
let def_conf_desc: &str = &format!("Create default config at \"{}\".", get_default_config_path().to_str().unwrap());
let app = App::new("pretty-git-prompt")
.version(version_ref)
fn main() {
let def_conf_desc: String = format!("Create default config at \"{}\".", get_default_config_path().to_str().unwrap());
let matches = Command::new("pretty-git-prompt")
.version(option_env!("CARGO_PKG_VERSION"))
.author("Tomas Tomecek <[email protected]>")
.about("Get `git status` inside your shell prompt.")
.subcommand(App::new(CLI_DEFAULT_CONFIG_SUBC_NAME)
.about(def_conf_desc))
.subcommand(Command::new(CLI_DEFAULT_CONFIG_SUBC_NAME)
.about(def_conf_desc))
.arg(Arg::new("config")
.short('c')
.long("config")
.value_name("FILE")
.help("Use the given config file.")
.takes_value(true))
.short('c')
.long("config")
.value_name("FILE")
.help("Use the given config file."))
.arg(Arg::new("debug")
.short('d')
.long("debug")
.help("Print debug messages, useful for identifying issues."));
let matches = app.get_matches();
.short('d')
.long("debug")
.help("Print debug messages, useful for identifying issues.")
.action(ArgAction::SetTrue)
).get_matches();

let debug_enabled = matches.is_present("debug");
let debug_enabled = matches.get_flag("debug");
if debug_enabled { println!("Debug messages are enabled."); }

let conf_path: Option<String> = if matches.is_present("config") {
Some(String::from(matches.value_of("config").unwrap()))
} else {
None
};
let conf_path = matches.get_one::<String>("config");

// create default config command
if matches.is_present(CLI_DEFAULT_CONFIG_SUBC_NAME) {
if matches.get_flag(CLI_DEFAULT_CONFIG_SUBC_NAME) {
let p = get_default_config_path();
match create_default_config(&p) {
Ok(path) => {
println!("Configuration file created at \"{}\"", path);
return Ok(());
::std::process::exit(0);
}
Err(e) => {
return Err(format!("Failed to create configuration file \"{}\": {}", p.to_str().unwrap(), e));
if let Err(e2) = writeln!(
io::stderr(),
"Failed to create configuration file \"{}\": {}",
p.to_str().unwrap(),
e
) {
println!("Writing error: {}", e2.to_string());
}
::std::process::exit(2);
}
};
} else {
Expand All @@ -95,25 +75,14 @@ fn run_app() -> Result<(), String> {
// not a git repository, ignore
Err(e) => {
if debug_enabled { println!("This is not a git repository: {:?}", e); }
return Ok(());
::std::process::exit(0);
}
};

let backend = Backend::new(repo, debug_enabled);
let dm: DisplayMaster = DisplayMaster::new(backend, debug_enabled);
let mut conf: Conf = get_configuration(conf_path, dm);
let mut conf: Conf = get_configuration(conf_path.cloned(), dm);
let out: String = conf.populate_values();
println!("{}", out);
}
Ok(())
}

fn main() {
::std::process::exit(match run_app() {
Ok(_) => 0,
Err(err) => {
writeln!(io::stderr(), "{}", err).unwrap();
2
}
});
}

0 comments on commit 1185771

Please sign in to comment.