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
503
504
505
506
507
508
509
510
511
512
513
514
//! # cli_parser
//!
//! Defines the cli args and parsers them.
//!

#[cfg(test)]
#[path = "cli_parser_test.rs"]
mod cli_parser_test;

use crate::cli::{
    AUTHOR, DEFAULT_LOG_LEVEL, DEFAULT_OUTPUT_FORMAT, DEFAULT_TASK_NAME, DESCRIPTION, VERSION,
};
use crate::profile;
use crate::types::{CliArgs, GlobalConfig};
use cliparser::types::{
    Argument, ArgumentHelp, ArgumentOccurrence, ArgumentValueType, CliParsed, CliSpec,
    CliSpecMetaInfo, PositionalArgument,
};

use crate::error::CargoMakeError;

fn get_args(
    cli_parsed: &CliParsed,
    global_config: &GlobalConfig,
    command_name: &str,
    sub_command: bool,
) -> CliArgs {
    let mut cli_args = CliArgs::new();

    cli_args.command = if sub_command {
        let mut binary = "cargo ".to_string();
        binary.push_str(command_name);
        binary
    } else {
        command_name.to_string()
    };

    cli_args.env = to_owned_vec(cli_parsed.argument_values.get("env"));

    cli_args.build_file = match cli_parsed.get_first_value("makefile") {
        Some(value) => Some(value),
        None => None,
    };

    cli_args.cwd = cli_parsed.get_first_value("cwd");

    let default_log_level = match global_config.log_level {
        Some(ref value) => value.to_string(),
        None => DEFAULT_LOG_LEVEL.to_string(),
    };
    cli_args.log_level = if cli_parsed.arguments.contains("verbose") {
        "verbose".to_string()
    } else if cli_parsed.arguments.contains("quiet") {
        "error".to_string()
    } else if cli_parsed.arguments.contains("silent") {
        "off".to_string()
    } else {
        cli_parsed
            .get_first_value("loglevel")
            .unwrap_or(default_log_level)
            .to_string()
    };

    let default_disable_color = match global_config.disable_color {
        Some(value) => value,
        None => false,
    };
    cli_args.disable_color = cli_parsed.arguments.contains("no-color")
        || envmnt::is("CARGO_MAKE_DISABLE_COLOR")
        || default_disable_color;

    cli_args.print_time_summary = cli_parsed.arguments.contains("time-summary")
        || envmnt::is("CARGO_MAKE_PRINT_TIME_SUMMARY");

    cli_args.env_file = match cli_parsed.get_first_value("envfile") {
        Some(value) => Some(value.to_string()),
        None => None,
    };

    cli_args.output_format = cli_parsed
        .get_first_value("output-format")
        .unwrap_or(DEFAULT_OUTPUT_FORMAT.to_string())
        .to_string();

    cli_args.list_category_steps = match cli_parsed.get_first_value("list-category-steps") {
        Some(value) => Some(value.to_string()),
        None => None,
    };

    cli_args.output_file = match cli_parsed.get_first_value("output-file") {
        Some(value) => Some(value.to_string()),
        None => None,
    };

    let profile_name = cli_parsed
        .get_first_value("profile")
        .unwrap_or_else(profile::default_profile);
    cli_args.profile = Some(profile_name.to_string());

    cli_args.disable_check_for_updates = cli_parsed.arguments.contains("disable-check-for-updates");
    cli_args.experimental = cli_parsed.arguments.contains("experimental");
    cli_args.print_only = cli_parsed.arguments.contains("print-steps");
    cli_args.disable_workspace = cli_parsed.arguments.contains("no-workspace");
    cli_args.disable_on_error = cli_parsed.arguments.contains("no-on-error");
    cli_args.allow_private = cli_parsed.arguments.contains("allow-private");
    cli_args.skip_init_end_tasks = cli_parsed.arguments.contains("skip-init-end-tasks");
    cli_args.list_all_steps = cli_parsed.arguments.contains("list-steps");
    cli_args.diff_execution_plan = cli_parsed.arguments.contains("diff-steps");
    cli_args.hide_uninteresting = cli_parsed.arguments.contains("hide-uninteresting");

    cli_args.skip_tasks_pattern = match cli_parsed.get_first_value("skip-tasks-pattern") {
        Some(value) => Some(value.to_string()),
        None => None,
    };

    let default_task_name = match global_config.default_task_name {
        Some(ref value) => value.to_string(),
        None => DEFAULT_TASK_NAME.to_string(),
    };
    let task = cli_parsed
        .get_first_value("task")
        .unwrap_or(default_task_name);
    let task_cmd = to_owned_vec(cli_parsed.argument_values.get("TASK_CMD")).unwrap_or(vec![]);
    let task_cmd_slice = task_cmd.as_slice();
    let (task, arguments) = match task_cmd_slice {
        &[] => (task, None),
        &[ref task_name, ref task_args @ ..] => {
            let args_strings = task_args.iter().map(|item| item.to_string()).collect();
            (task_name.to_string(), Some(args_strings))
        }
    };
    cli_args.task = task;
    cli_args.arguments = arguments;

    cli_args
}

pub fn create_cli(global_config: &GlobalConfig, mut spec: CliSpec, default_meta: bool) -> CliSpec {
    let default_task_name = match global_config.default_task_name {
        Some(ref value) => value.as_str(),
        None => &DEFAULT_TASK_NAME,
    };
    let default_log_level = match global_config.log_level {
        Some(ref value) => value.as_str(),
        None => &DEFAULT_LOG_LEVEL,
    };

    if default_meta {
        spec = spec
            .set_meta_info(Some(CliSpecMetaInfo {
                author: Some(AUTHOR.to_string()),
                version: Some(VERSION.to_string()),
                description: Some(DESCRIPTION.to_string()),
                project: Some("cargo-make".to_string()),
                help_post_text: Some(
                    "See more info at: https://github.com/sagiegurari/cargo-make".to_string(),
                ),
            }))
            .add_command("makers")
            .add_subcommand(vec!["cargo", "make"])
            .add_subcommand(vec!["cargo-make", "make"]); // done by cargo
    }
    add_arguments(spec, default_task_name, default_log_level)
}

fn add_arguments(spec: CliSpec, default_task_name: &str, default_log_level: &str) -> CliSpec {
    spec
        .add_argument(Argument {
            name: "help".to_string(),
            key: vec!["--help".to_string(), "-h".to_string()],
            argument_occurrence: ArgumentOccurrence::Single,
            value_type: ArgumentValueType::None,
            default_value: None,
            help: Some(ArgumentHelp::Text("Print help information".to_string())),
        })
        .add_argument(Argument {
            name: "version".to_string(),
            key: vec!["--version".to_string(), "-V".to_string()],
            argument_occurrence: ArgumentOccurrence::Single,
            value_type: ArgumentValueType::None,
            default_value: None,
            help: Some(ArgumentHelp::Text("Print version information".to_string())),
        })
        .add_argument(Argument {
            name: "makefile".to_string(),
            key: vec!["--makefile".to_string()],
            argument_occurrence: ArgumentOccurrence::Single,
            value_type: ArgumentValueType::Single,
            default_value: None,
            help: Some(ArgumentHelp::TextAndParam(
                "The optional toml file containing the tasks definitions".to_string(),
                "FILE".to_string(),
            )),
        })
        .add_argument(Argument {
            name: "task".to_string(),
            key: vec!["--task".to_string(), "-t".to_string()],
            argument_occurrence: ArgumentOccurrence::Single,
            value_type: ArgumentValueType::Single,
            default_value: Some(default_task_name.to_string()),
            help: Some(ArgumentHelp::TextAndParam(
                "The task name to execute (can omit the flag if the task name is the last argument)".to_string(),
                "TASK".to_string(),
            )),
        })
        .add_argument(Argument {
            name: "profile".to_string(),
            key: vec!["--profile".to_string(), "-p".to_string()],
            argument_occurrence: ArgumentOccurrence::Single,
            value_type: ArgumentValueType::Single,
            default_value: Some(profile::default_profile()),
            help: Some(ArgumentHelp::TextAndParam(
                "The profile name (will be converted to lower case)".to_string(),
                "PROFILE".to_string(),
            )),
        })
        .add_argument(Argument {
            name: "cwd".to_string(),
            key: vec!["--cwd".to_string()],
            argument_occurrence: ArgumentOccurrence::Single,
            value_type: ArgumentValueType::Single,
            default_value: None,
            help: Some(ArgumentHelp::TextAndParam(
                "Will set the current working directory. The search for the makefile will be from this directory if defined.".to_string(),
                "DIRECTORY".to_string(),
            )),
        })
        .add_argument(Argument {
            name: "no-workspace".to_string(),
            key: vec!["--no-workspace".to_string()],
            argument_occurrence: ArgumentOccurrence::Single,
            value_type: ArgumentValueType::None,
            default_value: None,
            help: Some(ArgumentHelp::Text(
                "Disable workspace support (tasks are triggered on workspace and not on members)".to_string(),
            )),
        })
        .add_argument(Argument {
            name: "no-on-error".to_string(),
            key: vec!["--no-on-error".to_string()],
            argument_occurrence: ArgumentOccurrence::Single,
            value_type: ArgumentValueType::None,
            default_value: None,
            help: Some(ArgumentHelp::Text(
                "Disable on error flow even if defined in config sections".to_string(),
            )),
        })
        .add_argument(Argument {
            name: "allow-private".to_string(),
            key: vec!["--allow-private".to_string()],
            argument_occurrence: ArgumentOccurrence::Single,
            value_type: ArgumentValueType::None,
            default_value: None,
            help: Some(ArgumentHelp::Text(
                "Allow invocation of private tasks".to_string(),
            )),
        })
        .add_argument(Argument {
            name: "skip-init-end-tasks".to_string(),
            key: vec!["--skip-init-end-tasks".to_string()],
            argument_occurrence: ArgumentOccurrence::Single,
            value_type: ArgumentValueType::None,
            default_value: None,
            help: Some(ArgumentHelp::Text(
                "If set, init and end tasks are skipped".to_string(),
            )),
        })
        .add_argument(Argument {
            name: "skip-tasks-pattern".to_string(),
            key: vec!["--skip-tasks".to_string()],
            argument_occurrence: ArgumentOccurrence::Single,
            value_type: ArgumentValueType::Single,
            default_value: None,
            help: Some(ArgumentHelp::TextAndParam(
                "Skip all tasks that match the provided regex (example: pre.*|post.*)".to_string(),
                "SKIP_TASK_PATTERNS".to_string(),
            )),
        })
        .add_argument(Argument {
            name: "envfile".to_string(),
            key: vec!["--env-file".to_string()],
            argument_occurrence: ArgumentOccurrence::Single,
            value_type: ArgumentValueType::Single,
            default_value: None,
            help: Some(ArgumentHelp::TextAndParam(
                "Set environment variables from provided file".to_string(),
                "FILE".to_string(),
            )),
        })
        .add_argument(Argument {
            name: "env".to_string(),
            key: vec!["--env".to_string(), "-e".to_string()],
            argument_occurrence: ArgumentOccurrence::Multiple,
            value_type: ArgumentValueType::Single,
            default_value: None,
            help: Some(ArgumentHelp::TextAndParam(
                "Set environment variables".to_string(),
                "ENV".to_string(),
            )),
        })
        .add_argument(Argument {
            name: "loglevel".to_string(),
            key: vec!["--loglevel".to_string(), "-l".to_string()],
            argument_occurrence: ArgumentOccurrence::Single,
            value_type: ArgumentValueType::Single,
            default_value: Some(default_log_level.to_string()),
            help: Some(ArgumentHelp::TextAndParam(
                "The log level (verbose, info, error, off)".to_string(),
                "LOG LEVEL".to_string(),
            )),
        })
        .add_argument(Argument {
            name: "verbose".to_string(),
            key: vec!["--verbose".to_string(), "-v".to_string()],
            argument_occurrence: ArgumentOccurrence::Single,
            value_type: ArgumentValueType::None,
            default_value: None,
            help: Some(ArgumentHelp::Text(
                "Sets the log level to verbose (shorthand for --loglevel verbose)".to_string(),
            )),
        })
        .add_argument(Argument {
            name: "quiet".to_string(),
            key: vec!["--quiet".to_string()],
            argument_occurrence: ArgumentOccurrence::Single,
            value_type: ArgumentValueType::None,
            default_value: None,
            help: Some(ArgumentHelp::Text(
                "Sets the log level to error (shorthand for --loglevel error)".to_string(),
            )),
        })
        .add_argument(Argument {
            name: "silent".to_string(),
            key: vec!["--silent".to_string()],
            argument_occurrence: ArgumentOccurrence::Single,
            value_type: ArgumentValueType::None,
            default_value: None,
            help: Some(ArgumentHelp::Text(
                "Sets the log level to off (shorthand for --loglevel off)".to_string(),
            )),
        })
        .add_argument(Argument {
            name: "no-color".to_string(),
            key: vec!["--no-color".to_string()],
            argument_occurrence: ArgumentOccurrence::Single,
            value_type: ArgumentValueType::None,
            default_value: None,
            help: Some(ArgumentHelp::Text(
                "Disables colorful output".to_string(),
            )),
        })
        .add_argument(Argument {
            name: "time-summary".to_string(),
            key: vec!["--time-summary".to_string()],
            argument_occurrence: ArgumentOccurrence::Single,
            value_type: ArgumentValueType::None,
            default_value: None,
            help: Some(ArgumentHelp::Text(
                "Print task level time summary at end of flow".to_string(),
            )),
        })
        .add_argument(Argument {
            name: "experimental".to_string(),
            key: vec!["--experimental".to_string()],
            argument_occurrence: ArgumentOccurrence::Single,
            value_type: ArgumentValueType::None,
            default_value: None,
            help: Some(ArgumentHelp::Text(
                "Allows access unsupported experimental predefined tasks.".to_string(),
            )),
        })
        .add_argument(Argument {
            name: "disable-check-for-updates".to_string(),
            key: vec!["--disable-check-for-updates".to_string()],
            argument_occurrence: ArgumentOccurrence::Single,
            value_type: ArgumentValueType::None,
            default_value: None,
            help: Some(ArgumentHelp::Text(
                "Disables the update check during startup".to_string(),
            )),
        })
        .add_argument(Argument {
            name: "output-format".to_string(),
            key: vec!["--output-format".to_string()],
            argument_occurrence: ArgumentOccurrence::Single,
            value_type: ArgumentValueType::Single,
            default_value: None,
            help: Some(ArgumentHelp::TextAndParam(
                "The print/list steps format (some operations do not support all formats) (default, short-description, markdown, markdown-single-page, markdown-sub-section, autocomplete)".to_string(),
                "OUTPUT FORMAT".to_string(),
            )),
        })
        .add_argument(Argument {
            name: "output-file".to_string(),
            key: vec!["--output-file".to_string()],
            argument_occurrence: ArgumentOccurrence::Single,
            value_type: ArgumentValueType::Single,
            default_value: None,
            help: Some(ArgumentHelp::TextAndParam(
                "The list steps output file name".to_string(),
                "OUTPUT_FILE".to_string(),
            )),
        })
        .add_argument(Argument {
            name: "hide-uninteresting".to_string(),
            key: vec!["--hide-uninteresting".to_string()],
            argument_occurrence: ArgumentOccurrence::Single,
            value_type: ArgumentValueType::None,
            default_value: None,
            help: Some(ArgumentHelp::Text(
                "Hide any minor tasks such as pre/post hooks.".to_string(),
            )),
        })
        .add_argument(Argument {
            name: "print-steps".to_string(),
            key: vec!["--print-steps".to_string()],
            argument_occurrence: ArgumentOccurrence::Single,
            value_type: ArgumentValueType::None,
            default_value: None,
            help: Some(ArgumentHelp::Text(
                "Only prints the steps of the build in the order they will be invoked but without invoking them".to_string(),
            )),
        })
        .add_argument(Argument {
            name: "list-steps".to_string(),
            key: vec!["--list-all-steps".to_string()],
            argument_occurrence: ArgumentOccurrence::Single,
            value_type: ArgumentValueType::None,
            default_value: None,
            help: Some(ArgumentHelp::Text(
                "Lists all known steps".to_string(),
            )),
        })
        .add_argument(Argument {
            name: "list-category-steps".to_string(),
            key: vec!["--list-category-steps".to_string()],
            argument_occurrence: ArgumentOccurrence::Single,
            value_type: ArgumentValueType::Single,
            default_value: None,
            help: Some(ArgumentHelp::TextAndParam(
                "List steps for a given category".to_string(),
                "CATEGORY".to_string(),
            )),
        })
        .add_argument(Argument {
            name: "diff-steps".to_string(),
            key: vec!["--diff-steps".to_string()],
            argument_occurrence: ArgumentOccurrence::Single,
            value_type: ArgumentValueType::None,
            default_value: None,
            help: Some(ArgumentHelp::Text(
                "Runs diff between custom flow and prebuilt flow (requires git)".to_string(),
            )),
        })
        .set_positional_argument(Some(PositionalArgument {
            name: "TASK_CMD".to_string(),
            help: Some(ArgumentHelp::Text(
                "The task to execute, potentially including arguments which can be accessed in the task itself.".to_string(),
            )),
        }))
}

pub fn parse_args(
    global_config: &GlobalConfig,
    command_name: &str,
    sub_command: bool,
    args: Option<Vec<&str>>,
    spec: CliSpec,
) -> Result<CliArgs, CargoMakeError> {
    let cli_parsed = match args {
        Some(args_vec) => cliparser::parse(&args_vec, &spec),
        None => cliparser::parse_process(&spec),
    }?;

    if cli_parsed.arguments.contains("help") {
        // generate help text
        let help_text = cliparser::help(&spec);
        println!("{}", help_text);
        Err(CargoMakeError::ExitCode(std::process::ExitCode::SUCCESS))
    } else if cli_parsed.arguments.contains("version") {
        // generate version text
        let version_text = cliparser::version(&spec);
        println!("{}", version_text);
        Err(CargoMakeError::ExitCode(std::process::ExitCode::SUCCESS))
    } else {
        Ok(get_args(
            &cli_parsed,
            &global_config,
            command_name,
            sub_command,
        ))
    }
}

pub fn parse(
    global_config: &GlobalConfig,
    command_name: &str,
    sub_command: bool,
) -> Result<CliArgs, CargoMakeError> {
    parse_args(
        global_config,
        command_name,
        sub_command,
        None,
        create_cli(&global_config, CliSpec::new(), true),
    )
}

fn to_owned_vec(vec_option: Option<&Vec<String>>) -> Option<Vec<String>> {
    match vec_option {
        Some(vec) => Some(vec.to_owned()),
        None => None,
    }
}