1use std::fs;
2use std::io::Write;
3use std::path::{Path, PathBuf};
4
5use anyhow::{Context, Result};
6
7pub fn fish_script() -> &'static str {
8 include_str!("../../assets/fish/a.fish")
9}
10
11pub fn install() -> Result<PathBuf> {
12 let home = std::env::var_os("HOME")
13 .map(PathBuf::from)
14 .context("HOME is not set")?;
15 install_to(&home)
16}
17
18pub fn install_to(home: &Path) -> Result<PathBuf> {
19 let directory = home.join(".config/fish/conf.d");
20 fs::create_dir_all(&directory)?;
21 let destination = directory.join("a.fish");
22 let mut temporary = tempfile::NamedTempFile::new_in(&directory)?;
23 temporary.write_all(fish_script().as_bytes())?;
24 temporary.as_file().sync_all()?;
25 temporary
26 .persist(&destination)
27 .map_err(|error| error.error)?;
28 Ok(destination)
29}