adana_cache_command/
lib.rs

1mod cache;
2mod os_command;
3mod parser;
4mod prelude;
5mod process;
6mod reserved_keywords;
7pub use cache::get_default_cache;
8pub use process::process_command;
9use strum::EnumCount;
10
11pub mod constants {
12    pub const PUT: &str = "put";
13    pub const ALIAS: &str = "alias";
14    pub const GET: &str = "get";
15    pub const CLIPPY: &str = "clippy";
16    pub const CLIPPY_ALT: &str = "clip";
17    pub const DESCRIBE: &str = "describe";
18    pub const DESCRIBE_ALT: &str = "ds";
19    pub const LIST_CACHE: &str = "listns";
20    pub const LIST_CACHE_ALT: &str = "lsns";
21    pub const CURR_CACHE: &str = "currns";
22    pub const CURR_CACHE_ALT: &str = "currentns";
23    pub const RESTORE: &str = "restore";
24    pub const BACKUP: &str = "backup";
25    pub const BACKUP_ALT: &str = "bckp";
26    pub const FLUSH: &str = "flush";
27    pub const DEL_CACHE: &str = "delns";
28    pub const DEL_CACHE_ALT: &str = "deletens";
29    pub const MERGE_CACHE: &str = "merge";
30    pub const MERGE_CACHE_ALT: &str = "mergens";
31    pub const DEL: &str = "del";
32    pub const DEL_ALT: &str = "delete";
33    pub const EXEC: &str = "exec";
34    pub const CD: &str = "cd";
35    pub const USE: &str = "use";
36    pub const DUMP: &str = "dump";
37    pub const CLEAR: &str = "clear";
38    pub const CLEAR_ALT: &str = "cls";
39    pub const PRINT_SCRIPT_CONTEXT: &str = "print_script_ctx";
40    pub const PRINT_SCRIPT_CONTEXT_ALT: &str = "script_ctx";
41    pub const STORE_SCRIPT_CONTEXT: &str = "store_script_ctx";
42    pub const LOAD_SCRIPT_CONTEXT: &str = "load_script_ctx";
43    pub const PRINT_AST: &str = "ast";
44    pub const HELP: &str = "help";
45}
46
47pub use constants::*;
48
49#[derive(Debug, EnumCount)]
50pub enum CacheCommand<'a> {
51    Put { aliases: Vec<&'a str>, value: &'a str },
52    Describe(Option<&'a str>),
53    ListCache,
54    CurrentCache,
55    Backup,
56    Flush,
57    Restore,
58    DeleteCache(Option<&'a str>),
59    Merge(&'a str),
60    Alias((&'a str, &'a str)),
61    Del(&'a str),
62    Get(&'a str),
63    Clip(&'a str),
64    Exec { key: &'a str, args: Option<&'a str> },
65    Cd(ChangeDirectoryType<'a>),
66    Using(&'a str),
67    Dump(Option<&'a str>),
68    Clear,
69    PrintScriptContext,
70    StoreScriptContext(Option<&'a str>),
71    LoadScriptContext(Option<&'a str>),
72    PrintAst(&'a str),
73    Help,
74}
75#[derive(Debug)]
76pub enum ChangeDirectoryType<'a> {
77    HomeDirectory(Option<&'a str>),
78    Path(&'a str),
79    Previous,
80}
81
82impl CacheCommand<'_> {
83    pub const fn doc() -> &'static [(&'static [&'static str], &'static str)] {
84        if CacheCommand::COUNT != 23 {
85            panic!("CacheCommand::doc() no longer valid!");
86        }
87        &[
88            (
89                &[PUT],
90                "Put a new value to current namespace. can have multiple aliases with option '-a'. e.g `put -a drc -a drcomp docker-compose`",
91            ),
92            (
93                &[DESCRIBE, DESCRIBE_ALT],
94                "List values within the current namespace. Optional parameter for searching e.g `ds ssh`",
95            ),
96            (&[LIST_CACHE, LIST_CACHE_ALT], "List available namespaces."),
97            (&[CURR_CACHE, CURR_CACHE_ALT], "Print current namespace."),
98            (
99                &[BACKUP, BACKUP_ALT],
100                "Backup the database of namespaces to the current directory",
101            ),
102            (&[FLUSH], "Flush to database."),
103            (&[RESTORE], "Restore the database from current directory"),
104            (&[ALIAS], "Alias a key with another. e.g alias commit gc"),
105            (
106                &[DEL_CACHE, DEL_CACHE_ALT],
107                "Delete namespace or clear current namespace values.",
108            ),
109            (
110                &[MERGE_CACHE, MERGE_CACHE_ALT],
111                "Merge current with a given namespace",
112            ),
113            (&[DEL, DEL_ALT], "Remove value from namespace. e.g `del drc`"),
114            (&[GET], "Get value from namespace.  e.g `get drc`"),
115            (
116                &[CLIPPY, CLIPPY_ALT],
117                "Get value from namespace and copy it to clipboard. e.g `clip drc`",
118            ),
119            (
120                &[EXEC],
121                "Run a value from the namespace as an OS command. e.g `exec drc`",
122            ),
123            (&[CD], "Navigate to a directory"),
124            (
125                &[USE],
126                "Switch to another namespace. default ns is DEFAULT. e.g `use linux`",
127            ),
128            (
129                &[DUMP],
130                "Dump namespace(s) as json. Take an optional parameter, the namespace name. e.g `dump linux`",
131            ),
132            (&[CLEAR, CLEAR_ALT], "Clear the terminal."),
133            (
134                &[PRINT_SCRIPT_CONTEXT, PRINT_SCRIPT_CONTEXT_ALT],
135                "Print script context",
136            ),
137            (
138                &[STORE_SCRIPT_CONTEXT],
139                "Store script context (name optional), e.g store_script_ctx 12022023",
140            ),
141            (
142                &[LOAD_SCRIPT_CONTEXT],
143                "Load script context (name optional), e.g load_script_ctx 12022023",
144            ),
145            (&[PRINT_AST], "print ast for script code."),
146            (&[HELP], "Display Help."),
147        ]
148    }
149}
150
151pub fn clear_terminal() {
152    if cfg!(unix) {
153        let _ = std::process::Command::new("clear").status();
154    } else if cfg!(windows) {
155        let _ = std::process::Command::new("cls").status();
156    } else {
157        eprintln!("cannot clear the terminal for the target os");
158    };
159}