aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/command/repository_command.rs
blob: c1c32187140b1d7440e5f92fc66afd93c550001b (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//! ref: composer/src/Composer/Command/RepositoryCommand.php

use indexmap::IndexMap;
use shirabe_external_packages::composer::pcre::preg::Preg;
use shirabe_external_packages::symfony::component::console::command::command::Command;
use shirabe_external_packages::symfony::console::completion::completion_input::CompletionInput;
use shirabe_external_packages::symfony::console::input::input_interface::InputInterface;
use shirabe_external_packages::symfony::console::output::output_interface::OutputInterface;
use shirabe_php_shim::{
    InvalidArgumentException, PHP_URL_HOST, PhpMixed, RuntimeException, parse_url, strtolower,
};

use crate::command::base_command::BaseCommand;
use crate::command::base_config_command::BaseConfigCommand;
use crate::composer::Composer;
use crate::config::Config;
use crate::config::json_config_source::JsonConfigSource;
use crate::console::input::input_argument::InputArgument;
use crate::console::input::input_option::InputOption;
use crate::factory::Factory;
use crate::io::io_interface::IOInterface;
use crate::json::json_file::JsonFile;

#[derive(Debug)]
pub struct RepositoryCommand {
    inner: Command,
    composer: Option<Composer>,
    io: Option<Box<dyn IOInterface>>,

    config: Option<Config>,
    config_file: Option<JsonFile>,
    config_source: Option<JsonConfigSource>,
}

impl RepositoryCommand {
    pub fn configure(&mut self) {
        let suggest_repo_names_before = self.suggest_repo_names();
        let suggest_repo_names_after = self.suggest_repo_names();
        let suggest_repo_names_name = self.suggest_repo_names();
        let suggest_type_for_add = Self::suggest_type_for_add();
        self.inner.inner
            .set_name("repository")
            .set_aliases(vec!["repo".to_string()])
            .set_description("Manages repositories")
            .set_definition(vec![
                InputOption::new("global", Some(PhpMixed::String("g".to_string())), Some(InputOption::VALUE_NONE), "Apply command to the global config file", None, vec![]),
                InputOption::new("file", Some(PhpMixed::String("f".to_string())), Some(InputOption::VALUE_REQUIRED), "If you want to choose a different composer.json or config.json", None, vec![]),
                InputOption::new("append", None, Some(InputOption::VALUE_NONE), "When adding a repository, append it (lower priority) instead of prepending it", None, vec![]),
                InputOption::new("before", None, Some(InputOption::VALUE_REQUIRED), "When adding a repository, insert it before the given repository name", None, suggest_repo_names_before),
                InputOption::new("after", None, Some(InputOption::VALUE_REQUIRED), "When adding a repository, insert it after the given repository name", None, suggest_repo_names_after),
                InputArgument::new("action", Some(InputArgument::OPTIONAL), "Action to perform: list, add, remove, set-url, get-url, enable, disable", Some(PhpMixed::String("list".to_string())), vec!["list", "add", "remove", "set-url", "get-url", "enable", "disable"]),
                InputArgument::new("name", Some(InputArgument::OPTIONAL), "Repository name (or special name packagist.org for enable/disable)", None, suggest_repo_names_name),
                InputArgument::new("arg1", Some(InputArgument::OPTIONAL), "Type for add, or new URL for set-url, or JSON config for add", None, suggest_type_for_add),
                InputArgument::new("arg2", Some(InputArgument::OPTIONAL), "URL for add (if not using JSON)", None, vec![]),
            ])
            .set_help(
                "This command lets you manage repositories in your composer.json.\n\n\
                Examples:\n  \
                composer repo list\n  \
                composer repo add foo vcs https://github.com/acme/foo\n  \
                composer repo add bar composer https://repo.packagist.com/bar\n  \
                composer repo add zips '{\"type\":\"artifact\",\"url\":\"/path/to/dir/with/zips\"}'\n  \
                composer repo add baz vcs https://example.org --before foo\n  \
                composer repo add qux vcs https://example.org --after bar\n  \
                composer repo remove foo\n  \
                composer repo set-url foo https://git.example.org/acme/foo\n  \
                composer repo get-url foo\n  \
                composer repo disable packagist.org\n  \
                composer repo enable packagist.org\n\n\
                Use --global/-g to alter the global config.json instead.\n\
                Use --file to alter a specific file."
            );
    }

    pub fn execute(
        &mut self,
        input: &dyn InputInterface,
        _output: &dyn OutputInterface,
    ) -> anyhow::Result<i64> {
        let action = strtolower(
            &input
                .get_argument("action")
                .as_string()
                .unwrap_or("")
                .to_string(),
        );
        let name = input
            .get_argument("name")
            .as_string()
            .map(|s| s.to_string());
        let arg1 = input
            .get_argument("arg1")
            .as_string()
            .map(|s| s.to_string());
        let arg2 = input
            .get_argument("arg2")
            .as_string()
            .map(|s| s.to_string());

        let config_data = self.inner.config_file.as_ref().unwrap().read()?;
        let config_file_path = self
            .inner
            .config_file
            .as_ref()
            .unwrap()
            .get_path()
            .to_string();
        self.inner
            .config
            .as_mut()
            .unwrap()
            .merge(config_data, &config_file_path);
        let repos = self.inner.config.as_ref().unwrap().get_repositories();

        match action.as_str() {
            "list" | "ls" | "show" => {
                self.list_repositories(repos);
                Ok(0)
            }
            "add" => {
                if name.is_none() {
                    return Err(anyhow::anyhow!(RuntimeException {
                        message: "You must pass a repository name. Example: composer repo add foo vcs https://example.org".to_string(),
                        code: 0,
                    }));
                }
                if arg1.is_none() {
                    return Err(anyhow::anyhow!(RuntimeException {
                        message: "You must pass the type and a url, or a JSON string.".to_string(),
                        code: 0,
                    }));
                }
                let arg1_str = arg1.as_deref().unwrap();
                let repo_config: PhpMixed = if Preg::is_match(r"^\s*\{", arg1_str).unwrap_or(false)
                {
                    JsonFile::parse_json(Some(arg1_str), None)?
                } else {
                    if arg2.is_none() {
                        return Err(anyhow::anyhow!(RuntimeException {
                            message: "You must pass the type and a url. Example: composer repo add foo vcs https://example.org".to_string(),
                            code: 0,
                        }));
                    }
                    let mut m = IndexMap::new();
                    m.insert(
                        "type".to_string(),
                        Box::new(PhpMixed::String(arg1_str.to_string())),
                    );
                    m.insert("url".to_string(), Box::new(PhpMixed::String(arg2.unwrap())));
                    PhpMixed::Array(m)
                };

                let before = input
                    .get_option("before")
                    .as_string()
                    .map(|s| s.to_string());
                let after = input.get_option("after").as_string().map(|s| s.to_string());
                if before.is_some() && after.is_some() {
                    return Err(anyhow::anyhow!(RuntimeException {
                        message: "You can not combine --before and --after".to_string(),
                        code: 0,
                    }));
                }

                if before.is_some() || after.is_some() {
                    if matches!(repo_config, PhpMixed::Bool(false)) {
                        return Err(anyhow::anyhow!(RuntimeException {
                            message: "Cannot use --before/--after with boolean repository values"
                                .to_string(),
                            code: 0,
                        }));
                    }
                    let reference_name = before.as_deref().or(after.as_deref()).unwrap();
                    let offset: i64 = if after.is_some() { 1 } else { 0 };
                    self.inner
                        .config_source
                        .as_mut()
                        .unwrap()
                        .insert_repository(
                            name.as_deref().unwrap(),
                            repo_config,
                            reference_name,
                            offset,
                        );
                    return Ok(0);
                }

                let append = input.get_option("append").as_bool().unwrap_or(false);
                self.inner.config_source.as_mut().unwrap().add_repository(
                    name.as_deref().unwrap(),
                    repo_config,
                    append,
                );
                Ok(0)
            }
            "remove" | "rm" | "delete" => {
                if name.is_none() {
                    return Err(anyhow::anyhow!(RuntimeException {
                        message: "You must pass the repository name to remove.".to_string(),
                        code: 0,
                    }));
                }
                let name_str = name.as_deref().unwrap();
                self.inner
                    .config_source
                    .as_mut()
                    .unwrap()
                    .remove_repository(name_str);
                if ["packagist", "packagist.org"].contains(&name_str) {
                    self.inner.config_source.as_mut().unwrap().add_repository(
                        "packagist.org",
                        PhpMixed::Bool(false),
                        false,
                    );
                }
                Ok(0)
            }
            "set-url" | "seturl" => {
                if name.is_none() || arg1.is_none() {
                    return Err(anyhow::anyhow!(RuntimeException {
                        message: "Usage: composer repo set-url <name> <new-url>".to_string(),
                        code: 0,
                    }));
                }
                self.inner
                    .config_source
                    .as_mut()
                    .unwrap()
                    .set_repository_url(name.as_deref().unwrap(), arg1.as_deref().unwrap());
                Ok(0)
            }
            "get-url" | "geturl" => {
                if name.is_none() {
                    return Err(anyhow::anyhow!(RuntimeException {
                        message: "Usage: composer repo get-url <name>".to_string(),
                        code: 0,
                    }));
                }
                let name_str = name.as_deref().unwrap();
                if let Some(repo) = repos.get(name_str) {
                    if let PhpMixed::Array(ref repo_map) = *repo {
                        let url = repo_map.get("url").and_then(|v| v.as_string());
                        if let Some(url) = url {
                            self.inner.inner.get_io().write(url);
                            return Ok(0);
                        }
                        return Err(anyhow::anyhow!(InvalidArgumentException {
                            message: format!("The {} repository does not have a URL", name_str),
                            code: 0,
                        }));
                    }
                }
                for (_key, val) in &repos {
                    if let PhpMixed::Array(ref repo_map) = *val {
                        if let Some(n) = repo_map.get("name").and_then(|v| v.as_string()) {
                            if n == name_str {
                                let url = repo_map.get("url").and_then(|v| v.as_string());
                                if let Some(url) = url {
                                    self.inner.inner.get_io().write(url);
                                    return Ok(0);
                                }
                                return Err(anyhow::anyhow!(InvalidArgumentException {
                                    message: format!(
                                        "The {} repository does not have a URL",
                                        name_str
                                    ),
                                    code: 0,
                                }));
                            }
                        }
                    }
                }
                Err(anyhow::anyhow!(InvalidArgumentException {
                    message: format!("There is no {} repository defined", name_str),
                    code: 0,
                }))
            }
            "disable" => {
                if name.is_none() {
                    return Err(anyhow::anyhow!(RuntimeException {
                        message: "Usage: composer repo disable packagist.org".to_string(),
                        code: 0,
                    }));
                }
                let name_str = name.as_deref().unwrap();
                if ["packagist", "packagist.org"].contains(&name_str) {
                    let append = input.get_option("append").as_bool().unwrap_or(false);
                    self.inner.config_source.as_mut().unwrap().add_repository(
                        "packagist.org",
                        PhpMixed::Bool(false),
                        append,
                    );
                    return Ok(0);
                }
                Err(anyhow::anyhow!(RuntimeException {
                    message: "Only packagist.org can be enabled/disabled using this command. Use add/remove for other repositories.".to_string(),
                    code: 0,
                }))
            }
            "enable" => {
                if name.is_none() {
                    return Err(anyhow::anyhow!(RuntimeException {
                        message: "Usage: composer repo enable packagist.org".to_string(),
                        code: 0,
                    }));
                }
                let name_str = name.as_deref().unwrap();
                if ["packagist", "packagist.org"].contains(&name_str) {
                    self.inner
                        .config_source
                        .as_mut()
                        .unwrap()
                        .remove_repository("packagist.org");
                    return Ok(0);
                }
                Err(anyhow::anyhow!(RuntimeException {
                    message: "Only packagist.org can be enabled/disabled using this command."
                        .to_string(),
                    code: 0,
                }))
            }
            _ => Err(anyhow::anyhow!(InvalidArgumentException {
                message: format!(
                    "Unknown action \"{}\". Use list, add, remove, set-url, get-url, enable, disable",
                    action
                ),
                code: 0,
            })),
        }
    }

    fn list_repositories(&self, mut repos: IndexMap<String, PhpMixed>) {
        let io = self.inner.inner.get_io();

        let mut packagist_present = false;
        for (_key, repo) in &repos {
            if let PhpMixed::Array(ref repo_map) = *repo {
                let has_type_and_url =
                    repo_map.contains_key("type") && repo_map.contains_key("url");
                let is_composer_type =
                    repo_map.get("type").and_then(|v| v.as_string()) == Some("composer");
                let url_host_ends_with_packagist = repo_map
                    .get("url")
                    .and_then(|v| v.as_string())
                    .map(|url| {
                        parse_url(url, PHP_URL_HOST)
                            .as_string()
                            .unwrap_or("")
                            .ends_with("packagist.org")
                    })
                    .unwrap_or(false);
                if has_type_and_url && is_composer_type && url_host_ends_with_packagist {
                    packagist_present = true;
                    break;
                }
            }
        }
        if !packagist_present {
            let mut packagist_entry = IndexMap::new();
            packagist_entry.insert("packagist.org".to_string(), Box::new(PhpMixed::Bool(false)));
            repos.insert(repos.len().to_string(), PhpMixed::Array(packagist_entry));
        }

        if repos.is_empty() {
            io.write("No repositories configured");
            return;
        }

        for (key, repo) in &repos {
            if matches!(*repo, PhpMixed::Bool(false)) {
                io.write(&format!("[{}] <info>disabled</info>", key));
                continue;
            }

            if let PhpMixed::Array(ref repo_map) = *repo {
                if repo_map.len() == 1 {
                    if let Some(first_val) = repo_map.values().next() {
                        if matches!(**first_val, PhpMixed::Bool(false)) {
                            let first_key = repo_map.keys().next().unwrap();
                            io.write(&format!("[{}] <info>disabled</info>", first_key));
                            continue;
                        }
                    }
                }

                let name = repo_map
                    .get("name")
                    .and_then(|v| v.as_string())
                    .unwrap_or(key.as_str());
                let r#type = repo_map
                    .get("type")
                    .and_then(|v| v.as_string())
                    .unwrap_or("unknown");
                let url = repo_map
                    .get("url")
                    .and_then(|v| v.as_string())
                    .map(|s| s.to_string())
                    .unwrap_or_else(|| JsonFile::encode(repo));
                io.write(&format!("[{}] <info>{}</info> {}", name, r#type, url));
            }
        }
    }

    fn suggest_type_for_add() -> Box<dyn Fn(&CompletionInput) -> Vec<String>> {
        Box::new(|input: &CompletionInput| {
            if input.get_argument("action").as_string() == Some("add") {
                vec![
                    "composer".to_string(),
                    "vcs".to_string(),
                    "artifact".to_string(),
                    "path".to_string(),
                ]
            } else {
                vec![]
            }
        })
    }

    fn suggest_repo_names(&self) -> Box<dyn Fn(&CompletionInput) -> Vec<String> + '_> {
        Box::new(move |input: &CompletionInput| {
            let action = input
                .get_argument("action")
                .as_string()
                .unwrap_or("")
                .to_string();
            if ["enable", "disable"].contains(&action.as_str()) {
                return vec!["packagist.org".to_string()];
            }
            if !["remove", "set-url", "get-url"].contains(&action.as_str()) {
                return vec![];
            }
            let config = Factory::create_config(None, None).unwrap();
            let config_file_path = self.inner.get_composer_config_file(input, &config);
            let config_file = JsonFile::new(config_file_path, None, None);
            let data = config_file.read().unwrap_or_default();
            let mut repos = vec![];
            if let Some(repositories) = data.get("repositories").and_then(|v| v.as_list()) {
                for repo in repositories {
                    if let PhpMixed::Array(ref repo_map) = **repo {
                        if let Some(name) = repo_map.get("name").and_then(|v| v.as_string()) {
                            repos.push(name.to_string());
                        }
                    }
                }
            }
            repos.sort();
            repos
        })
    }
}

impl BaseCommand for RepositoryCommand {
    fn inner(&self) -> &Command {
        &self.inner
    }

    fn inner_mut(&mut self) -> &mut Command {
        &mut self.inner
    }

    fn composer(&self) -> Option<&Composer> {
        self.composer.as_ref()
    }

    fn composer_mut(&mut self) -> &mut Option<Composer> {
        &mut self.composer
    }

    fn io(&self) -> Option<&dyn IOInterface> {
        self.io.as_deref()
    }

    fn io_mut(&mut self) -> &mut Option<Box<dyn IOInterface>> {
        &mut self.io
    }
}

impl BaseConfigCommand for RepositoryCommand {
    fn config(&self) -> Option<&Config> {
        self.config.as_ref()
    }

    fn config_mut(&mut self) -> Option<&mut Config> {
        self.config.as_mut()
    }

    fn config_file(&self) -> Option<&JsonFile> {
        self.config_file.as_ref()
    }

    fn config_file_mut(&mut self) -> Option<&mut JsonFile> {
        self.config_file.as_mut()
    }

    fn config_source(&self) -> Option<&JsonConfigSource> {
        self.config_source.as_ref()
    }

    fn config_source_mut(&mut self) -> Option<&mut JsonConfigSource> {
        self.config_source.as_mut()
    }
}