bat-cli 0.12.1

Blockchain Auditor Toolkit (BAT)
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
515
516
517
518
519
520
521
522
523
use crate::batbelt::bat_dialoguer::BatDialoguer;
use crate::batbelt::command_line::CodeEditor;

use crate::batbelt::path::{prettify_source_code_path, BatFile, BatFolder};

use crate::batbelt::BatEnumerator;
use crate::commands::{BatCommandEnumerator, CommandError, CommandResult};

use clap::Subcommand;
use colored::{ColoredString, Colorize};

use error_stack::{Report, ResultExt};
use lazy_regex::regex;

use crate::batbelt::metadata::functions_source_code_metadata::FunctionSourceCodeMetadata;
use crate::batbelt::metadata::structs_source_code_metadata::StructSourceCodeMetadata;
use crate::batbelt::metadata::traits_source_code_metadata::TraitSourceCodeMetadata;
use crate::batbelt::metadata::{BatMetadata, BatMetadataType};

use crate::batbelt::templates::package_json_template::PackageJsonTemplate;

use crate::batbelt;
use crate::batbelt::metadata::enums_source_code_metadata::EnumSourceCodeMetadata;
use crate::batbelt::parser::entrypoint_parser::EntrypointParser;
use crate::config::{BatAuditorConfig, BatConfig};
use log::Level;
use tabled::{Style, Table, Tabled};

#[derive(
    Subcommand, Debug, strum_macros::Display, PartialEq, Clone, strum_macros::EnumIter, Default,
)]
pub enum ToolCommand {
    /// Opens a file from source code metadata to code editor. If code editor is None, then prints the path
    #[default]
    OpenSourceCode,
    /// Customize the package.json according to certain log level
    CustomizePackageJson,
    /// Opens the co file and the instruction file of a started entrypoint
    OpenCodeOverhaulFile,
    /// Search source code metadata by id and opens on code editor, if is source_code
    GetMetadataById,
    /// Counts the to-review, started, finished and total co files
    CountCodeOverhaul,
    /// Shows a list of entry points along with the file path
    ListEntryPointsPath,
    /// Shows a list of code overhaul files an the state
    ListCodeOverhaul,
}

impl BatEnumerator for ToolCommand {}

impl BatCommandEnumerator for ToolCommand {
    fn execute_command(&self) -> CommandResult<()> {
        match self {
            ToolCommand::OpenSourceCode => self.execute_open_source_code(),
            ToolCommand::CustomizePackageJson => self.execute_package_json(),
            ToolCommand::GetMetadataById => self.execute_get_metadata_by_id(),
            ToolCommand::OpenCodeOverhaulFile => self.execute_open_co(),
            ToolCommand::CountCodeOverhaul => self.execute_count_co_files(),
            ToolCommand::ListEntryPointsPath => self.execute_list_entry_points(),
            ToolCommand::ListCodeOverhaul => self.execute_list_co(),
        }
    }

    fn check_metadata_is_initialized(&self) -> bool {
        match self {
            ToolCommand::OpenSourceCode => true,
            ToolCommand::CustomizePackageJson => false,
            ToolCommand::GetMetadataById => true,
            ToolCommand::OpenCodeOverhaulFile => true,
            ToolCommand::CountCodeOverhaul => false,
            ToolCommand::ListEntryPointsPath => true,
            ToolCommand::ListCodeOverhaul => false,
        }
    }

    fn check_correct_branch(&self) -> bool {
        match self {
            ToolCommand::OpenSourceCode => false,
            ToolCommand::CustomizePackageJson => false,
            ToolCommand::GetMetadataById => false,
            ToolCommand::OpenCodeOverhaulFile => false,
            ToolCommand::CountCodeOverhaul => false,
            ToolCommand::ListEntryPointsPath => false,
            ToolCommand::ListCodeOverhaul => false,
        }
    }
}

impl ToolCommand {
    fn execute_list_co(&self) -> CommandResult<()> {
        let co_bat_folder = BatFolder::CodeOverhaulFolderPath { program_name: None };
        let co_dir_entries = co_bat_folder
            .get_all_files_dir_entries(true, None, None)
            .change_context(CommandError)?;

        #[derive(Tabled, Clone)]
        struct Path {
            #[tabled(rename = "Code overhaul file")]
            co_file_name: String,
            #[tabled(rename = "Code overhaul file path")]
            co_path: String,
            #[tabled(rename = "Status")]
            status: ColoredString,
        }

        let path_regex = regex!(r#"code-overhaul/[\w\-]+"#);
        let path_vec = co_dir_entries
            .into_iter()
            .map(|dir_entry| {
                let co_path = dir_entry.path().to_str().unwrap().to_string();
                let co_file_name = dir_entry.file_name().to_str().unwrap().to_string();
                let status = path_regex
                    .find(&co_path)
                    .unwrap()
                    .as_str()
                    .split("/")
                    .last()
                    .unwrap();
                let status = match status {
                    "to-review" => status.bright_red(),
                    "started" => status.bright_yellow(),
                    "finished" => status.bright_green(),
                    "deprecated" => status.bright_blue(),
                    _ => status.bright_magenta(),
                };
                Path {
                    co_file_name,
                    status,
                    co_path,
                }
            })
            .collect::<Vec<_>>();

        let to_review_regex = regex!(r#"code-overhaul/to-review"#);
        let started_regex = regex!(r#"code-overhaul/started"#);
        let finished_regex = regex!(r#"code-overhaul/finished"#);
        let deprecated_regex = regex!(r#"code-overhaul/deprecated"#);

        let to_review_count = path_vec
            .clone()
            .into_iter()
            .filter(|path| to_review_regex.is_match(&path.co_path))
            .count();
        let started_count = path_vec
            .clone()
            .into_iter()
            .filter(|path| started_regex.is_match(&path.co_path))
            .count();
        let finished_count = path_vec
            .clone()
            .into_iter()
            .filter(|path| finished_regex.is_match(&path.co_path))
            .count();
        let deprecated_count = path_vec
            .clone()
            .into_iter()
            .filter(|path| deprecated_regex.is_match(&path.co_path))
            .count();

        let mut table = Table::new(path_vec);
        table.with(Style::re_structured_text());
        println!("{}", table);

        println!(
            "{}: {}, {}: {}, {}: {}, {}: {}; {}: {}",
            "To review".bright_red(),
            to_review_count,
            "Started".bright_yellow(),
            started_count,
            "Finished".bright_green(),
            finished_count,
            "Deprecated".bright_blue(),
            deprecated_count,
            "Total (to review + started + finished)".bright_white(),
            to_review_count + started_count + finished_count,
        );

        Ok(())
    }

    fn execute_list_entry_points(&self) -> CommandResult<()> {
        let bat_metadata = BatMetadata::read_metadata().change_context(CommandError)?;
        let entry_points_metadata = bat_metadata.entry_points;
        let ep_parser_vec = entry_points_metadata
            .into_iter()
            .map(|ep_meta| EntrypointParser::new_from_name(&ep_meta.name))
            .collect::<Result<Vec<_>, _>>()
            .change_context(CommandError)?;
        println!(
            "Printing {}, with {}:\n",
            "entry points".bright_green(),
            "dependency paths".bright_yellow()
        );

        #[derive(Tabled)]
        struct Path {
            #[tabled(rename = "Entry point name")]
            entry_point_name: String,
            #[tabled(rename = "Dependencies")]
            dependencies_path: String,
        }

        let mut path_vec: Vec<Path> = vec![];

        for ep_parser in ep_parser_vec {
            let deps_str = ep_parser
                .dependencies
                .iter()
                .map(|dep| {
                    format!(
                        "{}:{}",
                        prettify_source_code_path(&dep.path).unwrap_or_else(|_| dep.path.clone()),
                        dep.start_line_index
                    )
                })
                .collect::<Vec<_>>()
                .join(", ");
            path_vec.push(Path {
                entry_point_name: ep_parser.name,
                dependencies_path: if deps_str.is_empty() {
                    "No dependencies".to_string()
                } else {
                    deps_str
                },
            });
        }

        let mut table = Table::new(path_vec);
        table.with(Style::sharp());

        println!("{}", table);
        Ok(())
    }

    fn execute_open_source_code(&self) -> CommandResult<()> {
        let selected_bat_metadata_type =
            BatMetadataType::prompt_metadata_type_selection().change_context(CommandError)?;
        let (path, start_line_index) = match selected_bat_metadata_type {
            BatMetadataType::Struct => {
                let StructSourceCodeMetadata {
                    path,
                    start_line_index,
                    ..
                } = StructSourceCodeMetadata::prompt_selection().change_context(CommandError)?;
                (path, start_line_index)
            }
            BatMetadataType::Function => {
                let FunctionSourceCodeMetadata {
                    path,
                    start_line_index,
                    ..
                } = FunctionSourceCodeMetadata::prompt_selection().change_context(CommandError)?;
                (path, start_line_index)
            }
            BatMetadataType::Trait => {
                let TraitSourceCodeMetadata {
                    path,
                    start_line_index,
                    ..
                } = TraitSourceCodeMetadata::prompt_selection().change_context(CommandError)?;
                (path, start_line_index)
            }
            BatMetadataType::Enum => {
                let EnumSourceCodeMetadata {
                    path,
                    start_line_index,
                    ..
                } = EnumSourceCodeMetadata::prompt_selection().change_context(CommandError)?;
                (path, start_line_index)
            }
        };
        CodeEditor::open_file_in_editor(&path, Some(start_line_index))
            .change_context(CommandError)?;
        Ok(())
    }

    fn execute_package_json(&self) -> CommandResult<()> {
        let prompt_text = "Select the log level:".to_string();
        let log_level_vec = vec![
            Level::Warn,
            Level::Info,
            Level::Debug,
            Level::Trace,
            Level::Error,
        ];
        let selection = BatDialoguer::select(
            prompt_text,
            log_level_vec
                .clone()
                .into_iter()
                .enumerate()
                .map(|(idx, level)| ToolCommand::colored_from_index(&level.to_string(), idx))
                .collect::<Vec<_>>(),
            None,
        )?;
        let level_selected = log_level_vec[selection];
        PackageJsonTemplate::create_package_json(Some(level_selected)).change_context(CommandError)
    }

    fn execute_get_metadata_by_id(&self) -> CommandResult<()> {
        let metadata_id = BatDialoguer::input("Metadata id:".to_string())?;
        let bat_metadata = BatMetadata::read_metadata().change_context(CommandError)?;
        for function_metadata in bat_metadata.source_code.functions_source_code {
            if function_metadata.metadata_id == metadata_id {
                println!("Metadata found:\n{:#?}", function_metadata);
                CodeEditor::open_file_in_editor(
                    &function_metadata.path,
                    Some(function_metadata.start_line_index),
                )
                .change_context(CommandError)?;
                return Ok(());
            }
        }
        for struct_metadata in bat_metadata.source_code.structs_source_code {
            if struct_metadata.metadata_id == metadata_id {
                println!("Metadata found:\n{:#?}", struct_metadata);
                CodeEditor::open_file_in_editor(
                    &struct_metadata.path,
                    Some(struct_metadata.start_line_index),
                )
                .change_context(CommandError)?;
                return Ok(());
            }
        }
        for trait_metadata in bat_metadata.source_code.traits_source_code {
            if trait_metadata.metadata_id == metadata_id {
                println!("Metadata found:\n{:#?}", trait_metadata);
                CodeEditor::open_file_in_editor(
                    &trait_metadata.path,
                    Some(trait_metadata.start_line_index),
                )
                .change_context(CommandError)?;
                return Ok(());
            }
        }
        for enum_metadata in bat_metadata.source_code.enums_source_code {
            if enum_metadata.metadata_id == metadata_id {
                println!("Metadata found:\n{:#?}", enum_metadata);
                CodeEditor::open_file_in_editor(
                    &enum_metadata.path,
                    Some(enum_metadata.start_line_index),
                )
                .change_context(CommandError)?;
                return Ok(());
            }
        }
        if let Some(trait_metadata) = bat_metadata
            .traits
            .clone()
            .into_iter()
            .find(|trait_meta| trait_meta.metadata_id == metadata_id)
        {
            println!("Metadata found is trait metadata:\n{:#?}", trait_metadata);
            return Ok(());
        }

        if let Some(entry_point_meta) = bat_metadata
            .entry_points
            .clone()
            .into_iter()
            .find(|metadata| metadata.metadata_id == metadata_id)
        {
            println!(
                "Metadata found is entrypoint metadata:\n{:#?}",
                entry_point_meta
            );
            return Ok(());
        }

        if let Some(ca_meta) = bat_metadata
            .context_accounts
            .clone()
            .into_iter()
            .find(|metadata| metadata.metadata_id == metadata_id)
        {
            println!(
                "Metadata found is context accounts metadata:\n{:#?}",
                ca_meta
            );
            return Ok(());
        }

        if let Some(func_dep_meta) = bat_metadata
            .function_dependencies
            .into_iter()
            .find(|metadata| metadata.metadata_id == metadata_id)
        {
            println!(
                "Metadata found is function dependencies metadata:\n{:#?}",
                func_dep_meta
            );
            return Ok(());
        }

        Err(Report::new(CommandError)
            .attach_printable(format!("Metadata for {} couldn't be found", metadata_id)))
    }

    fn execute_open_co(&self) -> error_stack::Result<(), CommandError> {
        let bat_auditor_config = BatAuditorConfig::get_config().change_context(CommandError)?;
        // list to start
        if bat_auditor_config.use_code_editor {
            let options = vec!["started".green(), "finished".yellow()];
            let prompt_text = format!(
                "Do you want to open a {} or a {} file?",
                options[0], options[1]
            );
            let selection = BatDialoguer::select(prompt_text, options.clone(), None)
                .change_context(CommandError)?;
            let open_started = selection == 0;
            let bat_config = BatConfig::get_config().change_context(CommandError)?;
            let selected_program_name = if bat_config.is_multi_program() {
                Some(
                    bat_config
                        .prompt_select_program()
                        .change_context(CommandError)?,
                )
            } else {
                None
            };
            let co_folder = if open_started {
                BatFolder::CodeOverhaulStarted {
                    program_name: selected_program_name.clone(),
                }
            } else {
                BatFolder::CodeOverhaulFinished {
                    program_name: selected_program_name.clone(),
                }
            };
            let co_files = co_folder
                .get_all_files_dir_entries(true, None, None)
                .change_context(CommandError)?
                .into_iter()
                .map(|dir_entry| dir_entry.file_name().to_str().unwrap().to_string())
                .collect::<Vec<_>>();
            if !co_files.is_empty() {
                let prompt_text = "Select the code-overhaul file to open:";
                let selection = batbelt::bat_dialoguer::select(prompt_text, co_files.clone(), None)
                    .change_context(CommandError)?;
                let file_name = &co_files[selection].clone();
                let bat_file = if open_started {
                    BatFile::CodeOverhaulStarted {
                        file_name: file_name.clone(),
                        program_name: selected_program_name.clone(),
                    }
                } else {
                    BatFile::CodeOverhaulFinished {
                        file_name: file_name.clone(),
                        program_name: selected_program_name.clone(),
                    }
                };
                let ep_parser =
                    EntrypointParser::new_from_name(file_name.clone().trim_end_matches(".md"))
                        .change_context(CommandError)?;

                bat_file
                    .open_in_editor(false, None)
                    .change_context(CommandError)?;
                CodeEditor::open_file_in_editor(
                    &ep_parser.entry_point_function.path,
                    Some(ep_parser.entry_point_function.start_line_index),
                )
                .change_context(CommandError)?;
                return Ok(());
            } else {
                println!("Empty {} folder", options[selection].clone());
            }
        } else {
            print!("VSCode integration not enabled");
        }
        Ok(())
    }

    fn execute_count_co_files(&self) -> error_stack::Result<(), CommandError> {
        let (to_review_count, started_count, finished_count) = self.co_counter()?;
        println!("to-review co files: {}", format!("{to_review_count}").red());
        println!("started co files: {}", format!("{started_count}").yellow());
        println!("finished co files: {}", format!("{finished_count}").green());
        println!(
            "total co files: {}",
            format!("{}", to_review_count + started_count + finished_count).purple()
        );
        Ok(())
    }

    fn co_counter(&self) -> error_stack::Result<(usize, usize, usize), CommandError> {
        let bat_config = BatConfig::get_config().change_context(CommandError)?;
        let program_names: Vec<Option<String>> = if bat_config.is_multi_program() {
            bat_config
                .get_program_names()
                .into_iter()
                .map(Some)
                .collect()
        } else {
            vec![None]
        };
        let mut to_review_count = 0;
        let mut started_count = 0;
        let mut finished_count = 0;
        for pn in &program_names {
            to_review_count += BatFolder::CodeOverhaulToReview {
                program_name: pn.clone(),
            }
            .get_all_files_names(true, None, None)
            .change_context(CommandError)?
            .len();
            started_count += BatFolder::CodeOverhaulStarted {
                program_name: pn.clone(),
            }
            .get_all_files_names(true, None, None)
            .change_context(CommandError)?
            .len();
            finished_count += BatFolder::CodeOverhaulFinished {
                program_name: pn.clone(),
            }
            .get_all_files_names(true, None, None)
            .change_context(CommandError)?
            .len();
        }
        Ok((to_review_count, started_count, finished_count))
    }
}