Skip to content

Commit

Permalink
feat: add support for specifying XDG directories in configuration com…
Browse files Browse the repository at this point in the history
…mands

- Added an `xdg_directory` parameter to all configuration commands (`apply`, `backup`, `delete`, `read`, `reset`, `write`) to specify the XDG directory to use (e.g., `config`, `data`, `cache`, `state`, `runtime`).
- Updated the configuration functions to handle the specified XDG directory, allowing operations on different XDG base directories.
- Modified `schema.json` to include the `xdg_directory` field with an enumerated list of valid XDG directories.
- Enhanced the flexibility of configuration management by supporting multiple XDG directories.
  • Loading branch information
HeitorAugustoLN committed Dec 14, 2024
1 parent caf2daa commit 4297c69
Show file tree
Hide file tree
Showing 12 changed files with 260 additions and 121 deletions.
File renamed without changes.
4 changes: 4 additions & 0 deletions examples/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"component": "com.system76.CosmicComp",
"version": 1,
"operation": "write",
"xdg_directory": "config",
"entries": {
"autotile": "true",
"autotile_behavior": "PerWorkspace"
Expand All @@ -14,6 +15,7 @@
"component": "com.system76.CosmicSettings",
"version": 1,
"operation": "write",
"xdg_directory": "config",
"entries": {
"active-page": "\"time-date\""
}
Expand All @@ -22,6 +24,7 @@
"component": "com.system76.CosmicComp",
"version": 1,
"operation": "delete",
"xdg_directory": "config",
"entries": [
"autotile_behavior"
]
Expand All @@ -30,6 +33,7 @@
"component": "com.system76.CosmicComp",
"version": 1,
"operation": "read",
"xdg_directory": "config",
"entries": [
"autotile"
]
Expand Down
10 changes: 10 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@
"delete"
]
},
"xdg_directory": {
"type": "string",
"enum": [
"config",
"data",
"cache",
"runtime",
"state"
]
},
"entries": {
"oneOf": [
{
Expand Down
22 changes: 19 additions & 3 deletions src/commands/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ impl Command for ApplyCommand {
match (entry.operation, entry.entries) {
(Operation::Write, EntryContent::WriteEntries(entries)) => {
for (key, value) in entries {
match write_configuration(&entry.component, &entry.version, &key, &value) {
match write_configuration(
&entry.component,
&entry.version,
&key,
&value,
&entry.xdg_directory,
) {
Ok(false) => {
if self.verbose {
println!(
Expand All @@ -65,7 +71,12 @@ impl Command for ApplyCommand {
}
(Operation::Read, EntryContent::ReadDeleteEntries(keys)) => {
for key in keys {
match read_configuration(&entry.component, &entry.version, &key) {
match read_configuration(
&entry.component,
&entry.version,
&key,
&entry.xdg_directory,
) {
Ok(content) => {
println!(
"{}/v{}/{}: {}",
Expand All @@ -84,7 +95,12 @@ impl Command for ApplyCommand {
}
(Operation::Delete, EntryContent::ReadDeleteEntries(keys)) => {
for key in keys {
match delete_configuration(&entry.component, &entry.version, &key) {
match delete_configuration(
&entry.component,
&entry.version,
&key,
&entry.xdg_directory,
) {
Ok(()) => {
if self.verbose {
println!(
Expand Down
107 changes: 67 additions & 40 deletions src/commands/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,65 +14,92 @@ pub struct BackupCommand {
/// Show which entries are being backed up.
#[arg(short, long)]
verbose: bool,
/// The XDG directories to backup (comma-separated) (e.g., 'config,cache,data').
#[arg(short, long, value_delimiter = ',', default_value = "config,data")]
xdg_dirs: Vec<String>,
}

impl Command for BackupCommand {
type Err = Error;

fn execute(&self) -> Result<(), Self::Err> {
let cosmic_path = get_cosmic_configurations()?;
let mut operations: HashMap<(String, u64), HashMap<String, String>> = HashMap::new();
let mut entry_count = 0;
let mut total_entry_count = 0;
let mut all_operations = Vec::new();

for entry in WalkDir::new(cosmic_path)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.path().is_file())
{
if let Some((component, version, entry_name)) = parse_configuration_path(entry.path()) {
match read_configuration(&component, &version, &entry_name) {
Ok(content) => {
if self.verbose {
println!("Backing up: {}/v{}/{}", component, version, entry_name);
}
for xdg_dir in &self.xdg_dirs {
let cosmic_path = get_cosmic_configurations(xdg_dir)?;
let mut operations: HashMap<(String, u64), HashMap<String, String>> = HashMap::new();
let mut entry_count = 0;

operations
.entry((component.clone(), version))
.or_default()
.insert(entry_name, content);
for entry in WalkDir::new(&cosmic_path)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.file_type().is_file())
{
if let Some((component, version, entry_name)) =
parse_configuration_path(&entry.path())
{
match read_configuration(&component, &version, &entry_name, xdg_dir) {
Ok(content) => {
if self.verbose {
println!(
"Backing up [{}]: {}/v{}/{}",
xdg_dir, component, version, entry_name
);
}

entry_count += 1;
}
Err(e) => {
if self.verbose {
println!(
"Failed to backup {}/v{}/{}: {}",
component, version, entry_name, e
);
operations
.entry((component, version))
.or_default()
.insert(entry_name, content);

entry_count += 1;
}
Err(e) => {
if self.verbose {
println!(
"Failed to backup [{}] {}/v{}/{}: {}",
xdg_dir, component, version, entry_name, e
);
}
}
}
}
}

let xdg_entries: Vec<Entry> = operations
.into_iter()
.map(|((component, version), entries)| Entry {
component,
version,
operation: Operation::Write,
entries: EntryContent::WriteEntries(entries),
xdg_directory: xdg_dir.to_string(),
})
.collect();

all_operations.extend(xdg_entries);
total_entry_count += entry_count;

if self.verbose {
println!(
"Completed backup for {} directory: {} entries",
xdg_dir, entry_count
);
}
}

let backup_data = ConfigFile {
schema: "https://raw.githubusercontent.com/HeitorAugustoLN/cosmic-ctl/refs/heads/main/schema.json".to_string(),
operations: operations
.into_iter()
.map(|((component, version), entries)| Entry {
component,
version,
operation: Operation::Write,
entries: EntryContent::WriteEntries(entries),
})
.collect(),
};
let json_data = serde_json::to_string_pretty(&backup_data)?;
schema: "https://raw.githubusercontent.com/HeitorAugustoLN/cosmic-ctl/refs/heads/main/schema.json".to_string(),
operations: all_operations,
};

let json_data = serde_json::to_string_pretty(&backup_data)?;
fs::write(&self.file, json_data)?;

println!(
"Backup completed successfully. {} entries backed up.",
entry_count
"Backup completed successfully. {} total entries backed up.",
total_entry_count
);
Ok(())
}
Expand Down
5 changes: 4 additions & 1 deletion src/commands/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ pub struct DeleteCommand {
/// The specific configuration entry to modify (e.g., 'autotile').
#[arg(short, long)]
entry: String,
/// The XDG directory to use (e.g., 'config', 'cache', 'data').
#[arg(short, long, default_value = "config")]
xdg_dir: String,
}

impl Command for DeleteCommand {
type Err = Error;

fn execute(&self) -> Result<(), Self::Err> {
match delete_configuration(&self.component, &self.version, &self.entry) {
match delete_configuration(&self.component, &self.version, &self.entry, &self.xdg_dir) {
Ok(()) => {
println!("Configuration entry deleted successfully.");
Ok(())
Expand Down
5 changes: 4 additions & 1 deletion src/commands/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ pub struct ReadCommand {
/// The specific configuration entry to modify (e.g., 'autotile').
#[arg(short, long)]
entry: String,
/// The XDG directory to use (e.g., 'config', 'cache', 'data').
#[arg(short, long, default_value = "config")]
xdg_dir: String,
}

impl Command for ReadCommand {
type Err = Error;

fn execute(&self) -> Result<(), Self::Err> {
match read_configuration(&self.component, &self.version, &self.entry) {
match read_configuration(&self.component, &self.version, &self.entry, &self.xdg_dir) {
Ok(value) => {
println!("{}", value);
Ok(())
Expand Down
Loading

0 comments on commit 4297c69

Please sign in to comment.