-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathutils.rs
731 lines (632 loc) · 22.9 KB
/
utils.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
use adw::StyleManager;
use gettextrs::{bind_textdomain_codeset, setlocale, textdomain, LocaleCategory};
use gtk::gio::Settings;
use gtk::prelude::SettingsExt;
use std::collections::HashMap;
use std::env;
use std::path::Path;
use std::process::Command;
use crate::get_all_distroboxes;
use crate::APP_ID;
/// Used to represent any Filesystem overrides granted to the Flatpak
/// instance of `BoxBuddy`
pub struct FilesystemAccess {
/// Whether or not the user has granted `home` access
pub home: bool,
/// Whether or not the user has granted `host` access
pub host: bool,
}
/// Used to represent terminals `BoxBuddy` can spawn
pub struct TerminalOption {
/// Public-facing name of the terminal
pub name: String,
/// Command to execute to spawn the terminal
pub executable_name: String,
/// Argument provided to separate the terminal spawning from the command it should run
pub separator_arg: String,
pub flatpak_id: Option<String>,
}
/// Used to represent the resources used by a container
pub struct CpuMemUsage {
/// CPU usage
pub cpu: String,
/// Mem usage
pub mem: String,
/// Mem percentage usage
pub mem_percent: String,
}
impl FilesystemAccess {
fn new() -> Self {
FilesystemAccess {
home: false,
host: false,
}
}
}
/// Runs shell command. Uses flatpak-spawn if `BoxBuddy` is running as a Flatpak
pub fn run_command(
cmd_to_run: &str,
args_for_cmd: Option<&[&str]>,
) -> Result<std::process::Output, std::io::Error> {
let mut cmd = Command::new(cmd_to_run);
if is_flatpak() {
cmd = Command::new("flatpak-spawn");
cmd.arg("--host");
cmd.arg(cmd_to_run);
}
if let Some(a) = args_for_cmd {
cmd.args(a);
}
cmd.output()
}
/// Runs shell command and returns the output as a string
pub fn get_command_output(cmd_to_run: &str, args_for_cmd: Option<&[&str]>) -> String {
let output = run_command(cmd_to_run, args_for_cmd);
match output {
Ok(o) => {
let mut result = String::new();
if !o.stdout.is_empty() {
result = result
+ String::from_utf8_lossy(&o.stdout).into_owned().as_ref()
+ &String::from("\n");
}
if !o.stderr.is_empty() {
result = result
+ String::from_utf8_lossy(&o.stderr).into_owned().as_ref()
+ &String::from("\n");
}
result
}
Err(_) => "fail".to_string(),
}
}
/// Runs shell command and returns the output as a string, but does NOT
/// return stderr.
pub fn get_command_output_no_err(cmd_to_run: &str, args_for_cmd: Option<&[&str]>) -> String {
let output = run_command(cmd_to_run, args_for_cmd);
match output {
Ok(o) => {
let mut result = String::new();
if !o.stdout.is_empty() {
result = result
+ String::from_utf8_lossy(&o.stdout).into_owned().as_ref()
+ &String::from("\n");
}
result
}
Err(_) => "fail".to_string(),
}
}
/// Checks if the extension of a file (passed as a string) corresponds to a given string.
/// Case insensitive.
pub fn has_file_extension(path: &str, extension: &str) -> bool {
Path::new(path)
.extension()
.map_or(false, |ext| ext.eq_ignore_ascii_case(extension))
}
/// Gets the unicode dot character coloured with a colour similar to the distro's branding
pub fn get_distro_img(distro: &str) -> String {
let distro_colours: HashMap<&str, &str> = HashMap::from([
("alma", "#dadada"),
("alpine", "#2147ea"),
("amazon", "#de5412"),
("arch", "#12aaff"),
("centos", "#ff6600"),
("clearlinux", "#56bbff"),
("crystal", "#8839ef"),
("debian", "#da5555"),
("deepin", "#0050ff"),
("fedora", "#3b6db3"),
("gentoo", "#daaada"),
("kali", "#000000"),
("mageia", "#b612b6"),
("mint", "#6fbd20"),
("neon", "#27ae60"),
("opensuse", "#daff00"),
("oracle", "#ff0000"),
("redhat", "#ff6662"),
("rhel", "#ff6662"),
("rocky", "#91ff91"),
("slackware", "#6145a7"),
("ubuntu", "#FF4400"),
("vanilla", "#7f11e0"),
("void", "#abff12"),
]);
if distro_colours.contains_key(distro) {
return format!("<span foreground=\"{}\">⬤</span>", distro_colours[distro]);
}
format!("<span foreground=\"{}\">⬤</span>", "#000000")
}
/// Returns a vector of distros which can install .deb packages
pub fn get_deb_distros() -> Vec<String> {
vec![
"debian".to_owned(),
"deepin".to_owned(),
"mint".to_owned(),
"ubuntu".to_owned(),
"kali".to_owned(),
"neon".to_owned(),
]
}
/// Returns a vector of distros which can install .rpm packages
pub fn get_rpm_distros() -> Vec<String> {
vec![
"centos".to_owned(),
"alma".to_owned(),
"rocky".to_owned(),
"fedora".to_owned(),
"opensuse".to_owned(),
"oracle".to_owned(),
"redhat".to_owned(),
"rhel".to_owned(),
]
}
/// Returns a vector of the user's distroboxes which can install .deb packages
pub fn get_my_deb_boxes() -> Vec<String> {
let my_boxes = get_all_distroboxes();
let deb_distros = get_deb_distros();
let mut my_deb_boxes = Vec::<String>::new();
for dbox in my_boxes {
if deb_distros.contains(&dbox.distro) {
my_deb_boxes.push(dbox.name);
}
}
my_deb_boxes
}
/// Returns a vector of the user's distroboxes which can install .rpm packages
pub fn get_my_rpm_boxes() -> Vec<String> {
let my_boxes = get_all_distroboxes();
let rpm_distros = get_rpm_distros();
let mut my_rpm_boxes = Vec::<String>::new();
for dbox in my_boxes {
if rpm_distros.contains(&dbox.distro) {
my_rpm_boxes.push(dbox.name);
}
}
my_rpm_boxes
}
/// Whether or not the `distrobox` command can be successfully run
pub fn has_distrobox_installed() -> bool {
let output = get_command_output("which", Some(&["distrobox"]));
if output.contains("no distrobox in") || output.is_empty() {
return false;
}
true
}
/// Whether or not the `podman` or `docker` command can be successfully run
pub fn has_podman_or_docker_installed() -> bool {
let output = get_command_output("which", Some(&["podman"]));
if output.contains("no podman in") || output.is_empty() {
let docker_output = get_command_output("which", Some(&["docker"]));
if docker_output.contains("no docker in") || docker_output.is_empty() {
return false;
}
}
true
}
/// Returns a Vec of `TerminalOption`s representing all terminals supported by `BoxBuddy`
pub fn get_supported_terminals() -> Vec<TerminalOption> {
vec![
TerminalOption {
name: String::from("GNOME Console"),
executable_name: String::from("kgx"),
separator_arg: String::from("--"),
flatpak_id: None,
},
TerminalOption {
name: String::from("GNOME Terminal"),
executable_name: String::from("gnome-terminal"),
separator_arg: String::from("--"),
flatpak_id: None,
},
TerminalOption {
name: String::from("Konsole"),
executable_name: String::from("konsole"),
separator_arg: String::from("-e"),
flatpak_id: Some(String::from("org.kde.konsole")),
},
TerminalOption {
name: String::from("Xfce Terminal"),
executable_name: String::from("xfce4-terminal"),
separator_arg: String::from("-x"),
flatpak_id: None,
},
TerminalOption {
name: String::from("Tilix"),
executable_name: String::from("tilix"),
separator_arg: String::from("-e"),
flatpak_id: None,
},
TerminalOption {
name: String::from("Kitty"),
executable_name: String::from("kitty"),
separator_arg: String::from("--"),
flatpak_id: None,
},
TerminalOption {
name: String::from("Alacritty"),
executable_name: String::from("alacritty"),
separator_arg: String::from("-e"),
flatpak_id: None,
},
TerminalOption {
name: String::from("WezTerm"),
executable_name: String::from("wezterm"),
separator_arg: String::from("-e"),
flatpak_id: Some(String::from("org.wezfurlong.wezterm")),
},
TerminalOption {
name: String::from("Ghostty"),
executable_name: String::from("ghostty"),
separator_arg: String::from("-e"),
flatpak_id: None,
},
TerminalOption {
name: String::from("elementary Terminal"),
executable_name: String::from("io.elementary.terminal"),
separator_arg: String::from("--"),
flatpak_id: None,
},
TerminalOption {
name: String::from("Ptyxis"),
executable_name: String::from("ptyxis"),
separator_arg: String::from("--"),
flatpak_id: Some(String::from("app.devsuite.Ptyxis")),
},
TerminalOption {
name: String::from("Foot"),
executable_name: String::from("footclient"),
separator_arg: String::from("-e"),
flatpak_id: None,
},
TerminalOption {
name: String::from("Terminator"),
executable_name: String::from("terminator"),
separator_arg: String::from("-x"),
flatpak_id: None,
},
TerminalOption {
name: String::from("Deepin Terminal"),
executable_name: String::from("deepin-terminal"),
separator_arg: String::from("-e"),
flatpak_id: None,
},
TerminalOption {
name: String::from("Xterm"),
executable_name: String::from("xterm"),
separator_arg: String::from("-e"),
flatpak_id: None,
},
TerminalOption {
name: String::from("COSMIC Terminal"),
executable_name: String::from("cosmic-term"),
separator_arg: String::from("-e"),
flatpak_id: None,
},
]
}
/// Returns the executable command and separator arg for the terminal which
/// `BoxBuddy` will spawn. First tries to find the Preferred Terminal, if set,
/// then loops through all options in order if it can't.
/// Returns a tuple of the terminal exec, the terminal separator arg, and a boolean
/// of whether this terminal is a flatpak.
/// If the terminal IS a flatpak, the first tuple element will be the flatpak
/// ID, but if it's NOT a flatpak it will be the executable name
/// Returns two empty strings if no supported terminal can be detected
pub fn get_terminal_and_separator_arg() -> (String, String, bool) {
let settings = Settings::new(APP_ID);
let chosen_term = settings.string("default-terminal");
// first iter through supported terms and find the exec name of their default
let supported_terminals = get_supported_terminals();
let mut chosen_term_obj = &supported_terminals[0];
for term in &supported_terminals {
if term.name == chosen_term {
chosen_term_obj = term;
break;
}
}
let mut output = get_command_output("which", Some(&[&chosen_term_obj.executable_name]));
let mut potential_error_msg = format!("no {} in", chosen_term_obj.executable_name);
// if their chosen term is available, return its details
if !output.contains(&potential_error_msg) && !output.is_empty() {
return (
chosen_term_obj.executable_name.clone(),
chosen_term_obj.separator_arg.clone(),
false,
);
}
// if their term is NOT available, check if it is a flatpak
if chosen_term_obj.flatpak_id.is_some() {
let user_flatpaks = get_users_supported_terminal_flatpaks();
if user_flatpaks.contains(&chosen_term_obj.flatpak_id.as_ref().unwrap()) {
return (
chosen_term_obj.flatpak_id.as_ref().unwrap().clone(),
chosen_term_obj.separator_arg.clone(),
true,
);
}
}
// if chosen term is NOT available at all, iter through list as before
for term in &supported_terminals {
output = get_command_output("which", Some(&[&term.executable_name]));
potential_error_msg = format!("no {} in", term.executable_name);
if !output.contains(&potential_error_msg) && !output.is_empty() {
return (
term.executable_name.clone(),
term.separator_arg.clone(),
false,
);
}
}
(String::new(), String::new(), false)
}
/// Returns a single string of a bullet-pointed list of supported terminals
/// for display to the user if no supported terminal is found.
pub fn get_supported_terminals_list() -> String {
let terms = get_supported_terminals();
terms
.iter()
.map(|t| format!("- {}", t.name))
.collect::<Vec<String>>()
.join("\n")
}
/// Returns a Vec of flatpak IDs of any supported terminals which are installed
pub fn get_users_supported_terminal_flatpaks() -> Vec<String> {
// first check if they have flatpak at all
let mut has_fp_out = get_command_output("which", Some(&["flatpak"]));
if has_fp_out.contains("no flatpak in") || has_fp_out.is_empty() {
return Vec::new();
}
let output = get_command_output("flatpak", Some(&["list", "--columns=app"]));
let term_flatpak_ids: Vec<String> = get_supported_terminals()
.iter()
.map(|t| &t.flatpak_id)
.filter(|f| f.is_some())
.map(|t| t.as_ref().unwrap().clone())
.collect();
let mut user_flatpak_terms = Vec::<String>::new();
for line in output.lines() {
let line_string = String::from(line.trim());
if term_flatpak_ids.contains(&line_string) {
user_flatpak_terms.push(line_string);
}
}
user_flatpak_terms
}
/// Returns "podman" or "docker", based on which is installed, for use by
/// `get_repository_list` below
pub fn get_container_runtime() -> String {
let mut runtime = String::from("podman");
let output = get_command_output("which", Some(&["podman"]));
if output.contains("no podman in") || output.is_empty() {
runtime = String::from("docker");
}
runtime
}
/// Gets CPU and Memory used for each box.
/// In here instead of Distrobox Handler because we have
/// to shell out to the actual runtime.
pub fn get_cpu_and_mem_usage(box_name: &str) -> CpuMemUsage {
let runtime = get_container_runtime();
let stats_output = get_command_output_no_err(
&runtime,
Some(&[
"stats",
box_name,
"--no-stream",
"--format",
"{{.CPUPerc}};{{.MemPerc}};{{.MemUsage}}",
]),
);
let output_pieces: Vec<&str> = stats_output.split(';').collect();
if output_pieces.len() != 3 {
// We failed to get the output for some reason
return CpuMemUsage {
cpu: String::new(),
mem: String::new(),
mem_percent: String::new(),
};
}
CpuMemUsage {
cpu: output_pieces[0].trim().to_string(),
mem: output_pieces[1].trim().to_string(),
mem_percent: output_pieces[2].trim().to_string(),
}
}
/// Returns a Vec of "image:version" strings for all container images already
/// downloaded. This is used to show the symbol next to downloaded container
/// images on the Image select when creating a new box
pub fn get_repository_list() -> Vec<String> {
let runtime = get_container_runtime();
// podman
let output = get_command_output(
&runtime,
Some(&["images", "--format=\"{{.Repository}}:{{.Tag}}\""]),
);
return output
.lines()
.map(|s| s.trim().replace('"', "").to_string())
.filter(|s| !s.is_empty())
.collect();
}
/// Whether or not `BoxBuddy` is running as a Flatpak
pub fn is_flatpak() -> bool {
let fp_env = std::env::var("FLATPAK_ID").is_ok();
if fp_env {
return true;
}
Path::new("/.flatpak-info").exists()
}
/// Whether or not the user appears to have an NVIDIA card, used to pass
/// the --nvidia flag when creating a new box.
pub fn is_nvidia() -> bool {
let which_lspci = get_command_output("which", Some(&["lspci"]));
if which_lspci.contains("no lspci") || which_lspci.is_empty() {
// cant detect hardware, assume no
return false;
}
let lspci_output = get_command_output("lspci", None);
let mut has_nvidia = false;
for line in lspci_output.lines() {
if line.contains("NVIDIA") {
has_nvidia = true;
break;
}
}
has_nvidia
}
/// Set up gettext
#[allow(unused_assignments)]
pub fn set_up_localisation() {
textdomain("boxbuddyrs").expect("failed to initialise gettext");
bind_textdomain_codeset("boxbuddyrs", "UTF-8").expect("failed to bind textdomain for gettext");
let language_code = env::var("LANG").unwrap_or_else(|_| "en_US".to_string());
let mut locale_directory = String::from("./po");
// --TRANSLATORS: Comment out the next 8 lines to test your development locale
if is_flatpak() {
locale_directory = String::from("/app/po");
} else {
let home_dir = env::var("HOME").unwrap_or_else(|_| ".".to_string());
let data_home =
env::var("XDG_DATA_HOME").unwrap_or_else(|_| format!("{home_dir}/.local/share"));
locale_directory = format!("{data_home}/locale");
}
let locale_directory_path = std::path::PathBuf::from(&locale_directory);
gettextrs::bindtextdomain("boxbuddyrs", locale_directory_path).expect("a");
setlocale(LocaleCategory::LcMessages, language_code);
}
/// Gets list of .desktop files on the host system which may have been exported from
/// a box. This is to determine whether to show the "Remove from Menu" button on the
/// View Applications pop-up
pub fn get_host_desktop_files() -> Vec<String> {
let mut host_apps: Vec<String> = Vec::<String>::new();
if is_flatpak() {
// we can't use fs in the flatpak sandbox, so parse `ls`.
let mut data_home = get_command_output("bash", Some(&["-c", "echo $XDG_DATA_HOME"]));
if data_home.trim().is_empty() {
let mut home_dir = get_command_output("bash", Some(&["-c", "echo $HOME"]));
home_dir = home_dir.trim().to_string();
data_home = format!("{home_dir}/.local/share");
}
let applications_dir = format!("{data_home}/applications");
let ls_lines = get_command_output("ls", Some(&[applications_dir.as_str()]));
let desktop_files = ls_lines.split('\n');
for df in desktop_files {
if !df.is_empty() {
host_apps.push(df.to_string());
}
}
} else {
let home_dir = env::var("HOME").unwrap_or_else(|_| ".".to_string());
let data_home =
env::var("XDG_DATA_HOME").unwrap_or_else(|_| format!("{home_dir}/.local/share"));
let applications_dir = format!("{data_home}/applications");
let applications_dir_path = Path::new(&applications_dir);
if applications_dir_path.exists() {
let my_apps = std::fs::read_dir(applications_dir_path);
if let Ok(apps) = my_apps {
for host_app in apps.flatten() {
if let Ok(fname) = host_app.file_name().into_string() {
host_apps.push(fname);
}
}
}
}
}
host_apps
}
/// Returns a struct which allows us to determine whether the user has added
/// a `home` or `host` Filesystem override to a Flatpak install.
/// This lets us disable features which won't work without these permissions.
pub fn get_flatpak_filesystem_permissions() -> FilesystemAccess {
let mut access = FilesystemAccess::new();
// this will check for BoxBuddy installed as a system flatpak
let sys_output = get_command_output(
"flatpak",
Some(&["override", "--show", "io.github.dvlv.boxbuddyrs"]),
);
for line in sys_output.split('\n') {
if line.starts_with("filesystems=") {
let fs_overrides = line.replace("filesystems=", "");
for ovr in fs_overrides.split(';') {
match ovr {
"host" => {
access.host = true;
}
"home" => {
access.home = true;
}
_ => {}
}
}
}
}
// check for BoxBuddy as a user flatpak
let user_output = get_command_output(
"flatpak",
Some(&["override", "--user", "--show", "io.github.dvlv.boxbuddyrs"]),
);
for line in user_output.split('\n') {
if line.starts_with("filesystems=") {
let fs_overrides = line.replace("filesystems=", "");
for ovr in fs_overrides.split(';') {
match ovr {
"host" => {
access.host = true;
}
"home" => {
access.home = true;
}
_ => {}
}
}
}
}
access
}
/// Returns whether or not the user has added a `host` Filesystem override.
pub fn has_host_access() -> bool {
if is_flatpak() {
let access = get_flatpak_filesystem_permissions();
return access.host;
}
true
}
/// Gets the path to icons which are not part of GTK
#[allow(unreachable_code)]
pub fn get_icon_file_path(icon: &str) -> String {
if is_flatpak() {
return format!("/app/icons/{icon}");
}
// Runs only when developing
debug_assert!({
return format!("icons/{icon}");
});
let home_dir = env::var("HOME").unwrap_or_else(|_| ".".to_string());
let data_home =
env::var("XDG_DATA_HOME").unwrap_or_else(|_| format!("{home_dir}/.local/share"));
format!("{data_home}/icons/boxbuddy/{icon}")
}
/// Get the path to the icon used in the Assemble button. Gets a light
/// or dark icon depending on the user's GTK theme.
pub fn get_assemble_icon() -> String {
if is_dark_mode() {
return get_icon_file_path("build-alt-symbolic-light.svg");
}
get_icon_file_path("build-alt-symbolic.svg")
}
/// Whether or not the user is using a Dark GTK theme
pub fn is_dark_mode() -> bool {
StyleManager::default().is_dark()
}
/// Tries to find the path to the user's Download dir.
pub fn get_download_dir_path() -> String {
env::var("XDG_DOWNLOAD_DIR").unwrap_or_else(|_| {
let home_dir = env::var("HOME");
if home_dir.is_err() {
return String::new();
}
let hme = home_dir.unwrap();
format!("{hme}/Downloads")
})
}