git_project/
options.rs

1use std::{ffi, path};
2use structopt::{clap, StructOpt};
3
4/// A manager for all of your git projects
5#[derive(StructOpt)]
6pub struct Options {
7    #[structopt(flatten)]
8    pub command: Command,
9}
10
11#[derive(StructOpt)]
12pub struct BaseOptions {
13    /// The base directory that all of your repositories are inside
14    #[structopt(
15        short = "-d",
16        long = "--base-dir",
17        env = "GIT_PROJECT_BASE_DIR",
18        parse(try_from_os_str = "parse_canonical_path")
19    )]
20    pub base_dir: path::PathBuf,
21}
22
23#[derive(StructOpt)]
24pub enum Command {
25    /// Clone a new project into a folder based on the remote URL
26    #[structopt(name = "clone")]
27    Clone(CloneOptions),
28
29    /// List all repositories under the base path
30    #[structopt(name = "list")]
31    List(ListOptions),
32
33    /// Check all repositories under the base path to ensure the are up to date with remotes
34    #[structopt(name = "check")]
35    Check(CheckOptions),
36
37    /// Organize an existing directory of git repositories into a normalized format based on remotes
38    #[structopt(name = "organize")]
39    Organize(OrganizeOptions),
40
41    /// Generate CLI completions for your shell of choice
42    #[structopt(name = "gen-completions")]
43    GenCompletions(CompletionOptions),
44}
45
46#[derive(StructOpt)]
47pub struct CloneOptions {
48    #[structopt(flatten)]
49    pub base: BaseOptions,
50
51    /// Print the clone location for the given URL and exit
52    #[structopt(short = "-n", long = "--dry-run")]
53    pub only_print_location: bool,
54
55    /// The URL of the project to be cloned. Can be URL or ssh path
56    #[structopt(name = "URL")]
57    pub clone_url: String,
58}
59
60#[derive(StructOpt)]
61pub struct ListOptions {
62    #[structopt(flatten)]
63    pub base: BaseOptions,
64
65    #[structopt(flatten)]
66    pub list: BaseListOptions,
67}
68
69#[derive(StructOpt)]
70pub struct BaseListOptions {
71    /// Do not stop recursing when a .git folder is found
72    #[structopt(short = "-r", long = "--deep-recurse")]
73    pub deep_recurse: bool,
74}
75
76#[derive(StructOpt)]
77pub struct CheckOptions {
78    #[structopt(flatten)]
79    pub list: BaseListOptions,
80
81    #[structopt(flatten)]
82    pub base: BaseOptions,
83
84    /// Print a summary of the repositories
85    #[structopt(short = "-s", long = "--summarize")]
86    pub summarize: bool,
87}
88
89#[derive(StructOpt)]
90pub struct OrganizeOptions {
91    /// Directory to organize
92    #[structopt(name = "DIR", parse(from_os_str))]
93    pub dir: path::PathBuf,
94
95    /// Directory to place organized repositories in
96    #[structopt(name = "NEW_DIR", parse(from_os_str))]
97    pub new_dir: path::PathBuf,
98
99    /// Print out the folders that will be moved without actually moving anything
100    #[structopt(short = "-n", long = "--dry-run")]
101    pub dry_run: bool,
102}
103
104#[derive(StructOpt)]
105pub struct CompletionOptions {
106    /// The shell that the completions will be compatible with
107    #[structopt(
108        name = "SHELL",
109        raw(
110            possible_values = "&clap::Shell::variants()",
111            case_insensitive = "true"
112        )
113    )]
114    pub shell: clap::Shell,
115}
116
117fn parse_canonical_path(path_str: &ffi::OsStr) -> Result<path::PathBuf, ffi::OsString> {
118    path::Path::new(path_str).canonicalize().map_err(|err| {
119        ffi::OsString::from(format!(
120            "Unable to recognize base directory as absolute path: {}",
121            err
122        ))
123    })
124}