forked from denoland/deno_task_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
164 lines (151 loc) · 3.56 KB
/
mod.rs
File metadata and controls
164 lines (151 loc) · 3.56 KB
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
// Copyright 2018-2025 the Deno authors. MIT license.
mod args;
mod cat;
mod cd;
mod cp_mv;
mod echo;
mod executable;
mod exit;
mod export;
mod head;
mod mkdir;
mod pwd;
mod rm;
mod set;
mod shopt;
mod sleep;
mod unset;
mod xargs;
use std::collections::HashMap;
use std::ffi::OsString;
use std::rc::Rc;
use futures::future::LocalBoxFuture;
pub use executable::ExecutableCommand;
use super::types::ExecuteResult;
use super::types::FutureExecuteResult;
use super::types::ShellPipeReader;
use super::types::ShellPipeWriter;
use super::types::ShellState;
pub fn builtin_commands() -> HashMap<String, Rc<dyn ShellCommand>> {
HashMap::from([
(
"cat".to_string(),
Rc::new(cat::CatCommand) as Rc<dyn ShellCommand>,
),
(
"cd".to_string(),
Rc::new(cd::CdCommand) as Rc<dyn ShellCommand>,
),
(
"cp".to_string(),
Rc::new(cp_mv::CpCommand) as Rc<dyn ShellCommand>,
),
(
"echo".to_string(),
Rc::new(echo::EchoCommand) as Rc<dyn ShellCommand>,
),
(
"exit".to_string(),
Rc::new(exit::ExitCommand) as Rc<dyn ShellCommand>,
),
(
"export".to_string(),
Rc::new(export::ExportCommand) as Rc<dyn ShellCommand>,
),
(
"head".to_string(),
Rc::new(head::HeadCommand) as Rc<dyn ShellCommand>,
),
(
"mkdir".to_string(),
Rc::new(mkdir::MkdirCommand) as Rc<dyn ShellCommand>,
),
(
"mv".to_string(),
Rc::new(cp_mv::MvCommand) as Rc<dyn ShellCommand>,
),
(
"pwd".to_string(),
Rc::new(pwd::PwdCommand) as Rc<dyn ShellCommand>,
),
(
"rm".to_string(),
Rc::new(rm::RmCommand) as Rc<dyn ShellCommand>,
),
(
"set".to_string(),
Rc::new(set::SetCommand) as Rc<dyn ShellCommand>,
),
(
"shopt".to_string(),
Rc::new(shopt::ShoptCommand) as Rc<dyn ShellCommand>,
),
(
"sleep".to_string(),
Rc::new(sleep::SleepCommand) as Rc<dyn ShellCommand>,
),
(
"true".to_string(),
Rc::new(ExitCodeCommand(0)) as Rc<dyn ShellCommand>,
),
(
"false".to_string(),
Rc::new(ExitCodeCommand(1)) as Rc<dyn ShellCommand>,
),
(
"unset".to_string(),
Rc::new(unset::UnsetCommand) as Rc<dyn ShellCommand>,
),
(
"xargs".to_string(),
Rc::new(xargs::XargsCommand) as Rc<dyn ShellCommand>,
),
])
}
pub struct ExecuteCommandArgsContext {
pub args: Vec<OsString>,
pub state: ShellState,
pub stdin: ShellPipeReader,
pub stdout: ShellPipeWriter,
pub stderr: ShellPipeWriter,
}
pub struct ShellCommandContext {
pub args: Vec<OsString>,
pub state: ShellState,
pub stdin: ShellPipeReader,
pub stdout: ShellPipeWriter,
pub stderr: ShellPipeWriter,
pub execute_command_args:
Box<dyn FnOnce(ExecuteCommandArgsContext) -> FutureExecuteResult>,
}
pub trait ShellCommand {
fn execute(
&self,
context: ShellCommandContext,
) -> LocalBoxFuture<'static, ExecuteResult>;
}
macro_rules! execute_with_cancellation {
($result_expr:expr, $kill_signal:expr) => {
tokio::select! {
result = $result_expr => {
result
},
signal = $kill_signal.wait_aborted() => {
ExecuteResult::from_exit_code(signal.aborted_code())
}
}
};
}
pub(super) use execute_with_cancellation;
struct ExitCodeCommand(i32);
impl ShellCommand for ExitCodeCommand {
fn execute(
&self,
_context: ShellCommandContext,
) -> LocalBoxFuture<'static, ExecuteResult> {
// ignores additional arguments
Box::pin(futures::future::ready(ExecuteResult::from_exit_code(
self.0,
)))
}
}