1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
pub mod browse;
pub mod cicd;
pub mod common;
pub mod docker;
pub mod init;
pub mod merge_request;
pub mod my;
pub mod project;
pub mod release;
pub mod star;
pub mod trending;

use self::browse::BrowseCommand;
use self::browse::BrowseOptions;
use self::cicd::{PipelineCommand, PipelineOptions};
use self::common::validate_domain_project_repo_path;
use self::docker::{DockerCommand, DockerOptions};
use self::init::{InitCommand, InitCommandOptions};
use self::my::MyCommand;
use self::my::MyOptions;
use self::project::{ProjectCommand, ProjectOptions};
use self::release::{ReleaseCommand, ReleaseOptions};
use self::trending::TrendingCommand;
use self::trending::TrendingOptions;
use merge_request::{MergeRequestCommand, MergeRequestOptions};

use std::option::Option;

use clap::builder::{styling::AnsiColor, Styles};
use clap::Parser;

const CLI_STYLE: Styles = Styles::styled()
    .header(AnsiColor::Red.on_default().bold())
    .literal(AnsiColor::Blue.on_default().bold())
    .placeholder(AnsiColor::Green.on_default())
    .usage(AnsiColor::Red.on_default().bold());

#[derive(Parser)]
#[command(about = "A Github/Gitlab CLI tool", styles = CLI_STYLE)]
#[clap(next_help_heading = "Global options")]
struct Args {
    #[clap(subcommand)]
    pub command: Command,
    /// Verbose mode. Enable gitar's logging
    #[clap(long, short, global = true)]
    verbose: bool,
    /// Bypass local .git/config. Use repo instead. Ex: github.com/jordilin/gitar
    #[clap(
        long,
        global = true,
        value_name = "DOMAIN/OWNER/PROJECT_NAME",
        value_parser = validate_domain_project_repo_path
    )]
    pub repo: Option<String>,
}

#[derive(Parser)]
enum Command {
    #[clap(name = "mr", about = "Merge request operations")]
    MergeRequest(MergeRequestCommand),
    #[clap(name = "br", about = "Open the remote using your browser")]
    Browse(BrowseCommand),
    #[clap(name = "pp", about = "CI/CD Pipeline operations")]
    Pipeline(PipelineCommand),
    #[clap(name = "pj", about = "Gather project information metadata")]
    Project(ProjectCommand),
    #[clap(
        name = "dk",
        about = "Handles docker images in Gitlab/Github registries"
    )]
    Docker(DockerCommand),
    #[clap(name = "rl", about = "Release operations")]
    Release(ReleaseCommand),
    #[clap(
        name = "my",
        about = "Your user information, such as assigned merge requests, etc..."
    )]
    My(MyCommand),
    #[clap(name = "tr", about = "Trending repositories. Github.com only.")]
    Trending(TrendingCommand),
    #[clap(name = "init", about = "Initialize the config file")]
    Init(InitCommand),
}

// Parse cli and return CliOptions
pub fn parse_cli() -> OptionArgs {
    let args = Args::parse();
    let options = match args.command {
        Command::MergeRequest(sub_matches) => Some(CliOptions::MergeRequest(sub_matches.into())),
        Command::Browse(sub_matches) => Some(CliOptions::Browse(sub_matches.into())),
        Command::Pipeline(sub_matches) => Some(CliOptions::Pipeline(sub_matches.into())),
        Command::Project(sub_matches) => Some(CliOptions::Project(sub_matches.into())),
        Command::Init(sub_matches) => Some(CliOptions::Init(sub_matches.into())),
        Command::Docker(sub_matches) => Some(CliOptions::Docker(sub_matches.into())),
        Command::Release(sub_matches) => Some(CliOptions::Release(sub_matches.into())),
        Command::My(sub_matches) => Some(CliOptions::My(sub_matches.into())),
        Command::Trending(sub_matches) => Some(CliOptions::Trending(sub_matches.into())),
    };
    OptionArgs::new(options, CliArgs::new(args.verbose, args.repo))
}

pub enum CliOptions {
    MergeRequest(MergeRequestOptions),
    Browse(BrowseOptions),
    Pipeline(PipelineOptions),
    Project(ProjectOptions),
    Init(InitCommandOptions),
    Docker(DockerOptions),
    Release(ReleaseOptions),
    My(MyOptions),
    Trending(TrendingOptions),
}

#[derive(Clone)]
pub struct CliArgs {
    pub verbose: bool,
    pub repo: Option<String>,
}

impl CliArgs {
    pub fn new(verbose: bool, repo: Option<String>) -> Self {
        CliArgs { verbose, repo }
    }
}

pub struct OptionArgs {
    pub cli_options: Option<CliOptions>,
    pub cli_args: CliArgs,
}

impl OptionArgs {
    pub fn new(cli_options: Option<CliOptions>, cli_args: CliArgs) -> Self {
        OptionArgs {
            cli_options,
            cli_args,
        }
    }
}