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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
pub mod amps;
pub mod browse;
pub mod cache;
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 amps::AmpsCommand;
use amps::AmpsOptions;
use cache::CacheCommand;
use cache::CacheOptions;
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, version)]
#[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>,
    /// Bypass local .git/config. Use domain. Ex. for my subcommands
    #[clap(long, global = true, value_name = "DOMAIN")]
    pub domain: Option<String>,
    /// Full path to the config location. Default is $HOME/.config/gitar/api
    #[clap(long, global = true, value_name = "PATH")]
    pub config: 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),
    /// Interactively execute gitar amplifier commands using gitar. gr-in-gr
    #[clap(name = "amps")]
    Amps(AmpsCommand),
    #[clap(name = "init", about = "Initialize the config file")]
    Init(InitCommand),
    #[clap(name = "cache", about = "Local cache operations")]
    Cache(CacheCommand),
    #[clap(
        name = "manual",
        about = "Open the user manual in the browser",
        visible_alias = "man"
    )]
    Manual,
}

// 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())),
        Command::Cache(sub_matches) => Some(CliOptions::Cache(sub_matches.into())),
        Command::Manual => Some(CliOptions::Manual),
        Command::Amps(sub_matches) => Some(CliOptions::Amps(sub_matches.into())),
    };
    OptionArgs::new(
        options,
        CliArgs::new(args.verbose, args.repo, args.domain, args.config),
    )
}

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

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

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

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,
        }
    }
}