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 215 216 217 218 219 220
//! Contains config related structs and functions.
/// Struct for Apt Configuration
///
/// All apt configuration methods do not require this struct.
/// You can call the bindings directly from raw::apt if you would like.
pub struct Config {}
impl Default for Config {
/// Create a new config object and safely init the config system.
///
/// If you initialize the struct without `new()` or `default()`
/// You will need to manually initialize the config system.
fn default() -> Self { Self::new() }
}
impl Config {
/// Create a new config object and safely init the config system.
///
/// If you initialize the struct without `new()` or `default()`
/// You will need to manually initialize the config system.
pub fn new() -> Self {
init_config_system();
Self {}
}
/// Clears all configuratations, re-initialize, and returns the config
/// object.
pub fn new_clear() -> Self {
raw::config_clear_all();
Self::new()
}
/// Resets the configurations.
///
/// If you'd like to clear everything and NOT reinit
/// you can call `self.clear_all` or `raw::config_clear_all` directly
pub fn reset(&self) {
self.clear_all();
init_config_system();
}
/// Clears all values from a key.
///
/// If the value is a list, the entire list is cleared.
/// If you need to clear 1 value from a list see `self.clear_value`
pub fn clear(&self, key: &str) { raw::config_clear(key.to_string()); }
/// Clear a single value from a list.
/// Used for removing one item in an apt configuruation list
pub fn clear_value(&self, key: &str, value: &str) {
raw::config_clear_value(key.to_string(), value.to_string());
}
/// Clears all configuratations.
///
/// This will leave you with an empty configuration object
/// and most things probably won't work right.
pub fn clear_all(&self) { raw::config_clear_all(); }
/// Returns a string dump of configuration options separated by `\n`
pub fn dump(&self) -> String { raw::config_dump() }
/// Find a key and return it's value as a string.
///
/// default is what will be returned if nothing is found.
pub fn find(&self, key: &str, default: &str) -> String {
raw::config_find(key.to_string(), default.to_string())
}
/// Exactly like find but takes no default and returns an option instead.
pub fn get(&self, key: &str) -> Option<String> {
let value = raw::config_find(key.to_string(), "".to_string());
if value.is_empty() {
return None;
}
Some(value)
}
/// Find a file and return it's value as a string.
///
/// default is what will be returned if nothing is found.
///
/// `key = "Dir::Cache::pkgcache"` should return
/// `/var/cache/apt/pkgcache.bin`
///
/// There is not much difference in `self.dir` and `self.file`
///
/// `dir` will return with a trailing `/` where `file` will not.
pub fn file(&self, key: &str, default: &str) -> String {
raw::config_find_file(key.to_string(), default.to_string())
}
/// Find a directory and return it's value as a string.
///
/// default is what will be returned if nothing is found.
///
/// `key = "Dir::Etc::sourceparts"` should return `/etc/apt/sources.list.d/`
///
/// There is not much difference in `self.dir` and `self.file`
///
/// `dir` will return with a trailing `/` where `file` will not.
pub fn dir(&self, key: &str, default: &str) -> String {
raw::config_find_dir(key.to_string(), default.to_string())
}
/// Same as find, but for boolean values.
pub fn bool(&self, key: &str, default: bool) -> bool {
raw::config_find_bool(key.to_string(), default)
}
/// Same as find, but for i32 values.
pub fn int(&self, key: &str, default: i32) -> i32 {
raw::config_find_int(key.to_string(), default)
}
/// Return a vector for an Apt configuration list.
///
/// An example of a common key that contains a list `raw::NeverAutoRemove`.
pub fn find_vector(&self, key: &str) -> Vec<String> { raw::config_find_vector(key.to_string()) }
/// Simply check if a key exists.
pub fn contains(&self, key: &str) -> bool { raw::config_exists(key.to_string()) }
/// Set the given key to the specified value.
pub fn set(&self, key: &str, value: &str) {
raw::config_set(key.to_string(), value.to_string())
}
/// Add strings from a vector into an apt configuration list.
///
/// If the configuration key is not a list,
/// you will receive a vector with one item.
///
/// Example:
/// ```
/// use rust_apt::config::Config;
/// let config = Config::new();
///
/// let apt_list = vec!["This", "is", "my", "apt", "list"];
/// // Using "AptList" here will not work and will panic.
/// config.set_vector("AptList", &apt_list);
/// ```
pub fn set_vector(&self, key: &str, values: &Vec<&str>) {
let mut vec_key = String::from(key);
if !vec_key.ends_with("::") {
vec_key.push_str("::");
}
for value in values {
raw::config_set(vec_key.to_string(), value.to_string());
}
}
}
/// Safely Init Apt Configuration and System.
///
/// If the configuration has already been initialized, don't reinit.
///
/// This could cause some things to get reset.
pub fn init_config_system() {
if raw::config_find("APT".to_string(), "".to_string()).is_empty() {
raw::init_config();
}
raw::init_system();
}
/// This module contains the bindings and structs shared with c++
#[cxx::bridge]
pub mod raw {
unsafe extern "C++" {
include!("rust-apt/apt-pkg-c/configuration.h");
/// init the system. This must occur before creating the cache.
pub fn init_system();
/// init the config. This must occur before creating the cache.
pub fn init_config();
/// Returns a string dump of configuration options separated by `\n`
pub fn config_dump() -> String;
/// Find a key and return it's value as a string.
pub fn config_find(key: String, default_value: String) -> String;
/// Find a file and return it's value as a string.
pub fn config_find_file(key: String, default_value: String) -> String;
/// Find a directory and return it's value as a string.
pub fn config_find_dir(key: String, default_value: String) -> String;
/// Same as find, but for boolean values.
pub fn config_find_bool(key: String, default_value: bool) -> bool;
/// Same as find, but for i32 values.
pub fn config_find_int(key: String, default_value: i32) -> i32;
/// Return a vector for an Apt configuration list.
pub fn config_find_vector(key: String) -> Vec<String>;
/// Set the given key to the specified value.
pub fn config_set(key: String, value: String);
/// Simply check if a key exists.
pub fn config_exists(key: String) -> bool;
/// Clears all values from a key.
///
/// If the value is a list, the entire list is cleared.
/// If you need to clear 1 value from a list see `config_clear_value`
pub fn config_clear(key: String);
/// Clears all configuratations.
pub fn config_clear_all();
/// Clear a single value from a list.
/// Used for removing one item in an apt configuruation list
pub fn config_clear_value(key: String, value: String);
}
}