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