Skip to content

Commit

Permalink
Merge pull request #4 from kesslern/shell-commands
Browse files Browse the repository at this point in the history
Add arbitrary shell commands (Close #3)
  • Loading branch information
kesslern authored Feb 15, 2020
2 parents 13a9b53 + d3a8d88 commit 0de4e39
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 10 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Storing dotfiles in git repositories allows them to be shared across multiple co

## Features
* Make string substitutions in files according to configured key/value pairs.
* Use output from arbitrary shell commands in templated dotfiles (e.g. for passwords with GNU Pass).
* Toggle chunks of files per feature flags.
* Binary files are copied without templating.
* File permissions are preserved.
Expand Down Expand Up @@ -39,6 +40,12 @@ asdf=aoeu
```
will replace all occurances of `{SUBSTITION ONE}` with `123`, `[font size]` with `19`, and `asdf` with `aoeu`.

#### Arbitrary Shell Commands
If the `=` separating key/value pairs is immediately proceeded by `SHELL `, dot-templater will run the provided command and use the stdout when templating dotfiles. Providing following line in a config file will substitute any occurrance of `SHELL_COMMAND` with `1234`.
```
SHELL_COMMAND=SHELL echo 1234
```

#### Feature Flags
Any line in the rules configuration file that does not include a `=` character and is not a comment will enable the feature name that matches the line. Dotfiles can designate togglable features with three octothorpes followed by the feature name.
```
Expand Down
44 changes: 34 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::io::Lines;
use std::io::Read;
use std::io::Write;
use std::path::Path;
use std::process::Command;
use walkdir::WalkDir;

pub struct Config {
Expand All @@ -30,7 +31,7 @@ impl Config {
for line in lines {
let line = line?;

match Config::parse_line(line) {
match Config::parse_line(line)? {
Some(value) => match value {
ConfigValue::Feature(it) => {
config.features.push(it);
Expand All @@ -46,28 +47,51 @@ impl Config {
Ok(config)
}

fn parse_line(line: String) -> Option<ConfigValue> {
fn run_command(command: String) -> Result<String, Box<dyn Error>> {
let stdout = Command::new("sh")
.arg("-c")
.arg(&command)
.output()?
.stdout;

return Ok(String::from_utf8(stdout)?.trim().to_owned());
}


fn parse_line(line: String) -> Result<Option<ConfigValue>, Box<dyn Error>> {
let mut line = line.trim().to_string();

if line.is_empty() || line.starts_with("#") {
return None;
return Ok(None);
}

match line.chars().position(|c| c == '=') {
Some(idx) => {
let mut var = line.split_off(idx);
var.remove(0);
return Some(ConfigValue::Substitution {
key: line,
value: var,
});
let mut value = line.split_off(idx);
value.remove(0);

if value.starts_with("SHELL ") {
let command = value.split_off(6);
let result = &Config::run_command(command)?;

return Ok(Some(ConfigValue::Substitution {
key: line,
value: result.to_owned(),
}));
} else {
return Ok(Some(ConfigValue::Substitution {
key: line,
value: value,
}));
}
}
None => {
return Some(ConfigValue::Feature(line));
return Ok(Some(ConfigValue::Feature(line)));
}
}
}


fn template_file(&self, source: &Path, dest: &Path) -> Result<(), Box<dyn Error>> {
let source = BufReader::new(File::open(source)?);
let mut dest = File::create(dest)?;
Expand Down
2 changes: 2 additions & 0 deletions test/dotfiles/template
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Are BASICs really better than dogs?

Weird equals stuff can happen: 4 EQUALS

Test a shell command: SHELL_COMMAND

### FEATURE1
this should appear
### FEATURE1
Expand Down
2 changes: 2 additions & 0 deletions test/expected/template
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ Are cats really better than dogs?

Weird equals stuff can happen: ====

Test a shell command: 1234

this should appear
this should appear (test indentation)
2 changes: 2 additions & 0 deletions test/rules
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ EMPTY VARIABLE=

FEATURE1
# More comments...

SHELL_COMMAND=SHELL echo 1234

0 comments on commit 0de4e39

Please sign in to comment.