shine-cli 1.1.2

Cross-platform CLI for managed shell commands, app configs, and machine setup
Documentation
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
use super::metadata;
use crate::colors;

#[derive(Debug, Default)]
pub struct ShellUpgradeReport {
    pub snapshots_updated: usize,
    pub templates_updated: usize,
    pub links_created: usize,
    pub links_updated: usize,
    pub link_conflicts: usize,
    pub path_changed: bool,
}

pub(super) fn preset_extract_summary_parts(report: &crate::presets::ExtractReport) -> Vec<String> {
    let mut parts: Vec<String> = Vec::new();
    if !report.created.is_empty() {
        parts.push(colors::green(&format_file_action(
            report.created.len(),
            "created",
        )));
    }
    if !report.overwritten.is_empty() {
        parts.push(colors::green(&format_file_action(
            report.overwritten.len(),
            "updated",
        )));
    }
    if !report.skipped.is_empty() {
        parts.push(colors::dim(&format_file_action(
            report.skipped.len(),
            "skipped",
        )));
    }
    parts
}

pub(super) fn unlink_report_summary_parts(
    unlink_report: &crate::bin_links::UnlinkReport,
) -> Vec<String> {
    let mut parts: Vec<String> = Vec::new();
    if !unlink_report.removed.is_empty() {
        parts.push(colors::green(&format!(
            "{} removed",
            unlink_report.removed.len()
        )));
    }
    if !unlink_report.skipped.is_empty() {
        parts.push(colors::dim(&format!(
            "{} skipped",
            unlink_report.skipped.len()
        )));
    }
    parts
}

pub(super) fn remove_report_summary_parts(
    remove_report: &crate::presets::RemoveReport,
) -> Vec<String> {
    let mut parts: Vec<String> = Vec::new();
    if !remove_report.removed.is_empty() {
        parts.push(colors::green(&format_file_action(
            remove_report.removed.len(),
            "removed",
        )));
    }
    if !remove_report.skipped.is_empty() {
        parts.push(colors::dim(&format_file_action(
            remove_report.skipped.len(),
            "skipped",
        )));
    }
    parts
}

pub(super) fn link_report_summary_parts(link_report: &crate::bin_links::LinkReport) -> Vec<String> {
    let mut parts: Vec<String> = Vec::new();
    if !link_report.created.is_empty() {
        parts.push(colors::green(&format!(
            "{} created",
            link_report.created.len()
        )));
    }
    if !link_report.overwritten.is_empty() {
        parts.push(colors::green(&format!(
            "{} updated",
            link_report.overwritten.len()
        )));
    }
    if !link_report.skipped.is_empty() {
        parts.push(colors::dim(&format!(
            "{} up to date",
            link_report.skipped.len()
        )));
    }
    if !link_report.conflicts.is_empty() {
        parts.push(colors::yellow(&format!(
            "{} conflicts",
            link_report.conflicts.len()
        )));
    }
    if parts.is_empty() {
        parts.push(colors::dim("0 linked"));
    }
    parts
}

pub(super) fn upgrade_link_report_summary_parts(
    link_report: &crate::bin_links::LinkReport,
    verbose: bool,
) -> Vec<String> {
    let mut parts: Vec<String> = Vec::new();
    if !link_report.created.is_empty() {
        parts.push(colors::green(&format!(
            "{} created",
            link_report.created.len()
        )));
    }
    if !link_report.overwritten.is_empty() {
        parts.push(colors::green(&format!(
            "{} updated",
            link_report.overwritten.len()
        )));
    }
    if verbose && !link_report.skipped.is_empty() {
        parts.push(colors::dim(&format!(
            "{} up to date",
            link_report.skipped.len()
        )));
    }
    if !link_report.conflicts.is_empty() {
        parts.push(colors::yellow(&format!(
            "{} conflicts",
            link_report.conflicts.len()
        )));
    }
    parts
}

fn format_file_action(count: usize, action: &str) -> String {
    let noun = if count == 1 { "file" } else { "files" };
    format!("{count} {noun} {action}")
}

pub async fn handle_list(config: &crate::config::Config) -> anyhow::Result<()> {
    handle_list_with_presets_note(config, true).await
}

#[doc(hidden)]
pub async fn handle_list_with_presets_note(
    config: &crate::config::Config,
    print_presets_note: bool,
) -> anyhow::Result<()> {
    if print_presets_note {
        crate::config::print_presets_note(config);
    }
    let categories = if config.is_external_presets {
        metadata::load_installed_categories(config, None).await?
    } else {
        metadata::load_embedded_categories(None)?
    };

    if categories.is_empty() {
        println!("{}", colors::dim("No shell preset categories found."));
        return Ok(());
    }

    println!("{}\n", colors::bold("Shell Preset Categories"));

    let bun_available = crate::platform::command_exists_on_path("bun");

    for cat in &categories {
        let word = if cat.files.len() == 1 {
            "script"
        } else {
            "scripts"
        };
        println!(
            "  {}  {}",
            cat.name,
            colors::dim(&format!("{} {}", cat.files.len(), word))
        );

        let names: Vec<&str> = cat.files.iter().map(|s| s.command_name.as_str()).collect();
        let max_name = names.iter().map(|s| s.len()).max().unwrap_or(0);
        let gap = 4;
        let desc_col = max_name + gap;
        let continuation_indent = " ".repeat(4 + desc_col);

        for (script, name) in cat.files.iter().zip(names.iter()) {
            let padding = " ".repeat(desc_col - name.len());
            match script.description.as_slice() {
                [] => println!("    {name}"),
                [first, rest @ ..] => {
                    println!("    {name}{padding}{first}");
                    for line in rest {
                        if line.is_empty() {
                            println!();
                        } else {
                            println!("{continuation_indent}{line}");
                        }
                    }
                }
            }
            if script.runtime == crate::bin_links::LinkRuntime::Bun {
                let status = if bun_available {
                    colors::green("available")
                } else {
                    colors::yellow("not found on PATH")
                };
                println!(
                    "{continuation_indent}{} {status}",
                    colors::dim("runtime: bun ·")
                );
            }
            println!();
        }
    }

    println!(
        "{}",
        colors::dim("Run `shine install shell/<CATEGORY>` to install a specific category.")
    );
    println!(
        "{}",
        colors::dim("Run `shine shell install` to install all.")
    );
    println!();
    println!(
        "{}",
        colors::dim(
            "After installation, commands are available directly by name (e.g. `setproxy`)."
        )
    );

    Ok(())
}

pub async fn handle_info(config: &crate::config::Config, target: &str) -> anyhow::Result<()> {
    use anyhow::bail;

    crate::config::print_presets_note(config);
    let categories = metadata::load_active_categories(config, None).await?;
    let target = target.trim();
    if target.is_empty() {
        bail!("shell info target must not be empty");
    }

    let (category, files) = if let Some(category) = categories.iter().find(|cat| cat.name == target)
    {
        (category, category.files.iter().collect::<Vec<_>>())
    } else if let Some((category_name, command_name)) = target.split_once('/') {
        let Some(category) = categories.iter().find(|cat| cat.name == category_name) else {
            bail!("shell preset category not found: {category_name}");
        };
        let Some(file) = category
            .files
            .iter()
            .find(|file| file.command_name == command_name)
        else {
            bail!("shell preset command not found: {target}");
        };
        (category, vec![file])
    } else {
        let matches = categories
            .iter()
            .flat_map(|category| {
                category
                    .files
                    .iter()
                    .filter(move |file| file.command_name == target)
                    .map(move |file| (category, file))
            })
            .collect::<Vec<_>>();
        match matches.as_slice() {
            [] => bail!(
                "shell preset target not found: {target}\n\nRun `shine shell list` to see available presets."
            ),
            [(category, file)] => (*category, vec![*file]),
            _ => {
                let choices = matches
                    .iter()
                    .map(|(category, file)| format!("{}/{}", category.name, file.command_name))
                    .collect::<Vec<_>>()
                    .join(", ");
                bail!("ambiguous shell preset target `{target}`; use one of: {choices}");
            }
        }
    };

    let rows = crate::status::build_shell_rows(config).await?;
    println!("{}", colors::bold(&category.name));
    if let Some(description) = &category.description {
        println!("  {}", colors::dim(description));
    }

    let bun_available = crate::platform::command_exists_on_path("bun");
    let mut any_installed = false;
    for file in files {
        let label = format!("{}/{}", category.name, file.command_name);
        let row = rows.iter().find(|row| row.label == label);
        let command_path = crate::bin_links::command_path_for_name(
            config.bin_dir(),
            std::ffi::OsStr::new(&file.command_name),
        );
        any_installed |= command_path.exists()
            || tokio::fs::symlink_metadata(&command_path)
                .await
                .is_ok_and(|metadata| metadata.file_type().is_symlink());
        println!();
        println!("  {}", colors::bold(&file.command_name));
        println!(
            "    {:<12} shell/{}/{}",
            "Source",
            category.name,
            file.source_rel.display()
        );
        let runtime = match file.runtime {
            crate::bin_links::LinkRuntime::Native => "native".to_string(),
            crate::bin_links::LinkRuntime::Bun if bun_available => "bun (available)".to_string(),
            crate::bin_links::LinkRuntime::Bun => "bun (not found on PATH)".to_string(),
        };
        println!("    {:<12} {runtime}", "Runtime");
        println!(
            "    {:<12} {}",
            "Transforms",
            if file.transforms.is_empty() {
                "none".to_string()
            } else {
                file.transforms.join(", ")
            }
        );
        println!(
            "    {:<12} {}",
            "Environment",
            if file.env.is_empty() {
                "none".to_string()
            } else {
                file.env
                    .iter()
                    .map(crate::env::EnvVarSpec::to_with_arg)
                    .collect::<Vec<_>>()
                    .join(", ")
            }
        );
        println!(
            "    {:<12} {}",
            "Status",
            row.map_or("not installed", |row| row.status_text)
        );
        for (index, line) in file.description.iter().enumerate() {
            println!(
                "    {:<12} {}",
                if index == 0 { "Description" } else { "" },
                line
            );
        }
    }

    println!();
    if any_installed {
        println!(
            "{}",
            colors::dim(&format!(
                "Run `shine install shell/{} --replace-managed` to repair this category.",
                category.name
            ))
        );
    } else {
        println!(
            "{}",
            colors::dim(&format!(
                "Run `shine shell install {}` to install this category.",
                category.name
            ))
        );
    }
    Ok(())
}

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

    #[tokio::test]
    async fn embedded_shell_info_accepts_category_command_and_canonical_target() {
        let dir = crate::test_support::make_temp_dir("shine-shell-info").await;
        let config = crate::test_support::test_config(&dir);

        handle_info(&config, "proxy").await.unwrap();
        handle_info(&config, "setproxy").await.unwrap();
        handle_info(&config, "proxy/setproxy").await.unwrap();

        tokio::fs::remove_dir_all(dir).await.unwrap();
    }

    #[tokio::test]
    async fn shell_info_rejects_unknown_and_empty_targets() {
        let dir = crate::test_support::make_temp_dir("shine-shell-info-errors").await;
        let config = crate::test_support::test_config(&dir);

        assert!(handle_info(&config, "").await.is_err());
        assert!(handle_info(&config, "not-a-preset").await.is_err());

        tokio::fs::remove_dir_all(dir).await.unwrap();
    }
}