-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from tsirysndr/feat/slackpkg-installer
feat: add slackpkg installer
- Loading branch information
Showing
8 changed files
with
235 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
use anyhow::Error; | ||
use crosup_macros::{check_version, exec_sh_with_output, slackpkg_install}; | ||
use crosup_types::slackpkg::Package; | ||
use owo_colors::OwoColorize; | ||
use ssh2::Session; | ||
use std::{any::Any, io::BufRead, process::Stdio}; | ||
|
||
use super::Installer; | ||
|
||
#[derive(Default, Clone)] | ||
pub struct SlackpkgInstaller { | ||
pub name: String, | ||
pub version: String, | ||
pub dependencies: Vec<String>, | ||
pub slackpkg_dependencies: Vec<String>, | ||
pub packages: Option<Vec<String>>, | ||
pub postinstall: Option<String>, | ||
pub version_check: Option<String>, | ||
pub provider: String, | ||
pub session: Option<Session>, | ||
} | ||
|
||
impl From<Package> for SlackpkgInstaller { | ||
fn from(pkg: Package) -> Self { | ||
Self { | ||
name: pkg.name, | ||
packages: pkg.packages, | ||
slackpkg_dependencies: pkg.depends_on.unwrap_or(vec![]), | ||
provider: "slackpkg".into(), | ||
version_check: pkg.version_check, | ||
..Default::default() | ||
} | ||
} | ||
} | ||
|
||
impl SlackpkgInstaller { | ||
pub fn install_dependencies(&self) -> Result<(), Error> { | ||
if self.slackpkg_dependencies.is_empty() { | ||
return Ok(()); | ||
} | ||
|
||
println!( | ||
"-> Installing dependencies for {}", | ||
self.name.bright_green() | ||
); | ||
let deps = self.slackpkg_dependencies.join(" "); | ||
slackpkg_install!(deps, self.session.clone()); | ||
Ok(()) | ||
} | ||
|
||
fn postinstall(&self) -> Result<(), Error> { | ||
if let Some(command) = self.postinstall.clone() { | ||
println!( | ||
"-> Running postinstall command:\n{}", | ||
command.bright_green() | ||
); | ||
for cmd in command.split("\n") { | ||
exec_sh_with_output!(cmd, self.session.clone()); | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl Installer for SlackpkgInstaller { | ||
fn install(&self) -> Result<(), Error> { | ||
if self.is_installed().is_ok() { | ||
if self.is_installed().unwrap() { | ||
println!( | ||
"-> {} is already installed, skipping", | ||
self.name().bright_green() | ||
); | ||
return Ok(()); | ||
} | ||
} | ||
|
||
self.install_dependencies()?; | ||
|
||
if let Some(packages) = self.packages.clone() { | ||
let packages = packages.join(" "); | ||
let command = format!("sudo slackpkg install {}", packages); | ||
println!("-> Running {}", command.bright_green()); | ||
slackpkg_install!(packages, self.session.clone()); | ||
} | ||
|
||
self.postinstall()?; | ||
Ok(()) | ||
} | ||
|
||
fn is_installed(&self) -> Result<bool, Error> { | ||
if let Some(command) = self.version_check.clone() { | ||
println!( | ||
"-> Checking if {} is already installed", | ||
self.name.bright_green() | ||
); | ||
check_version!(self, command, self.session.clone()); | ||
return Ok(true); | ||
} | ||
Ok(false) | ||
} | ||
|
||
fn name(&self) -> &str { | ||
&self.name | ||
} | ||
|
||
fn version(&self) -> &str { | ||
&self.version | ||
} | ||
|
||
fn dependencies(&self) -> Vec<String> { | ||
self.dependencies.clone() | ||
} | ||
|
||
fn is_default(&self) -> bool { | ||
true | ||
} | ||
|
||
fn provider(&self) -> &str { | ||
&self.provider | ||
} | ||
|
||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use indexmap::IndexMap; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone, Default)] | ||
pub struct SlackpkgConfiguration { | ||
#[serde(serialize_with = "hcl::ser::labeled_block")] | ||
pub pkg: IndexMap<String, Package>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone, Default)] | ||
pub struct Package { | ||
#[serde(skip_serializing, skip_deserializing)] | ||
pub name: String, | ||
|
||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub packages: Option<Vec<String>>, | ||
|
||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub depends_on: Option<Vec<String>>, | ||
|
||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub postinstall: Option<String>, | ||
|
||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub version_check: Option<String>, | ||
} |