Skip to content

Commit

Permalink
✨ feature: 0.8.0: --no-config, --rule (closes #70)
Browse files Browse the repository at this point in the history
  • Loading branch information
queer committed Jul 10, 2023
1 parent 9ffb0d8 commit fa9f318
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "boxxy"
version = "0.7.2"
version = "0.8.0"
edition = "2021"
repository = "https://github.com/queer/boxxy"

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ force it to puts its data somewhere nice and proper.
- `0.6.1`: boxxy rules can inject env vars: ![image of 0.6.1 features](https://cdn.mewna.xyz/2023/03/29/ukcWuiYdtI8yq.png)
- `0.7.2`: boxxy can fork the boxxed process into the background with the
`--daemon` flag.
- `0.8.0`: boxxy can pass rules at the command line with `--rule`, and disable
loading config files with `--no-config`.

### potential drawbacks

Expand Down
56 changes: 46 additions & 10 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::process::Command;

use color_eyre::Result;
use log::*;

use crate::enclosure::rule::BoxxyRules;
use crate::enclosure::rule::{BoxxyRules, Rule};

pub struct BoxxyConfig {
pub rules: BoxxyRules,
Expand Down Expand Up @@ -101,6 +102,39 @@ impl BoxxyConfig {
Ok(rules)
}

pub fn load_rules_from_cli_flag(rules: &[String]) -> Result<BoxxyRules> {
let rules = rules
.iter()
.map(|s| {
let parts: Vec<&str> = s.split(':').collect();
match parts.as_slice() {
[src, dest] => Rule {
name: format!("cli-loaded rule: {src} -> {dest}"),
target: src.to_string(),
rewrite: dest.to_string(),
mode: crate::enclosure::rule::RuleMode::File,
context: vec![],
only: vec![],
env: HashMap::new(),
},

[src, dest, mode] => Rule {
name: format!("cli-loaded rule: {src} -> {dest} ({mode})"),
target: src.to_string(),
rewrite: dest.to_string(),
mode: mode.parse().unwrap(),
context: vec![],
only: vec![],
env: HashMap::new(),
},

_ => panic!("invalid format for cli rule: {s}"),
}
})
.collect();
Ok(BoxxyRules { rules })
}

pub fn merge(configs: Vec<BoxxyRules>) -> BoxxyRules {
let mut merged = BoxxyRules { rules: vec![] };
for config in configs {
Expand All @@ -111,18 +145,20 @@ impl BoxxyConfig {
}

pub fn load_config(args: crate::Args) -> Result<Self> {
// Load rules
let rules = {
let paths = Self::rule_paths()?;
let mut configs = vec![];
for path in paths {
debug!("loading rules from {}", path.display());
let config = Self::load_rules_from_path(&path)?;
debug!("loaded {} rules", config.rules.len());
configs.push(config);
let mut rules = vec![];
if !args.no_config {
debug!("loading rules (not asked not to!)");
for config in BoxxyConfig::rule_paths()? {
info!("loading rules from {}", config.display());
rules.push(BoxxyConfig::load_rules_from_path(&config)?);
}
}

Self::merge(configs)
rules.push(BoxxyConfig::load_rules_from_cli_flag(&args.arg_rules)?);
BoxxyConfig::merge(rules)
};
info!("loaded {} total rule(s)", rules.rules.len());

let (cmd, cmd_args) = (&args.command_with_args[0], &args.command_with_args[1..]);

Expand Down
13 changes: 13 additions & 0 deletions src/enclosure/rule.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::str::FromStr;

use color_eyre::Result;
use log::*;
Expand Down Expand Up @@ -198,3 +199,15 @@ pub enum RuleMode {
File,
Directory,
}

impl FromStr for RuleMode {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"file" => Ok(RuleMode::File),
"directory" => Ok(RuleMode::Directory),
_ => Err(format!("invalid rule mode: {}", s)),
}
}
}
29 changes: 17 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;
use std::path::PathBuf;

use atty::Stream;
use clap::{Parser, Subcommand};
use clap::{ArgAction, Parser, Subcommand};
use color_eyre::Result;
use log::*;
use scanner::App;
Expand Down Expand Up @@ -76,6 +76,22 @@ pub struct Args {
)]
pub daemon: bool,

#[arg(
long = "no-config",
default_value = "false",
help = "Disable loading config files entirely.",
action = ArgAction::SetTrue
)]
pub no_config: bool,

#[arg(
short = 'r',
long = "rule",
help = "Pass rules via CLI. -r/--rule `/remount/this:/to/this:<file/dir>`",
action = ArgAction::Append
)]
pub arg_rules: Vec<String>,

#[command(subcommand)]
pub command: Option<BoxxySubcommand>,
}
Expand Down Expand Up @@ -119,17 +135,6 @@ fn main() -> Result<()> {
}
}

// Load rules
let rules = {
let mut rules = vec![];
for config in BoxxyConfig::rule_paths()? {
info!("loading rules from {}", config.display());
rules.push(BoxxyConfig::load_rules_from_path(&config)?);
}
BoxxyConfig::merge(rules)
};
info!("loaded {} total rule(s)", rules.rules.len());

// Do the thing!
enclosure::Enclosure::new(BoxxyConfig::load_config(cfg)?).run()?;

Expand Down

0 comments on commit fa9f318

Please sign in to comment.