Browse and listen to thousands of radio stations across the globe right from your terminal ๐ ๐ป ๐ตโจ
radio
rust
tokio
web-radio
command-line-tool
tui
1use std::{path::Path, process::Command};
2
3use anyhow::Error;
4
5const SERVICE_TEMPLATE: &str = include_str!("./systemd/tunein.service");
6
7pub fn install() -> Result<(), Error> {
8 if cfg!(not(target_os = "linux")) {
9 println!("This command is only supported on Linux");
10 std::process::exit(1);
11 }
12
13 let home = std::env::var("HOME")?;
14 let service_path: &str = &format!("{}/.config/systemd/user/tunein.service", home);
15 std::fs::create_dir_all(format!("{}/.config/systemd/user", home))
16 .expect("Failed to create systemd user directory");
17
18 if Path::new(service_path).exists() {
19 println!("Service file already exists. Nothing to install.");
20 return Ok(());
21 }
22
23 let tunein_path = std::env::current_exe()?;
24
25 let service_template: &str = &SERVICE_TEMPLATE.replace(
26 "ExecStart=/usr/bin/tunein",
27 &format!("ExecStart={}", tunein_path.display()),
28 );
29
30 std::fs::write(service_path, service_template).expect("Failed to write service file");
31
32 Command::new("systemctl")
33 .arg("--user")
34 .arg("daemon-reload")
35 .status()?;
36
37 Command::new("systemctl")
38 .arg("--user")
39 .arg("enable")
40 .arg("tunein")
41 .status()?;
42
43 Command::new("systemctl")
44 .arg("--user")
45 .arg("start")
46 .arg("tunein")
47 .status()?;
48
49 println!("โ
Tunein service installed successfully!");
50
51 Ok(())
52}
53
54pub fn uninstall() -> Result<(), Error> {
55 if cfg!(not(target_os = "linux")) {
56 println!("This command is only supported on Linux");
57 std::process::exit(1);
58 }
59
60 let home = std::env::var("HOME")?;
61 let service_path: &str = &format!("{}/.config/systemd/user/tunein.service", home);
62
63 if Path::new(service_path).exists() {
64 Command::new("systemctl")
65 .arg("--user")
66 .arg("stop")
67 .arg("tunein")
68 .status()?;
69
70 Command::new("systemctl")
71 .arg("--user")
72 .arg("disable")
73 .arg("tunein")
74 .status()?;
75
76 std::fs::remove_file(service_path).expect("Failed to remove service file");
77
78 Command::new("systemctl")
79 .arg("--user")
80 .arg("daemon-reload")
81 .status()?;
82
83 println!("โ
Tunein service uninstalled successfully!");
84 } else {
85 println!("Service file does not exist. Nothing to uninstall.");
86 }
87
88 Ok(())
89}
90
91pub fn status() -> Result<(), Error> {
92 if cfg!(not(target_os = "linux")) {
93 println!("This command is only supported on Linux");
94 std::process::exit(1);
95 }
96
97 let home = std::env::var("HOME")?;
98 let service_path: &str = &format!("{}/.config/systemd/user/tunein.service", home);
99
100 if Path::new(service_path).exists() {
101 Command::new("systemctl")
102 .arg("--user")
103 .arg("status")
104 .arg("tunein")
105 .status()?;
106 } else {
107 println!("Service file does not exist. Tunein service is not installed.");
108 }
109
110 Ok(())
111}