cdp_cli/
lib.rs

1mod handlers;
2mod types;
3mod utils;
4mod constants;
5
6use clap::CommandFactory;
7use crossterm::style::Stylize;
8use types::*;
9use handlers::{
10    general::*, aliases::*, create_project::*,
11};
12
13use crate::{handlers::errors::CliErr, utils::parse_path};
14
15pub fn cli_run() -> Result<(), CliErr> {
16    let args = Cli::parse();
17
18    match args.command {
19        Some(Commands::General(cmd)) => {
20            let path = match parse_path::parse_path(cmd.path.clone()) {
21                Ok(s) => s,
22                Err(e) => {
23                    eprintln!("{e}");
24                    cmd.path
25                }
26            };
27
28            if cmd.current_user {
29                let home_path = if cfg!(windows) {
30                    "%USER/"
31                } else {
32                    "~/"
33                };
34                println!(
35                    "{}: the {} (or {}) flag is no longer necessary.\nYou can specify whether the directory is in user/home using: {}your_dir", 
36                    "Deprecated".yellow(), 
37                    "--current-user".red(), 
38                    "-C".red(), 
39                    home_path.green()
40                );
41            }
42            if cmd.alias {
43                println!(
44                    "{}: the {} (or {}) flag is no longer necessary.\nYou can specify whether is an alias using {}. (e.g. {})", 
45                    "Deprecated".yellow(), 
46                    "--alias".red(), 
47                    "--al".red(),
48                    ":".green(),
49                    ":cdpproject/src/parse_path.rs".green()
50                );
51            }
52            if cmd.ls {
53                if let Err(e) = ls::ls(path.clone()) {
54                    eprintln!("{e}");
55                }
56            }
57            if cmd.vsc {
58                println!(
59                    "{}: the {} flag is no longer necessary. Use {} (or {}) instead. (e.g. {})", 
60                    "Deprecated".yellow(), 
61                    "--vsc".red(),
62                    "--editor".green(),
63                    "-E".green(),
64                    r#"cdp general ":cdptests/assets" -E vsc"#.green()
65                );
66            }
67            if cmd.editor.is_some() {
68                if let Err(e) = editors::open_editor(Editors::Vsc, path.clone()) {
69                    eprintln!("{e}");
70                }
71            }
72        },
73        Some(Commands::Aliases(cmd)) => {
74            if let (Some(path), Some(alias)) = (cmd.path, cmd.alias) {
75                    if let Err(e) = aliases::aliases(path, alias) {
76                        eprintln!("{e}");
77                    }
78            }
79            if let Some(edit) = cmd.edit {
80                    if let Err(e) = edit::parse_response(edit) {
81                        eprintln!("{e}");
82                    }
83            }
84            if let Some(remove) = cmd.remove {
85                    if let Err(e) = remove::remove_alias(remove) {
86                        eprintln!("{e}");
87                    }
88            }
89            if cmd.list {
90                if let Err(e) = list::list_aliases() {
91                    eprintln!("{e}");
92                }
93            }
94        },
95        Some(Commands::CreateProject(cmd)) => {
96            if let Err(e) = create_project::create_project(
97                cmd.lang,
98                cmd.name,
99                cmd.alias,
100                cmd.path,
101            ) {
102                eprintln!("{e}");
103            }
104        },
105        None => { Cli::command().print_help().unwrap(); std::process::exit(1); }
106    }
107
108    Ok(())
109}