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
// Copyright 2020 Sebastian Wiesner <sebastian@swsnr.de>

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

//! Install binaries to $HOME.
//!
//! Not a package manager.

#![deny(warnings, clippy::all, missing_docs)]

use std::path::PathBuf;
use std::process::Command;

use anyhow::{anyhow, Context, Error, Result};
use colored::Colorize;
use fehler::throws;
use versions::Versioning;

pub use dirs::*;
pub use manifest::{Manifest, ManifestRepo, ManifestStore};
pub use repos::HomebinRepos;

use crate::operations::{ApplyOperation, Operation};
use crate::tools::{manpath, path_contains};

mod checksum;
mod dirs;
mod process;
mod repos;
mod tools;

/// Manifest types and loading.
pub mod manifest;
/// Operations to apply manifests to a home directory.
pub mod operations;

/// Check whether the environment is ok, and print warnings to stderr if not.
///
/// This specifically checks whether `install_dirs` are contained in the relevant environment variables
/// such as `$PATH` or `$MANPATH`.
#[throws]
pub fn check_environment(install_dirs: &InstallDirs) -> () {
    match std::env::var_os("PATH") {
        None => eprintln!("{}", "WARNING: $PATH not set!".yellow().bold()),
        Some(path) => {
            if !path_contains(&path, install_dirs.bin_dir()) {
                eprintln!(
                    "{}\nAdd {} to $PATH in your shell profile.",
                    format!(
                        "WARNING: $PATH does not contain bin dir at {}",
                        install_dirs.bin_dir().display()
                    )
                    .yellow()
                    .bold(),
                    install_dirs.bin_dir().display()
                )
            }
        }
    };

    if !path_contains(&manpath()?, install_dirs.man_dir()) {
        eprintln!(
            "{}\nAdd {} to $MANPATH in your shell profile; see man 1 manpath for more information",
            format!(
                "WARNING: manpath does not contain man dir at {}",
                install_dirs.man_dir().display()
            )
            .yellow()
            .bold(),
            install_dirs.man_dir().display()
        );
    }
}

#[throws]
fn apply_operations(
    dirs: &HomebinProjectDirs,
    install_dirs: &mut InstallDirs,
    manifest: &Manifest,
    operations: &[Operation<'_>],
) -> () {
    let op_dirs = ManifestOperationDirs::for_manifest(dirs, install_dirs, manifest)?;
    op_dirs.ensure()?;
    for operation in operations {
        operation.apply_operation(&op_dirs)?;
    }
}
/// Install a manifest.
///
/// Apply the operations of a `manifest` against the given `install_dirs`; using the given project `dirs` for downloads.
pub fn install_manifest(
    dirs: &HomebinProjectDirs,
    install_dirs: &mut InstallDirs,
    manifest: &Manifest,
) -> Result<()> {
    apply_operations(
        dirs,
        install_dirs,
        manifest,
        &operations::install_manifest(manifest),
    )
}

/// Update a manifest
///
/// Apply the update operations of the `manifest` against the given install dirs.
pub fn update_manifest(
    dirs: &HomebinProjectDirs,
    install_dirs: &mut InstallDirs,
    manifest: &Manifest,
) -> Result<()> {
    apply_operations(
        dirs,
        install_dirs,
        manifest,
        &operations::update_manifest(manifest),
    )
}

/// Remove a manifest.
///
/// Apply the remove operations of the `manifest` against the given install dirs.
pub fn remove_manifest(
    dirs: &HomebinProjectDirs,
    install_dirs: &mut InstallDirs,
    manifest: &Manifest,
) -> Result<()> {
    apply_operations(
        dirs,
        install_dirs,
        manifest,
        &operations::remove_manifest(manifest),
    )
}

/// Get the installed version of the given manifest.
///
/// Attempt to invoke the version check denoted in the manifest, i.e. the given binary with the
/// version check arguments, and use the pattern to extract a version number.
///
/// Return `None` if the binary doesn't exist or its output doesn't match the pattern;
/// fail if we cannot invoke it for other reasons or if we fail to parse the version from other.
#[throws]
pub fn installed_manifest_version(dirs: &InstallDirs, manifest: &Manifest) -> Option<Versioning> {
    let args = &manifest.discover.version_check.args;
    let binary = dirs.bin_dir().join(&manifest.discover.binary);
    if binary.is_file() {
        let output = Command::new(&binary).args(args).output().with_context(|| {
            format!(
                "Failed to run {} with {:?}",
                binary.display(),
                &manifest.discover.version_check.args
            )
        })?;
        let pattern = manifest.discover.version_check.regex().with_context(|| {
            format!(
                "Version check for {} failed: Invalid regex {}",
                manifest.info.name, manifest.discover.version_check.pattern
            )
        })?;
        let output = std::str::from_utf8(&output.stdout).with_context(|| {
            format!(
                "Output of command {} with {:?} returned non-utf8 stdout: {:?}",
                binary.display(),
                args,
                output.stdout
            )
        })?;
        let version = pattern
            .captures(output)
            .and_then(|c| c.get(1))
            .map(|m| m.as_str());

        version
            .map(|s| {
                Versioning::new(s).ok_or_else(|| {
                    anyhow!(
                        "Output of command {} with {:?} returned invalid version {:?}",
                        binary.display(),
                        args,
                        version
                    )
                })
            })
            .transpose()?
    } else {
        None
    }
}

/// Whether the given manifest is outdated and needs updating.
///
/// Return the installed version if it's outdated, otherwise return None.
#[throws]
pub fn outdated_manifest_version(dirs: &InstallDirs, manifest: &Manifest) -> Option<Versioning> {
    installed_manifest_version(dirs, manifest)?
        .filter(|installed| installed < &manifest.info.version)
}

/// Get all files the `manifest` would install to `dirs`.
pub fn installed_files(dirs: &InstallDirs, manifest: &Manifest) -> Vec<PathBuf> {
    operations::operation_destinations(operations::install_manifest(manifest).iter())
        .map(|destination| dirs.path(destination.directory()).join(destination.name()))
        .collect()
}

/// Get all files that would be removed when removing `manifest`.
pub fn files_to_remove(dirs: &InstallDirs, manifest: &Manifest) -> Vec<PathBuf> {
    operations::operation_destinations(operations::remove_manifest(manifest).iter())
        .map(|destination| dirs.path(destination.directory()).join(destination.name()))
        .collect()
}