Skip to content

Commit 6e62c8a

Browse files
committed
fix: bug when trying to edit or view logs of user units
1 parent 6db9a5c commit 6e62c8a

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

src/infrastructure/systemd_service_adapter.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ pub enum ConnectionType {
3030
}
3131

3232
pub struct SystemdServiceAdapter {
33-
connection: Connection
33+
connection: Connection,
34+
connection_type: ConnectionType
3435
}
3536

3637
impl SystemdServiceAdapter {
@@ -41,11 +42,9 @@ impl SystemdServiceAdapter {
4142
ConnectionType::System => Connection::system()?
4243
};
4344

44-
Ok(Self {connection})
45+
Ok(Self {connection, connection_type})
4546
}
4647

47-
48-
4948
fn manager_proxy(&self) -> Result<Proxy<'static>, Box<dyn std::error::Error>> {
5049
let proxy = Proxy::new(
5150
&self.connection,
@@ -63,6 +62,7 @@ impl ServiceRepository for SystemdServiceAdapter {
6362
ConnectionType::Session => Connection::session()?,
6463
ConnectionType::System => Connection::system()?
6564
};
65+
self.connection_type = connection_type;
6666
Ok(())
6767
}
6868

@@ -138,10 +138,17 @@ impl ServiceRepository for SystemdServiceAdapter {
138138
}
139139

140140
fn get_service_log(&self, name: &str) -> Result<String, Box<dyn std::error::Error>> {
141-
let output = std::process::Command::new("journalctl")
142-
.arg("-e")
141+
let mut cmd = std::process::Command::new("journalctl");
142+
143+
cmd.arg("-e")
143144
.arg(format!("--unit={}", name))
144-
.arg("--no-pager")
145+
.arg("--no-pager");
146+
147+
if matches!(self.connection_type, ConnectionType::Session){
148+
cmd.arg("--user");
149+
}
150+
151+
let output = cmd
145152
.output()?;
146153

147154
let log = if output.status.success() {
@@ -154,9 +161,18 @@ impl ServiceRepository for SystemdServiceAdapter {
154161
}
155162

156163
fn systemctl_cat(&self, name: &str) -> Result<String, Box<dyn std::error::Error>> {
157-
let output = Command::new("systemctl")
164+
let mut cmd = Command::new("systemctl");
165+
166+
cmd
158167
.arg("cat")
159-
.arg("--no-pager")
168+
.arg("--no-pager");
169+
170+
if matches!(self.connection_type, ConnectionType::Session){
171+
cmd
172+
.arg("--user");
173+
}
174+
175+
let output = cmd
160176
.arg("--")
161177
.arg(name)
162178
.output()?;

0 commit comments

Comments
 (0)