Skip to content

Add Config::{add,delete}_property_value() #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions src/smf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,36 @@ impl Config {
pub fn set_property<S: AsRef<str>>(fmri: S) -> ConfigSetProperty {
ConfigSetProperty::new(fmri.as_ref().into())
}

/// Builds a [ConfigAddPropertyValue] object.
///
/// ```no_run
/// let property = smf::Property::new(
/// smf::PropertyName::new("group", "comment").unwrap(),
/// smf::PropertyValue::Astring("hello".to_string())
/// );
/// smf::Config::add_property_value("my_service:default")
/// .run(property)
/// .unwrap();
/// ```
pub fn add_property_value<S: AsRef<str>>(fmri: S) -> ConfigAddPropertyValue {
ConfigAddPropertyValue::new(fmri.as_ref().into())
}

/// Builds a [ConfigDeletePropertyValue] object.
///
/// Note that the property value passed to
/// [ConfigDeletePropertyValue::run()] is a glob pattern.
///
/// ```no_run
/// let property_name = smf::PropertyName::new("group", "comment").unwrap();
/// smf::Config::delete_property_value("my_service:default")
/// .run(&property_name, "hello*")
/// .unwrap();
/// ```
pub fn delete_property_value<S: AsRef<str>>(fmri: S) -> ConfigDeletePropertyValue {
ConfigDeletePropertyValue::new(fmri.as_ref().into())
}
}

trait ConfigSubcommand {
Expand Down Expand Up @@ -729,6 +759,59 @@ impl ConfigSetProperty {
}
}

/// Created by [Config::add_property_value], adds a property value for a
/// service.
pub struct ConfigAddPropertyValue {
fmri: String,
}

impl ConfigAddPropertyValue {
fn new(fmri: String) -> Self {
Self { fmri }
}

/// Runs the add property value command.
pub fn run(&self, property: Property) -> Result<(), ConfigError> {
let name = property.name.to_string();
let value = property.value.to_string();
let args = vec!["-s", &self.fmri, "addpropvalue", &name, &value];
std::process::Command::new("/usr/sbin/svccfg")
.env_clear()
.args(args)
.output()
.map_err(ConfigError::Command)?
.read_stdout()
.map(|_| ())
.map_err(|err| err.into())
}
}

/// Created by [Config::delete_property_value], adds a property value for a
/// service.
pub struct ConfigDeletePropertyValue {
fmri: String,
}

impl ConfigDeletePropertyValue {
fn new(fmri: String) -> Self {
Self { fmri }
}

/// Runs the delete property value command.
pub fn run(&self, property_name: &PropertyName, value: &str) -> Result<(), ConfigError> {
let name = property_name.to_string();
let args = vec!["-s", &self.fmri, "delpropvalue", &name, value];
std::process::Command::new("/usr/sbin/svccfg")
.env_clear()
.args(args)
.output()
.map_err(ConfigError::Command)?
.read_stdout()
.map(|_| ())
.map_err(|err| err.into())
}
}

/*
*
* SVCADM
Expand Down