git_workty/commands/
install_man.rs

1use crate::Cli;
2use anyhow::{Context, Result};
3use clap::CommandFactory;
4use clap_mangen::Man;
5use dialoguer::theme::ColorfulTheme;
6use dialoguer::Confirm;
7use std::fs;
8
9pub fn execute(yes: bool) -> Result<()> {
10    let home = dirs::home_dir().context("Could not determine home directory")?;
11    let target_dir = home.join(".local/share/man/man1");
12    let target_file = target_dir.join("git-workty.1");
13
14    if !yes {
15        let theme = ColorfulTheme::default();
16        let confirmed = Confirm::with_theme(&theme)
17            .with_prompt(format!("Install manpage to {}?", target_file.display()))
18            .default(true)
19            .interact()?;
20
21        if !confirmed {
22            println!("Aborted.");
23            return Ok(());
24        }
25    }
26
27    fs::create_dir_all(&target_dir)
28        .with_context(|| format!("Failed to create directory: {}", target_dir.display()))?;
29
30    let cmd = Cli::command();
31    let man = Man::new(cmd);
32
33    let mut buffer: Vec<u8> = Default::default();
34    man.render(&mut buffer)?;
35
36    fs::write(&target_file, buffer)
37        .with_context(|| format!("Failed to write manpage to {}", target_file.display()))?;
38
39    println!("Manpage installed to {}", target_file.display());
40    println!("You may need to add ~/.local/share/man to your MANPATH if it's not already there.");
41
42    Ok(())
43}