als-manager 0.1.0

A TUI for managing, auditing, and searching shell aliases across Zsh, Bash, and Fish.
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
pub mod git;
pub mod history;
pub mod shell;

use crate::models::{Alias, ShadowedDefinition};
use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use which::which;

pub fn get_all_aliases() -> Vec<Alias> {
    let mut raw_aliases = Vec::new();
    let mut history_data = HashMap::new();

    let mut env_vars: Vec<(String, String)> = std::env::vars().collect();
    env_vars.sort_by(|a, b| b.0.len().cmp(&a.0.len()));

    if let Some(home) = home::home_dir() {
        history_data = history::parse_history(&home);

        let mut visited_files = HashSet::new();

        // Zsh entry points
        let zsh_files = [".zshrc", ".zshenv", ".zprofile"];
        for file in zsh_files {
            let _ = shell::parse_file_recursive(
                &home.join(file),
                &mut raw_aliases,
                &mut visited_files,
                &env_vars,
            );
        }

        // Bash entry points
        let bash_files = [".bashrc", ".bash_profile", ".profile"];
        for file in bash_files {
            let _ = shell::parse_file_recursive(
                &home.join(file),
                &mut raw_aliases,
                &mut visited_files,
                &env_vars,
            );
        }

        // Fish entry point
        let fish_config = home.join(".config/fish/config.fish");
        let _ = shell::parse_file_recursive(
            &fish_config,
            &mut raw_aliases,
            &mut visited_files,
            &env_vars,
        );

        // Git config
        let gitconfig_path = home.join(".gitconfig");
        let _ = git::parse_git_config(&gitconfig_path, &mut raw_aliases);
    }

    // Group by name to find effective aliases and their shadow chains
    let mut alias_groups: HashMap<String, Vec<Alias>> = HashMap::new();
    for alias in raw_aliases {
        alias_groups
            .entry(alias.name.clone())
            .or_default()
            .push(alias);
    }

    let mut effective_aliases = Vec::new();
    let mut command_to_names: HashMap<String, Vec<String>> = HashMap::new();

    for (name, mut group) in alias_groups {
        if let Some(mut effective) = group.pop() {
            for shadow in group {
                effective.is_conflicting = true;
                effective.shadows.push(ShadowedDefinition {
                    source_file: shadow.source_file,
                    line_number: shadow.line_number,
                    command: shadow.command,
                });
            }

            if let Some(info) = history_data.get(&name) {
                effective.usage_count = info.count;
                if info.last_used > 0 {
                    effective.last_used = Some(info.last_used);
                }
            }

            if effective.name.starts_with("git ") {
                if effective.command.starts_with('!') {
                    let cmd = &effective.command[1..];
                    effective.is_broken = !validate_command(cmd, &env_vars);
                } else {
                    effective.is_broken = false;
                }
            } else {
                effective.is_broken = !validate_command(&effective.command, &env_vars);
            }

            command_to_names
                .entry(effective.command.clone())
                .or_default()
                .push(effective.name.clone());
            effective_aliases.push(effective);
        }
    }

    // Calculate duplicates and expansions
    let mut alias_map: HashMap<String, String> = effective_aliases
        .iter()
        .map(|a| (a.name.clone(), a.command.clone()))
        .collect();

    // Add git subcommands to the map for git-specific expansion
    for alias in &effective_aliases {
        if let Some(stripped) = alias.name.strip_prefix("git ")
            && !alias_map.contains_key(stripped)
        {
            alias_map.insert(stripped.to_string(), alias.command.clone());
        }
    }

    for alias in &mut effective_aliases {
        if let Some(names) = command_to_names.get(&alias.command) {
            alias.duplicates = names
                .iter()
                .filter(|&n| n != &alias.name)
                .cloned()
                .collect();
        }

        alias.expanded_command = resolve_expansion(&alias.command, &alias_map);
    }

    effective_aliases
}

pub fn validate_command(command: &str, env_vars: &[(String, String)]) -> bool {
    let parts: Vec<&str> = command.split_whitespace().collect();
    if let Some(cmd) = parts.first() {
        let mut expanded_cmd = cmd.to_string();
        for (key, value) in env_vars {
            let key_dollar = format!("${}", key);
            if expanded_cmd.contains(&key_dollar) {
                expanded_cmd = expanded_cmd.replace(&key_dollar, value);
            }
        }

        if expanded_cmd.starts_with("./")
            || expanded_cmd.starts_with("../")
            || expanded_cmd.contains('/')
        {
            return true;
        }

        if which(&expanded_cmd).is_ok() {
            return true;
        }

        let builtins = [
            "cd",
            "echo",
            "export",
            "source",
            ".",
            "alias",
            "unalias",
            "history",
            "exit",
            "pwd",
            "true",
            "false",
            "test",
            "[",
            "[[",
            "local",
            "read",
            "type",
            "command",
            "builtin",
            "eval",
            "exec",
            "set",
            "unset",
            "wait",
            "trap",
            "ulimit",
            "umask",
            "fg",
            "bg",
            "jobs",
            "kill",
            "fc",
            "hash",
            "popd",
            "pushd",
            "dirs",
            "shift",
            "time",
            "times",
            "return",
            "break",
            "continue",
            "printf",
            "getopts",
            "declare",
            "typeset",
            "let",
            "shopt",
            "caller",
            "compgen",
            "complete",
            "compopt",
            "disown",
            "enable",
            "help",
            "logout",
            "mapfile",
            "readarray",
            "suspend",
        ];
        if builtins.contains(&expanded_cmd.as_str()) {
            return true;
        }

        return false;
    }
    true
}

pub fn resolve_path(
    path_str: &str,
    base_dir: &Path,
    env_vars: &[(String, String)],
) -> Option<PathBuf> {
    let mut expanded = path_str.to_string();

    if expanded.contains("$(brew --prefix)") {
        expanded = expanded.replace("$(brew --prefix)", "/opt/homebrew");
    }

    for (key, value) in env_vars {
        let key_dollar = format!("${}", key);
        let key_braces = format!("${{{}}}", key);
        if expanded.contains(&key_dollar) {
            expanded = expanded.replace(&key_dollar, value);
        }
        if expanded.contains(&key_braces) {
            expanded = expanded.replace(&key_braces, value);
        }
    }

    if let Some(home) = home::home_dir() {
        if expanded == "~" {
            return Some(home);
        } else if let Some(stripped) = expanded.strip_prefix("~/") {
            let mut p = home;
            p.push(stripped);
            return Some(p);
        }
    }

    let p = PathBuf::from(&expanded);
    if p.is_absolute() {
        Some(p)
    } else {
        let mut abs_p = base_dir.to_path_buf();
        abs_p.push(p);
        if abs_p.exists() { Some(abs_p) } else { None }
    }
}

pub fn resolve_expansion(command: &str, alias_map: &HashMap<String, String>) -> Option<String> {
    let mut current_command = command.to_string();
    let mut history = HashSet::new();
    let mut expanded = false;

    for _ in 0..10 {
        let parts: Vec<&str> = current_command.split_whitespace().collect();
        if parts.is_empty() {
            break;
        }

        let potential_alias = parts[0];
        if let Some(expansion) = alias_map.get(potential_alias) {
            if history.contains(potential_alias) {
                return Some(format!("{} (Loop detected)", current_command));
            }
            history.insert(potential_alias.to_string());

            let args = if current_command.len() > potential_alias.len() {
                &current_command[potential_alias.len()..]
            } else {
                ""
            };
            current_command = format!("{}{}", expansion, args);
            expanded = true;
        } else {
            break;
        }
    }

    if expanded {
        Some(current_command)
    } else {
        None
    }
}

pub fn extract_tags(comment: Option<&str>) -> Vec<String> {
    let mut tags = Vec::new();
    if let Some(c) = comment {
        for word in c.split_whitespace() {
            if word.starts_with('@') && word.len() > 1 {
                tags.push(word[1..].to_string());
            }
        }
    }
    tags
}

pub fn clean_description(comment: Option<String>) -> Option<String> {
    comment
        .map(|c| {
            let cleaned: String = c
                .split_whitespace()
                .filter(|word| !word.starts_with('@'))
                .collect::<Vec<_>>()
                .join(" ");
            cleaned
        })
        .filter(|s| !s.is_empty())
}

pub fn compare_ignore_case(a: &str, b: &str) -> Ordering {
    a.chars()
        .flat_map(char::to_lowercase)
        .cmp(b.chars().flat_map(char::to_lowercase))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_resolve_path() {
        let base = Path::new("/home/user");
        let env_vars = vec![("TEST_DIR".to_string(), "/tmp/test".to_string())];

        assert_eq!(
            resolve_path("/abs/path", base, &[]),
            Some(PathBuf::from("/abs/path"))
        );
        assert_eq!(
            resolve_path("$TEST_DIR/file", base, &env_vars),
            Some(PathBuf::from("/tmp/test/file"))
        );
    }

    #[test]
    fn test_resolve_expansion() {
        let mut map = HashMap::new();
        map.insert("g".to_string(), "git".to_string());
        map.insert("gs".to_string(), "g status".to_string());
        map.insert("bar".to_string(), "echo backfoo".to_string());
        map.insert("foo".to_string(), "bar".to_string());

        assert_eq!(resolve_expansion("g", &map), Some("git".to_string()));
        assert_eq!(
            resolve_expansion("gs", &map),
            Some("git status".to_string())
        );
        assert_eq!(
            resolve_expansion("foo", &map),
            Some("echo backfoo".to_string())
        );

        map.insert("a".to_string(), "b".to_string());
        map.insert("b".to_string(), "a".to_string());
        let res = resolve_expansion("a", &map).unwrap();
        assert!(res.contains("Loop detected"));
    }

    #[test]
    fn test_compare_ignore_case() {
        assert_eq!(compare_ignore_case("apple", "Apple"), Ordering::Equal);
        assert_eq!(compare_ignore_case("a", "B"), Ordering::Less);
        assert_eq!(compare_ignore_case("B", "a"), Ordering::Greater);
    }

    #[test]
    fn test_validate_command() {
        assert!(validate_command("ls", &[]));
        assert!(validate_command("cd", &[]));
        assert!(!validate_command("thiscommandcertainlydoesnotexist", &[]));
    }

    #[test]
    fn test_extract_tags() {
        assert_eq!(
            extract_tags(Some("@work @utils info")),
            vec!["work", "utils"]
        );
        assert_eq!(extract_tags(None), Vec::<String>::new());
    }

    #[test]
    fn test_clean_description() {
        assert_eq!(
            clean_description(Some("@work info".into())),
            Some("info".into())
        );
        assert_eq!(clean_description(Some("@work @utils".into())), None);
    }
}