Skip to main content

git_global/
subcommands.rs

1//! Subcommand implementations and dispatch function `run()`.
2pub mod ahead;
3pub mod ignore;
4pub mod ignored;
5pub mod info;
6pub mod install_manpage;
7pub mod list;
8pub mod scan;
9pub mod staged;
10pub mod stashed;
11pub mod status;
12pub mod unstaged;
13
14use std::path::PathBuf;
15
16use crate::config::Config;
17use crate::errors::{GitGlobalError, Result};
18use crate::report::Report;
19
20/// Run a subcommand, returning a `Report`.
21///
22/// If `None` is given for the optional subcommand, run `config.default_cmd`.
23/// Else, try to match the given `&str` to a known subcommand.
24/// The `args` parameter carries additional positional arguments for
25/// subcommands that accept them (e.g. `ignore`, `scan`).
26pub fn run(
27    maybe_subcmd: Option<&str>,
28    config: Config,
29    args: Vec<String>,
30) -> Result<Report> {
31    let command = maybe_subcmd.unwrap_or(&config.default_cmd);
32    match command {
33        "info" => info::execute(config),
34        "list" => list::execute(config),
35        "scan" => {
36            let paths = args.into_iter().map(PathBuf::from).collect();
37            scan::execute(config, paths)
38        }
39        "staged" => staged::execute(config),
40        "stashed" => stashed::execute(config),
41        "status" => status::execute(config),
42        "unstaged" => unstaged::execute(config),
43        "ahead" => ahead::execute(config),
44        "install-manpage" => install_manpage::execute(config),
45        "ignore" => {
46            let pattern = args.into_iter().next().ok_or_else(|| {
47                GitGlobalError::BadSubcommand(
48                    "ignore requires a pattern argument".to_string(),
49                )
50            })?;
51            ignore::execute(config, &pattern)
52        }
53        "ignored" => ignored::execute(config),
54        cmd => Err(GitGlobalError::BadSubcommand(cmd.to_string())),
55    }
56}
57
58/// Return the list of all subcommand names and descriptions.
59///
60/// Used for building the clap::Command in the cli module.
61pub fn get_subcommands() -> Vec<(&'static str, &'static str)> {
62    vec![
63        (
64            "ahead",
65            "Shows repos with changes that are not pushed to a remote",
66        ),
67        ("ignore", "Ignores a repo, removing it from the list"),
68        ("ignored", "Lists all ignored repos"),
69        ("info", "Shows meta-information about git-global"),
70        (
71            "install-manpage",
72            "Attempts to install git-global's man page",
73        ),
74        ("list", "Lists all known repos"),
75        ("scan", "Updates cache of known repos"),
76        (
77            "staged",
78            "Shows git index status for repos with staged changes",
79        ),
80        ("stashed", "Shows repos with stashed changes"),
81        (
82            "status",
83            "Shows status (`git status -s`) for repos with any changes",
84        ),
85        (
86            "unstaged",
87            "Shows working dir status for repos with unstaged changes",
88        ),
89    ]
90}