-
-
Notifications
You must be signed in to change notification settings - Fork 633
/
Copy pathpowershell.rs
169 lines (146 loc) · 4.63 KB
/
powershell.rs
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
use crate::shell::{Alias, Var};
use crate::store::{AliasStore, var::VarStore};
use std::path::PathBuf;
async fn cached_aliases(path: PathBuf, store: &AliasStore) -> String {
match tokio::fs::read_to_string(path).await {
Ok(aliases) => aliases,
Err(r) => {
// we failed to read the file for some reason, but the file does exist
// fallback to generating new aliases on the fly
store.powershell().await.unwrap_or_else(|e| {
format!("echo 'Atuin: failed to read and generate aliases: \n{r}\n{e}'",)
})
}
}
}
async fn cached_vars(path: PathBuf, store: &VarStore) -> String {
match tokio::fs::read_to_string(path).await {
Ok(vars) => vars,
Err(r) => {
// we failed to read the file for some reason, but the file does exist
// fallback to generating new vars on the fly
store.powershell().await.unwrap_or_else(|e| {
format!("echo 'Atuin: failed to read and generate vars: \n{r}\n{e}'",)
})
}
}
}
/// Return powershell dotfile config
///
/// Do not return an error. We should not prevent the shell from starting.
///
/// In the worst case, Atuin should not function but the shell should start correctly.
///
/// While currently this only returns aliases, it will be extended to also return other synced dotfiles
pub async fn alias_config(store: &AliasStore) -> String {
// First try to read the cached config
let aliases = atuin_common::utils::dotfiles_cache_dir().join("aliases.ps1");
if aliases.exists() {
return cached_aliases(aliases, store).await;
}
if let Err(e) = store.build().await {
return format!("echo 'Atuin: failed to generate aliases: {}'", e);
}
cached_aliases(aliases, store).await
}
pub async fn var_config(store: &VarStore) -> String {
// First try to read the cached config
let vars = atuin_common::utils::dotfiles_cache_dir().join("vars.ps1");
if vars.exists() {
return cached_vars(vars, store).await;
}
if let Err(e) = store.build().await {
return format!("echo 'Atuin: failed to generate vars: {}'", e);
}
cached_vars(vars, store).await
}
pub fn format_alias(alias: &Alias) -> String {
// Set-Alias doesn't support adding implicit arguments, so use a function.
// See https://github.com/PowerShell/PowerShell/issues/12962
let mut result = secure_command(&format!(
"function {} {{\n {}{} @args\n}}",
alias.name,
if alias.value.starts_with(['"', '\'']) {
"& "
} else {
""
},
alias.value
));
// This makes the file layout prettier
result.insert(0, '\n');
result
}
pub fn format_var(var: &Var) -> String {
secure_command(&format!(
"${}{} = '{}'",
if var.export { "env:" } else { "" },
var.name,
var.value.replace("'", "''")
))
}
/// Wraps the given command in an Invoke-Expression to ensure the outer script is not halted
/// if the inner command contains a syntax error.
fn secure_command(command: &str) -> String {
format!(
"Invoke-Expression -ErrorAction Continue -Command '{}'\n",
command.replace("'", "''")
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn aliases() {
assert_eq!(
format_alias(&Alias {
name: "gp".to_string(),
value: "git push".to_string(),
}),
"\n".to_string()
+ &secure_command(
"function gp {
git push @args
}"
)
);
assert_eq!(
format_alias(&Alias {
name: "spc".to_string(),
value: "\"path with spaces\" arg".to_string(),
}),
"\n".to_string()
+ &secure_command(
"function spc {
& \"path with spaces\" arg @args
}"
)
);
}
#[test]
fn vars() {
assert_eq!(
format_var(&Var {
name: "FOO".to_owned(),
value: "bar 'baz'".to_owned(),
export: true,
}),
secure_command("$env:FOO = 'bar ''baz'''")
);
assert_eq!(
format_var(&Var {
name: "TEST".to_owned(),
value: "1".to_owned(),
export: false,
}),
secure_command("$TEST = '1'")
);
}
#[test]
fn invoke_expression() {
assert_eq!(
secure_command("echo 'foo'"),
"Invoke-Expression -ErrorAction Continue -Command 'echo ''foo'''\n"
)
}
}