Skip to content

Commit 0de4e39

Browse files
authoredFeb 15, 2020
Merge pull request #4 from kesslern/shell-commands
Add arbitrary shell commands (Close #3)
2 parents 13a9b53 + d3a8d88 commit 0de4e39

File tree

5 files changed

+47
-10
lines changed

5 files changed

+47
-10
lines changed
 

‎README.md

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Storing dotfiles in git repositories allows them to be shared across multiple co
66

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

43+
#### Arbitrary Shell Commands
44+
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`.
45+
```
46+
SHELL_COMMAND=SHELL echo 1234
47+
```
48+
4249
#### Feature Flags
4350
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.
4451
```

‎src/lib.rs

+34-10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::io::Lines;
1313
use std::io::Read;
1414
use std::io::Write;
1515
use std::path::Path;
16+
use std::process::Command;
1617
use walkdir::WalkDir;
1718

1819
pub struct Config {
@@ -30,7 +31,7 @@ impl Config {
3031
for line in lines {
3132
let line = line?;
3233

33-
match Config::parse_line(line) {
34+
match Config::parse_line(line)? {
3435
Some(value) => match value {
3536
ConfigValue::Feature(it) => {
3637
config.features.push(it);
@@ -46,28 +47,51 @@ impl Config {
4647
Ok(config)
4748
}
4849

49-
fn parse_line(line: String) -> Option<ConfigValue> {
50+
fn run_command(command: String) -> Result<String, Box<dyn Error>> {
51+
let stdout = Command::new("sh")
52+
.arg("-c")
53+
.arg(&command)
54+
.output()?
55+
.stdout;
56+
57+
return Ok(String::from_utf8(stdout)?.trim().to_owned());
58+
}
59+
60+
61+
fn parse_line(line: String) -> Result<Option<ConfigValue>, Box<dyn Error>> {
5062
let mut line = line.trim().to_string();
5163

5264
if line.is_empty() || line.starts_with("#") {
53-
return None;
65+
return Ok(None);
5466
}
5567

5668
match line.chars().position(|c| c == '=') {
5769
Some(idx) => {
58-
let mut var = line.split_off(idx);
59-
var.remove(0);
60-
return Some(ConfigValue::Substitution {
61-
key: line,
62-
value: var,
63-
});
70+
let mut value = line.split_off(idx);
71+
value.remove(0);
72+
73+
if value.starts_with("SHELL ") {
74+
let command = value.split_off(6);
75+
let result = &Config::run_command(command)?;
76+
77+
return Ok(Some(ConfigValue::Substitution {
78+
key: line,
79+
value: result.to_owned(),
80+
}));
81+
} else {
82+
return Ok(Some(ConfigValue::Substitution {
83+
key: line,
84+
value: value,
85+
}));
86+
}
6487
}
6588
None => {
66-
return Some(ConfigValue::Feature(line));
89+
return Ok(Some(ConfigValue::Feature(line)));
6790
}
6891
}
6992
}
7093

94+
7195
fn template_file(&self, source: &Path, dest: &Path) -> Result<(), Box<dyn Error>> {
7296
let source = BufReader::new(File::open(source)?);
7397
let mut dest = File::create(dest)?;

‎test/dotfiles/template

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Are BASICs really better than dogs?
44

55
Weird equals stuff can happen: 4 EQUALS
66

7+
Test a shell command: SHELL_COMMAND
8+
79
### FEATURE1
810
this should appear
911
### FEATURE1

‎test/expected/template

+2
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ Are cats really better than dogs?
44

55
Weird equals stuff can happen: ====
66

7+
Test a shell command: 1234
8+
79
this should appear
810
this should appear (test indentation)

‎test/rules

+2
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ EMPTY VARIABLE=
1111

1212
FEATURE1
1313
# More comments...
14+
15+
SHELL_COMMAND=SHELL echo 1234

0 commit comments

Comments
 (0)
Please sign in to comment.