upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/bin/ngit/main.rs
blob: 1dbf02035173210c87b1fec3f6eeda3685340e36 (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#![cfg_attr(not(test), warn(clippy::pedantic))]
#![allow(clippy::large_futures)]
#![cfg_attr(not(test), warn(clippy::expect_used))]

use clap::Parser;
use cli::{AccountCommands, Cli, Commands, IssueCommands, PrCommands, CUSTOMISE_TEMPLATE};

mod cli;
use ngit::{
    cli_interactor::{self, CliError},
    client,
    git::{self, utils::set_git_timeout},
    git_events, login, repo_ref,
};

mod sub_commands;

#[tokio::main]
#[allow(clippy::too_many_lines)]
async fn main() {
    let cli = Cli::parse();

    // Non-interactive by default; set NGIT_INTERACTIVE_MODE only when -i is
    // specified
    if cli.interactive {
        std::env::set_var("NGIT_INTERACTIVE_MODE", "1");
    }

    if cli.verbose || std::env::var("NGITTEST").is_ok() {
        std::env::set_var("NGIT_VERBOSE", "1");
    }

    if cli.customize {
        print!("{CUSTOMISE_TEMPLATE}");
        std::process::exit(0); // Exit the program
    }

    let _ = set_git_timeout();

    let result = if let Some(command) = &cli.command {
        match command {
            Commands::Account(args) => match &args.account_command {
                AccountCommands::Login(sub_args) => {
                    sub_commands::login::launch(&cli, sub_args).await
                }
                AccountCommands::Logout => sub_commands::logout::launch().await,
                AccountCommands::ExportKeys => sub_commands::export_keys::launch().await,
                AccountCommands::Create(sub_args) => {
                    sub_commands::create::launch(&cli, sub_args).await
                }
                AccountCommands::Whoami(sub_args) => {
                    sub_commands::whoami::launch(&cli, sub_args).await
                }
            },
            Commands::Init(args) => sub_commands::init::launch(&cli, args).await,
            Commands::Repo(args) => {
                sub_commands::repo::launch(&cli, args.repo_command.as_ref(), args.offline, args.json).await
            }
            Commands::Send(args) => sub_commands::send::launch(&cli, args, false).await,
            Commands::Pr(args) => match &args.pr_command {
                PrCommands::List {
                    status,
                    labels,
                    json,
                    id,
                    offline,
                } => {
                    sub_commands::list::launch(
                        status.clone(),
                        labels.clone(),
                        *json,
                        false,
                        id.clone(),
                        *offline,
                    )
                    .await
                }
                PrCommands::View {
                    id,
                    json,
                    comments,
                    offline,
                } => {
                    sub_commands::list::launch(
                        "open,draft,closed,applied".to_string(),
                        vec![],
                        *json,
                        *comments,
                        Some(id.clone()),
                        *offline,
                    )
                    .await
                }
                PrCommands::Checkout { id, offline } => {
                    sub_commands::checkout::launch(id, *offline).await
                }
                PrCommands::Apply {
                    id,
                    stdout,
                    offline,
                } => sub_commands::apply::launch(id, *stdout, *offline).await,
                PrCommands::Send(sub_args) => {
                    sub_commands::send::launch(&cli, sub_args, false).await
                }
                PrCommands::Close { id, reason, offline } => {
                    sub_commands::pr_status::launch_close(id, *offline, reason.as_deref()).await
                }
                PrCommands::Reopen { id, reason, offline } => {
                    sub_commands::pr_status::launch_reopen(id, *offline, reason.as_deref()).await
                }
                PrCommands::Ready { id, reason, offline } => {
                    sub_commands::pr_status::launch_ready(id, *offline, reason.as_deref()).await
                }
                PrCommands::Draft { id, reason, offline } => {
                    sub_commands::pr_status::launch_draft(id, *offline, reason.as_deref()).await
                }
                PrCommands::Comment {
                    id,
                    body,
                    reply_to,
                    offline,
                } => {
                    sub_commands::comment::launch_pr_comment(
                        id,
                        body,
                        reply_to.as_deref(),
                        *offline,
                    )
                    .await
                }
                PrCommands::Merge {
                    id,
                    squash,
                    offline,
                } => sub_commands::pr_merge::launch(id, *squash, *offline).await,
                PrCommands::Label {
                    id,
                    labels,
                    offline,
                } => sub_commands::label::launch_pr_label(id, labels, *offline).await,
                PrCommands::SetSubject {
                    id,
                    subject,
                    offline,
                } => sub_commands::set_subject::launch_pr_set_subject(id, subject, *offline).await,
            },
            Commands::Issue(args) => match &args.issue_command {
                IssueCommands::List {
                    status,
                    labels,
                    json,
                    id,
                    offline,
                } => {
                    sub_commands::issue_list::launch(
                        status.clone(),
                        labels.clone(),
                        *json,
                        false,
                        id.clone(),
                        *offline,
                    )
                    .await
                }
                IssueCommands::View {
                    id,
                    json,
                    comments,
                    offline,
                } => {
                    sub_commands::issue_list::launch(
                        "open,draft,closed,applied".to_string(),
                        vec![],
                        *json,
                        *comments,
                        Some(id.clone()),
                        *offline,
                    )
                    .await
                }
                IssueCommands::Create {
                    title,
                    body,
                    labels,
                } => {
                    sub_commands::issue_create::launch(title.clone(), body.clone(), labels.clone())
                        .await
                }
                IssueCommands::Close { id, reason, offline } => {
                    sub_commands::issue_status::launch_close(id, *offline, reason.as_deref()).await
                }
                IssueCommands::Resolved { id, reason, offline } => {
                    sub_commands::issue_status::launch_resolved(id, *offline, reason.as_deref())
                        .await
                }
                IssueCommands::Reopen { id, reason, offline } => {
                    sub_commands::issue_status::launch_reopen(id, *offline, reason.as_deref()).await
                }
                IssueCommands::Comment {
                    id,
                    body,
                    reply_to,
                    offline,
                } => {
                    sub_commands::comment::launch_issue_comment(
                        id,
                        body,
                        reply_to.as_deref(),
                        *offline,
                    )
                    .await
                }
                IssueCommands::Label {
                    id,
                    labels,
                    offline,
                } => sub_commands::label::launch_issue_label(id, labels, *offline).await,
                IssueCommands::SetSubject {
                    id,
                    subject,
                    offline,
                } => {
                    sub_commands::set_subject::launch_issue_set_subject(id, subject, *offline).await
                }
            },
            Commands::Sync(args) => sub_commands::sync::launch(args).await,
        }
    } else {
        // Show help when no command is provided
        Cli::parse_from(["ngit", "--help"]);
        std::process::exit(0);
    };

    if let Err(err) = result {
        if err.downcast_ref::<CliError>().is_some() {
            // Already printed styled output to stderr
            std::process::exit(1);
        }
        eprintln!("Error: {err:?}");
        std::process::exit(1);
    }
}