Skip to content

Commit 014c9c0

Browse files
Y7n05hErikBjare
andauthored
feat: add cmdline arg --webpath and use fallbacks when XDG_DATA_DIRS empty or not set (#262)
* feat: Add cmdline arg which can override asset path feat: fallback to /usr/local/share:/usr/share when XDG_DATA_DIRS is not set or empty * Update aw-server/src/main.rs Co-authored-by: Erik Bjäreholt <[email protected]> Co-authored-by: Erik Bjäreholt <[email protected]>
1 parent 63a2975 commit 014c9c0

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

Diff for: aw-server/src/main.rs

+27-11
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ struct Opts {
2929
/// Path to database override
3030
#[clap(long)]
3131
dbpath: Option<String>,
32+
/// Path to webui override
33+
#[clap(long)]
34+
webpath: Option<String>,
3235
/// Device ID override
3336
#[clap(long)]
3437
device_id: Option<String>,
@@ -77,7 +80,10 @@ async fn main() -> Result<(), rocket::Error> {
7780
};
7881
info!("Using DB at path {:?}", db_path);
7982

80-
let asset_path = get_asset_path();
83+
let asset_path = match opts.webpath {
84+
Some(webpath) => PathBuf::from(webpath),
85+
None => get_asset_path(),
86+
};
8187
info!("Using aw-webui assets at path {:?}", asset_path);
8288

8389
let legacy_import = !opts.no_legacy_import;
@@ -99,6 +105,7 @@ async fn main() -> Result<(), rocket::Error> {
99105
endpoints::build_rocket(server_state, config).launch().await
100106
}
101107

108+
use std::ffi::OsString;
102109
use std::path::PathBuf;
103110

104111
// The appdirs implementation of site_data_dir is broken on computers which has flatpak installed
@@ -110,16 +117,27 @@ use std::path::PathBuf;
110117
// don't want this change?
111118
fn site_data_dir(app: Option<&str>, _: Option<&str>) -> Result<PathBuf, ()> {
112119
// Iterate over all XDG_DATA_DIRS and return first match that exists
113-
if let Some(joined) = env::var_os("XDG_DATA_DIRS") {
114-
for mut data_dir in env::split_paths(&joined) {
115-
if let Some(app) = app {
116-
data_dir.push(app);
120+
let joined = match env::var_os("XDG_DATA_DIRS") {
121+
// If $XDG_DATA_DIRS is either not set or empty, a value equal to /usr/local/share/:/usr/share/ should be used.
122+
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
123+
Some(path) => {
124+
if path.is_empty() {
125+
OsString::from("/usr/local/share:/usr/share")
126+
} else {
127+
path
117128
}
118-
if !data_dir.is_dir() {
119-
continue;
120-
}
121-
return Ok(data_dir);
122129
}
130+
None => OsString::from("/usr/local/share:/usr/share"),
131+
};
132+
133+
for mut data_dir in env::split_paths(&joined) {
134+
if let Some(app) = app {
135+
data_dir.push(app);
136+
}
137+
if !data_dir.is_dir() {
138+
continue;
139+
}
140+
return Ok(data_dir);
123141
}
124142
// If no dirs exists in XDG_DATA_DIRS, fallback to /usr/local/share
125143
let default = "/usr/local/share";
@@ -139,8 +157,6 @@ fn site_data_dir(app: Option<&str>, _: Option<&str>) -> Result<PathBuf, ()> {
139157
fn get_asset_path() -> PathBuf {
140158
use std::env::current_exe;
141159

142-
// TODO: Add cmdline arg which can override asset path?
143-
144160
// Search order for asset path is:
145161
// 1. ./aw-webui/dist
146162
// 2. $current_exe_dir/aw_server_rust/static

0 commit comments

Comments
 (0)