summaryrefslogtreecommitdiff
path: root/src/symlink.rs
blob: 68a804f78698dc539b0c391bbda94aa59919788e (plain)
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
use std::path::PathBuf;
use tokio::fs;

use crate::{AppImage, Result};

#[derive(Debug, Default)]
pub struct SymlinkManager {}

impl SymlinkManager {
    pub fn new() -> Self {
        Self {}
    }
    pub async fn remove(&self, executable: &str) -> Result<()> {
        let home = std::env::var("HOME")?;
        let symlink_path = PathBuf::from(home).join(".local/bin").join(executable);

        fs::remove_file(symlink_path).await?;

        Ok(())
    }
    pub async fn create(&self, appimage: &AppImage) -> Result<()> {
        let home = std::env::var("HOME")?;
        let local_bin = PathBuf::from(home).join(".local/bin");

        fs::create_dir_all(&local_bin).await?;

        let symlink_path = local_bin.join(&appimage.executable);

        #[cfg(unix)]
        {
            use tokio::fs;

            if symlink_path.exists() {
                fs::remove_file(&symlink_path).await?;
            }

            std::os::unix::fs::symlink(&appimage.file_path, &symlink_path)?;
        }

        Ok(())
    }
}