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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
use AtomicBool;
use Arc;
use anyhow;
use Result;
use sleep;
use Duration;
use crateget_current_version;
use cratePackage;
use cratePackageType;
/// Handles the proxy command with optional arguments.
///
/// This function processes the provided arguments and executes the appropriate
/// action based on the input. If the first argument is `--hyper-jump`, it
/// prints the version information of itself. Otherwise, it constructs a new
/// `Package` to processes it.
///
/// # Arguments
///
/// * `rest_args` - A slice of strings containing the command-line arguments.
///
/// # Returns
///
/// This function returns a `Result` indicating the success or failure of the
/// operation.
///
/// * `Ok(())` - The operation was successful.
/// * `Err(miette::Error)` - An error occurred during the operation.
///
/// # Examples
///
/// ```rust
/// let args = vec!["some-other-arg".to_string()];
/// handle_proxy(&args).await?;
/// ```
///
/// # Errors
///
/// This function will return an error if the `handle_package_process` function
/// fails.
pub async
/// Handles the execution process.
///
/// It retrieves the downloads directory and the currently used version from the
/// configuration. It then constructs the path to the binary and spawns a new
/// process with the given arguments. The function then enters a loop where it
/// continuously checks the status of the spawned process. If the process exits
/// with a status code of `0`, the function returns `Ok(())`. If the process
/// exits with a non-zero status code, the function returns an error with the
/// status code as the error message. If the process is terminated by a signal,
/// the function returns an error with the message "Process terminated by
/// signal". If the function fails to wait on the child process, it returns an
/// error with the message "Failed to wait on child process".
///
/// # Arguments
///
/// * `args` - A slice of `String` arguments to be passed to the process.
///
/// # Returns
///
/// This function returns a `Result` that indicates whether the operation was
/// successful. If the operation was successful, the function returns `Ok(())`.
/// If the operation failed, the function returns `Err` with a description of
/// the error.
///
/// # Errors
///
/// This function will return an error if:
///
/// * The process exits with a non-zero status code.
/// * The process is terminated by a signal.
/// * The function fails to wait on the child process.
///
/// # Example
///
/// ```rust
/// let args = vec!["-v".to_string()];
/// handle_package_process(&args).await;
/// ```
pub async
/// Watches a spawned child process and handles termination signals.
///
/// This function concurrently waits for the spawned child process to exit or
/// for a Ctrl-C signal to be received. It handles each scenario appropriately.
///
/// # Arguments
///
/// * `spawned_child` - A mutable reference to the spawned child process.
/// * `term_signal` - An `Arc` containing an `AtomicBool` used to signal
/// termination.
///
/// # Returns
///
/// This function returns a `Result` indicating the success or failure of the
/// operation.
///
/// * `Ok(())` - The operation was successful.
/// * `Err(anyhow::Error)` - An error occurred during the operation.
///
/// # Errors
///
/// This function will return an error if either `handle_process_exit` or
/// `handle_ctrl_c` encounters an error.
///
/// # Examples
///
/// ```rust
/// let term_signal = Arc::new(AtomicBool::new(false));
/// let mut child = tokio::process::Command::new("some_command").spawn()?;
/// watch_process(&mut child, &term_signal).await?.
/// ```
async
/// Handles the exit of a spawned child process.
///
/// This function processes the exit status of the child process and returns an
/// appropriate result based on the exit code.
///
/// # Arguments
///
/// * `status` - The exit status of the child process.
///
/// # Returns
///
/// This function returns a `Result` indicating the success or failure of the
/// operation.
///
/// * `Ok(())` - The process exited successfully.
/// * `Err(anyhow::Error)` - The process exited with an error code or was
/// terminated by a signal.
///
/// # Errors
///
/// This function will return an error if the process exited with a non-zero
/// exit code or was terminated by a signal.
///
/// # Examples
///
/// ```rust
/// let status = Ok(std::process::ExitStatus::from_raw(0));
/// handle_process_exit(status).await?;
/// ```
async
/// Handles the Ctrl-C signal.
///
/// This function sets the termination signal and handles Unix-specific signals
/// if applicable.
///
/// # Arguments
///
/// * `spawned_child` - A mutable reference to the spawned child process.
/// * `term_signal` - An `Arc` containing an `AtomicBool` used to signal
/// termination.
///
/// # Returns
///
/// This function returns a `Result` indicating the success or failure of the
/// operation.
///
/// * `Ok(())` - The operation was successful.
/// * `Err(anyhow::Error)` - An error occurred during the operation.
///
/// # Errors
///
/// This function will return an error if `handle_unix_signals` encounters an
/// error.
///
/// # Examples
///
/// ```rust
/// let term_signal = Arc::new(AtomicBool::new(false));
/// let mut child = tokio::process::Command::new("some_command").spawn()?;
/// handle_ctrl_c(&mut child, &term_signal).await?;
/// ```
async
/// Handles Unix-specific termination signals.
///
/// This function sends a Unix signal to the spawned child process if the
/// termination signal is set.
///
/// # Arguments
///
/// * `spawned_child` - A mutable reference to the spawned child process.
/// * `term_signal` - An `Arc` containing an `AtomicBool` used to signal
/// termination.
///
/// # Returns
///
/// This function returns a `Result` indicating the success or failure of the
/// operation.
///
/// * `Ok(())` - The operation was successful.
/// * `Err(anyhow::Error)` - An error occurred during the operation.
///
/// # Errors
///
/// This function will return an error if it fails to send the Unix signal.
///
/// # Examples
///
/// ```rust
/// let term_signal = Arc::new(AtomicBool::new(true));
/// let mut child = tokio::process::Command::new("some_command").spawn()?;
/// handle_unix_signals(&mut child, &term_signal)?;
/// ```