forked from Spotifyd/spotifyd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.rs
202 lines (191 loc) · 7.26 KB
/
process.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
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
use crate::error::Error;
use librespot_playback::player::PlayerEvent;
use log::info;
use std::{collections::HashMap, process::Stdio};
use tokio::{
io::{self, AsyncWriteExt},
process::{self, Command},
};
/// Blocks while provided command is run in a subprocess using the provided
/// shell. If successful, returns the contents of the subprocess's `stdout` as a
/// `String`.
pub(crate) fn run_program(shell: &str, cmd: &str) -> Result<String, Error> {
info!("Running {:?} using {:?}", cmd, shell);
let output = std::process::Command::new(shell)
.arg("-c")
.arg(cmd)
.output()
.map_err(|e| Error::subprocess_with_err(shell, cmd, e))?;
if !output.status.success() {
let s = std::str::from_utf8(&output.stderr).map_err(|_| Error::subprocess(shell, cmd))?;
return Err(Error::subprocess_with_str(shell, cmd, s));
}
let s = String::from_utf8(output.stdout).map_err(|_| Error::subprocess(shell, cmd))?;
Ok(s)
}
/// Spawns provided command in a subprocess using the provided shell.
fn spawn_program(shell: &str, cmd: &str, env: HashMap<&str, String>) -> Result<Child, Error> {
info!(
"Running {:?} using {:?} with environment variables {:?}",
cmd, shell, env
);
let inner = Command::new(shell)
.arg("-c")
.arg(cmd)
.envs(env.iter())
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.map_err(|e| Error::subprocess_with_err(shell, cmd, e))?;
let child = Child::new(cmd.to_string(), inner, shell.to_string());
Ok(child)
}
/// Spawns provided command in a subprocess using the provided shell.
/// Various environment variables are included in the subprocess's environment
/// depending on the `PlayerEvent` that was passed in.
pub(crate) fn spawn_program_on_event(
shell: &str,
cmd: &str,
event: PlayerEvent,
) -> Result<Child, Error> {
let mut env = HashMap::new();
match event {
PlayerEvent::Changed {
old_track_id,
new_track_id,
} => {
env.insert("OLD_TRACK_ID", old_track_id.to_base62().unwrap());
env.insert("PLAYER_EVENT", "change".to_string());
env.insert("TRACK_ID", new_track_id.to_base62().unwrap());
}
PlayerEvent::Started {
track_id,
play_request_id,
position_ms,
} => {
env.insert("PLAYER_EVENT", "start".to_string());
env.insert("TRACK_ID", track_id.to_base62().unwrap());
env.insert("PLAY_REQUEST_ID", play_request_id.to_string());
env.insert("POSITION_MS", position_ms.to_string());
}
PlayerEvent::Stopped {
track_id,
play_request_id,
} => {
env.insert("PLAYER_EVENT", "stop".to_string());
env.insert("TRACK_ID", track_id.to_base62().unwrap());
env.insert("PLAY_REQUEST_ID", play_request_id.to_string());
}
PlayerEvent::Loading {
track_id,
play_request_id,
position_ms,
} => {
env.insert("PLAYER_EVENT", "load".to_string());
env.insert("TRACK_ID", track_id.to_base62().unwrap());
env.insert("PLAY_REQUEST_ID", play_request_id.to_string());
env.insert("POSITION_MS", position_ms.to_string());
}
PlayerEvent::Playing {
track_id,
play_request_id,
position_ms,
duration_ms,
} => {
env.insert("PLAYER_EVENT", "play".to_string());
env.insert("TRACK_ID", track_id.to_base62().unwrap());
env.insert("PLAY_REQUEST_ID", play_request_id.to_string());
env.insert("POSITION_MS", position_ms.to_string());
env.insert("DURATION_MS", duration_ms.to_string());
}
PlayerEvent::Paused {
track_id,
play_request_id,
position_ms,
duration_ms,
} => {
env.insert("PLAYER_EVENT", "pause".to_string());
env.insert("TRACK_ID", track_id.to_base62().unwrap());
env.insert("PLAY_REQUEST_ID", play_request_id.to_string());
env.insert("POSITION_MS", position_ms.to_string());
env.insert("DURATION_MS", duration_ms.to_string());
}
PlayerEvent::TimeToPreloadNextTrack {
track_id,
play_request_id,
} => {
env.insert("PLAYER_EVENT", "preload".to_string());
env.insert("TRACK_ID", track_id.to_base62().unwrap());
env.insert("PLAY_REQUEST_ID", play_request_id.to_string());
}
PlayerEvent::EndOfTrack {
track_id,
play_request_id,
} => {
env.insert("PLAYER_EVENT", "endoftrack".to_string());
env.insert("TRACK_ID", track_id.to_base62().unwrap());
env.insert("PLAY_REQUEST_ID", play_request_id.to_string());
}
PlayerEvent::VolumeSet { volume } => {
env.insert("PLAYER_EVENT", "volumeset".to_string());
env.insert("VOLUME", volume.to_string());
}
PlayerEvent::Unavailable {
play_request_id,
track_id,
} => {
env.insert("PLAYER_EVENT", "unavailable".to_string());
env.insert("TRACK_ID", track_id.to_base62().unwrap());
env.insert("PLAY_REQUEST_ID", play_request_id.to_string());
}
PlayerEvent::Preloading { track_id } => {
env.insert("PLAYER_EVENT", "preloading".to_string());
env.insert("TRACK_ID", track_id.to_base62().unwrap());
}
}
spawn_program(shell, cmd, env)
}
/// Wraps `tokio::process::Child` so that when this `Child` exits:
/// * successfully: It writes the contents of it's stdout to the stdout of the
/// main process.
/// * unsuccesfully: It returns an error that includes the contents it's stderr
/// as well as information on the command that was run and the shell that
/// invoked it.
pub(crate) struct Child {
cmd: String,
child: process::Child,
shell: String,
}
impl Child {
pub(crate) fn new(cmd: String, child: process::Child, shell: String) -> Self {
Self { cmd, child, shell }
}
pub(crate) async fn wait(self) -> Result<(), Error> {
let Child { cmd, shell, child } = self;
let output = child
.wait_with_output()
.await
.map_err(|e| Error::subprocess_with_err(&shell, &cmd, e))?;
if output.status.success() {
// If successful, write subprocess's stdout to main process's stdout...
let mut stdout = io::stdout();
stdout
.write_all(&output.stdout)
.await
.map_err(|e| Error::subprocess_with_err(&shell, &cmd, e))?;
stdout
.flush()
.await
.map_err(|e| Error::subprocess_with_err(&shell, &cmd, e))?;
Ok(())
} else {
// If unsuccessful, return an error that includes the contents of stderr...
let err = match String::from_utf8(output.stderr) {
Ok(stderr) => Error::subprocess_with_str(&shell, &cmd, &stderr),
Err(_) => Error::subprocess(&shell, &cmd),
};
Err(err)
}
}
}