Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/rust/src/shell.rs
2885 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
use crate::{Logger, WINDOWS};
19
use anyhow::Error;
20
21
pub const CRLF: &str = "\r\n";
22
pub const LF: &str = "\n";
23
24
pub struct Command {
25
pub single_arg: Option<String>,
26
pub multiple_args: Option<Vec<&'static str>>,
27
}
28
29
impl Command {
30
pub fn new_single(single_arg: String) -> Self {
31
Command {
32
single_arg: Some(single_arg),
33
multiple_args: None,
34
}
35
}
36
37
pub fn new_multiple(multiple_args: Vec<&'static str>) -> Self {
38
Command {
39
single_arg: None,
40
multiple_args: Some(multiple_args),
41
}
42
}
43
44
pub fn is_single(&self) -> bool {
45
self.single_arg.is_some()
46
}
47
48
pub fn is_multiple(&self) -> bool {
49
self.multiple_args.is_some()
50
}
51
52
pub fn display(&self) -> String {
53
if self.is_single() {
54
self.single_arg.clone().unwrap()
55
} else {
56
self.multiple_args
57
.clone()
58
.unwrap()
59
.into_iter()
60
.map(|c| c.to_string())
61
.collect::<Vec<String>>()
62
.join(" ")
63
}
64
}
65
}
66
67
pub fn run_shell_command_with_log(
68
log: &Logger,
69
os: &str,
70
command: Command,
71
) -> Result<String, Error> {
72
log.debug(format!("Running command: {}", command.display()));
73
let output = run_shell_command_by_os(os, command)?;
74
log.debug(format!("Output: {:?}", output));
75
Ok(output)
76
}
77
78
pub fn run_shell_command_by_os(os: &str, command: Command) -> Result<String, Error> {
79
let (shell, flag) = if WINDOWS.is(os) {
80
("cmd", "/c")
81
} else {
82
("sh", "-c")
83
};
84
run_shell_command(shell, flag, command)
85
}
86
87
pub fn run_shell_command(shell: &str, flag: &str, command: Command) -> Result<String, Error> {
88
let mut process = std::process::Command::new(shell);
89
process.arg(flag);
90
91
if command.is_single() {
92
process.arg(command.single_arg.unwrap());
93
} else {
94
for arg in command.multiple_args.unwrap().iter() {
95
process.arg(arg);
96
}
97
}
98
let output = process.output()?;
99
Ok(strip_trailing_newline(&String::from_utf8_lossy(&output.stdout)).to_string())
100
}
101
102
pub fn strip_trailing_newline(input: &str) -> &str {
103
input
104
.strip_suffix(CRLF)
105
.or_else(|| input.strip_suffix(LF))
106
.unwrap_or(input)
107
}
108
109
pub fn split_lines(string: &str) -> Vec<&str> {
110
if string.contains(CRLF) {
111
string.split(CRLF).collect()
112
} else {
113
string.split(LF).collect()
114
}
115
}
116
117