-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
244 additions
and
15 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env rash | ||
|
||
- name: Install Rust dependencies | ||
pacman: | ||
executable: "{{ rash.dir }}/../rash_core/tests/mocks/pacman.rh" | ||
extra_args: "--nodeps --nodeps" | ||
force: true | ||
name: | ||
- rustup | ||
- bpftrace | ||
state: latest |
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,211 @@ | ||
/// ANCHOR: module | ||
/// # pacman | ||
/// | ||
/// Manage packages with the pacman package manager, which is used by Arch Linux and its variants. | ||
/// | ||
/// ## Attributes | ||
/// | ||
/// ```yaml | ||
/// check_mode: | ||
/// support: always | ||
/// ``` | ||
/// ANCHOR_END: module | ||
/// ANCHOR: examples | ||
/// ## Example | ||
/// | ||
/// ```yaml | ||
/// - name: Install package rustup from repo | ||
/// pacman: | ||
/// name: rustup | ||
/// state: present | ||
/// | ||
/// - pacman: | ||
/// executable: yay | ||
/// name: | ||
/// - rash | ||
/// - timer-rs | ||
/// state: present | ||
/// ``` | ||
/// ANCHOR_END: examples | ||
use crate::error::{Error, ErrorKind, Result}; | ||
use crate::modules::{parse_params, Module, ModuleResult}; | ||
use crate::modules::utils::default_false; | ||
use crate::vars::Vars; | ||
|
||
#[cfg(feature = "docs")] | ||
use rash_derive::DocJsonSchema; | ||
|
||
#[cfg(feature = "docs")] | ||
use schemars::schema::RootSchema; | ||
#[cfg(feature = "docs")] | ||
use schemars::JsonSchema; | ||
use serde::Deserialize; | ||
use serde_with::{serde_as, OneOrMany}; | ||
use serde_yaml::Value; | ||
#[cfg(feature = "docs")] | ||
use strum_macros::{Display, EnumString}; | ||
|
||
|
||
fn default_executable() -> Option<String> { | ||
Some("pacman".to_string()) | ||
} | ||
|
||
#[derive(Debug, PartialEq, Deserialize)] | ||
#[cfg_attr(feature = "docs", derive(EnumString, Display, JsonSchema))] | ||
#[serde(rename_all = "lowercase")] | ||
enum State { | ||
Absent, | ||
Installed, | ||
Latest, | ||
Present, | ||
Removed, | ||
} | ||
|
||
impl Default for State { | ||
fn default() -> Self { | ||
State::Present | ||
} | ||
} | ||
|
||
fn default_state() -> Option<State> { | ||
Some(State::default()) | ||
} | ||
|
||
#[serde_as] | ||
#[derive(Debug, PartialEq, Deserialize)] | ||
#[cfg_attr(feature = "docs", derive(JsonSchema, DocJsonSchema))] | ||
#[serde(deny_unknown_fields)] | ||
pub struct Params { | ||
/// Path of the binary to use. This can either be `pacman` or a pacman compatible AUR helper. | ||
/// **[default: `"pacman"`]** | ||
#[serde(default = "default_executable")] | ||
executable: Option<String>, | ||
/// Additional option to pass to executable. | ||
extra_args: Option<String>, | ||
/// When removing packages, forcefully remove them, without any checks. | ||
/// Same as extra_args=”–nodeps –nodeps”. When combined with update_cache,] | ||
/// force a refresh of all package databases. Same as update_cache_extra_args=”–refresh –refresh”. | ||
/// **[default: `false`]** | ||
#[serde(default = "default_false")] | ||
force: Option<bool>, | ||
/// Name or list of names of the package(s) or file(s) to install, upgrade, or remove. | ||
#[serde_as(deserialize_as = "OneOrMany<_>")] | ||
#[serde(default)] | ||
name: Vec<String>, | ||
/// Whether to install (`present`, `latest`), or remove (`absent`) a package. | ||
/// `present` will simply ensure that a desired package is installed. | ||
/// `latest` will update the specified package if it is not of the latest available version. | ||
/// `absent` will remove the specified package. | ||
/// **[default: `"present"`]** | ||
#[serde(default = "default_state")] | ||
state: Option<State>, | ||
} | ||
|
||
impl Default for Params { | ||
fn default() -> Self { | ||
Params { | ||
executable: Some("pacman".to_string()), | ||
extra_args: None, | ||
force: Some(false), | ||
name: Vec::new(), | ||
state: Some(State::Present), | ||
} | ||
} | ||
} | ||
|
||
|
||
#[derive(Debug)] | ||
pub struct Pacman; | ||
|
||
|
||
impl Module for Pacman { | ||
fn get_name(&self) -> &str { | ||
"pacman" | ||
} | ||
|
||
fn exec(&self, optional_params: Value, vars: Vars, _check_mode: bool) -> Result<(ModuleResult, Vars)> { | ||
Ok(( | ||
pacman(parse_params(optional_params)?, &vars)?, | ||
vars, | ||
)) | ||
} | ||
|
||
#[cfg(feature = "docs")] | ||
fn get_json_schema(&self) -> Option<RootSchema> { | ||
Some(Params::get_json_schema()) | ||
} | ||
} | ||
|
||
fn pacman(params: Params, vars: &Vars) -> Result<ModuleResult> { | ||
Ok(ModuleResult { | ||
changed: false, | ||
output: None, | ||
extra: None, | ||
}) | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_parse_params() { | ||
let yaml: Value = serde_yaml::from_str( | ||
r#" | ||
name: rustup | ||
state: present | ||
"#, | ||
) | ||
.unwrap(); | ||
let params: Params = parse_params(yaml).unwrap(); | ||
assert_eq!( | ||
params, | ||
Params { | ||
name: vec!["rustup".to_string()], | ||
state: Some(State::Present), | ||
..Default::default() | ||
} | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_parse_params_all_values() { | ||
let yaml: Value = serde_yaml::from_str( | ||
r#" | ||
executable: yay | ||
extra_args: "--nodeps --nodeps" | ||
force: true | ||
name: | ||
- rustup | ||
- bpftrace | ||
state: latest | ||
"#, | ||
) | ||
.unwrap(); | ||
let params: Params = parse_params(yaml).unwrap(); | ||
assert_eq!( | ||
params, | ||
Params { | ||
executable: Some("yay".to_string()), | ||
extra_args: Some("--nodeps --nodeps".to_string()), | ||
force: Some(true), | ||
name: vec!["rustup".to_string(), "bpftrace".to_string()], | ||
state: Some(State::Latest), | ||
} | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_parse_params_random_field() { | ||
let yaml: Value = serde_yaml::from_str( | ||
r#" | ||
name: rustup | ||
foo: yea | ||
"#, | ||
) | ||
.unwrap(); | ||
let error = parse_params::<Params>(yaml).unwrap_err(); | ||
assert_eq!(error.kind(), ErrorKind::InvalidData); | ||
} | ||
} |
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,9 @@ | ||
|
||
pub fn default_false() -> Option<bool> { | ||
Some(false) | ||
} | ||
|
||
|
||
pub fn default_true() -> Option<bool> { | ||
Some(true) | ||
} |