-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathCommands.re
More file actions
141 lines (132 loc) · 3.24 KB
/
Commands.re
File metadata and controls
141 lines (132 loc) · 3.24 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
let shellEscape = path => Filename.quote(path);
let execFull = (~input=?, ~pwd=?, ~env=Unix.environment(), cmd) => {
let cmd =
if (Sys.os_type == "Win32") {
Printf.sprintf("\"%s\"", cmd)
} else {
cmd
}
let env = switch pwd {
| None => env
| Some(pwd) => Array.map(item => String.length(item) > 4 && String.sub(item, 0, 4) == "PWD=" ? "PWD=" ++ pwd : item, env)
};
let prevCwd = switch pwd {
| None => None
| Some(pwd) =>
let prevCwd = Unix.getcwd();
if (prevCwd == pwd) {
None
} else {
Unix.chdir(pwd);
Some(prevCwd)
}
}
let (cmd_out, cmd_in, cmd_err) = Unix.open_process_full(cmd, env);
switch prevCwd {
| None => ()
| Some(prevCwd) => Unix.chdir(prevCwd)
};
switch input {
| None => ()
| Some(text) => output_string(cmd_in, text)
};
close_out_noerr(cmd_in);
let cmd_out_descr = Unix.descr_of_in_channel(cmd_out);
let cmd_err_descr = Unix.descr_of_in_channel(cmd_err);
let rec loop = ((out, err, opens)) => {
if (opens == []) {
(out, err)
} else {
let (can_read, _, _) = Unix.select(opens, [], [], 1.0);
List.fold_left(
((out, err, opens), fh) =>
try (
if (fh == cmd_err_descr) {
(out, [input_line(cmd_err), ...err], opens);
} else {
([input_line(cmd_out), ...out], err, opens);
}
) {
| End_of_file => (out, err, List.filter((fh') => fh != fh', opens))
},
(out, err, opens),
can_read
) |> loop
};
};
let (out, err) = loop(([], [], [cmd_out_descr, cmd_err_descr]));
let out = List.rev(out);
let err = List.rev(err);
switch(Unix.close_process_full((cmd_out, cmd_in, cmd_err))) {
| WEXITED(0) => (out, err, true)
| WEXITED(_)
| WSIGNALED(_)
| WSTOPPED(_) => (out, err, false)
}
};
/**
* Get the output of a command, in lines.
*/
let execSync = (cmd) => {
let cin = Unix.open_process_in(cmd);
try {
let rec loop = () =>
switch (input_line(cin)) {
| exception End_of_file => []
| line => {
[line, ...loop()]
}
};
let lines = loop();
switch (Unix.close_process_in(cin)) {
| WEXITED(0) => (lines, true)
| WEXITED(_)
| WSIGNALED(_)
| WSTOPPED(_) => (lines, false)
}
} {
| End_of_file => ([], false)
}
};
let execOption = cmd => {
let (lines, success) = execSync(cmd);
if (success) {
Some(String.concat("\n", lines))
} else {
None
}
};
let execResult = cmd => {
let (lines, success) = execSync(cmd);
if (success) {
RResult.Ok(String.concat("\n", lines))
} else {
RResult.Error(String.concat("\n", lines))
}
};
/**
* Get the output of a command, in lines.
*/
let execWithInput = (cmd, input) => {
let (stdout, stdin) = Unix.open_process(cmd);
output_string(stdin, input);
close_out(stdin);
try {
let rec loop = () =>
switch (input_line(stdout)) {
| exception End_of_file => []
| line => {
[line, ...loop()]
}
};
let lines = loop();
switch (Unix.close_process((stdout, stdin))) {
| WEXITED(0) => (lines, true)
| WEXITED(_)
| WSIGNALED(_)
| WSTOPPED(_) => (lines, false)
}
} {
| End_of_file => ([], false)
}
};