feat: add info command line subcommand (#1330)
* feat: add `info` command line subcommand Adding an info command allows the documentation to refer to it when mentioning platform specific information. This gives users a nicer experience since they don't need to think about how `ncspot` will behave on their system, but can run `ncspot info` to get that information. * fix: info command don't create runtime directory * fix: don't print runtime path on Windows Windows doesn't use the runtime path so it shouldn't be printed there. * fix: make `info` command easier to parse * docs: add back the default configuration directory
This commit is contained in:
34
src/utils.rs
34
src/utils.rs
@@ -72,20 +72,7 @@ pub fn create_runtime_directory() -> Result<PathBuf, Box<dyn std::error::Error>>
|
||||
os::unix::prelude::PermissionsExt,
|
||||
};
|
||||
|
||||
let linux_runtime_directory =
|
||||
PathBuf::from(format!("/run/user/{}/", unsafe { libc::getuid() }));
|
||||
let unix_runtime_directory = PathBuf::from("/tmp/");
|
||||
|
||||
let user_runtime_directory = if let Some(xdg_runtime_directory) = xdg_runtime_directory() {
|
||||
Some(xdg_runtime_directory.join("ncspot"))
|
||||
} else if cfg!(linux) && linux_runtime_directory.exists() {
|
||||
Some(linux_runtime_directory.join("ncspot"))
|
||||
} else if unix_runtime_directory.exists() {
|
||||
Some(unix_runtime_directory.join(format!("ncspot-{}", unsafe { libc::getuid() })))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
.ok_or("no runtime directory found")?;
|
||||
let user_runtime_directory = user_runtime_directory().ok_or("no runtime directory found")?;
|
||||
|
||||
let creation_result = fs::create_dir(&user_runtime_directory);
|
||||
|
||||
@@ -106,6 +93,25 @@ pub fn create_runtime_directory() -> Result<PathBuf, Box<dyn std::error::Error>>
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the path to the current user's runtime directory, or None if it couldn't be found.
|
||||
/// This function does not guarantee correct ownership or permissions of the directory.
|
||||
#[cfg(unix)]
|
||||
pub fn user_runtime_directory() -> Option<PathBuf> {
|
||||
let linux_runtime_directory =
|
||||
PathBuf::from(format!("/run/user/{}/", unsafe { libc::getuid() }));
|
||||
let unix_runtime_directory = PathBuf::from("/tmp/");
|
||||
|
||||
if let Some(xdg_runtime_directory) = xdg_runtime_directory() {
|
||||
Some(xdg_runtime_directory.join("ncspot"))
|
||||
} else if cfg!(linux) && linux_runtime_directory.exists() {
|
||||
Some(linux_runtime_directory.join("ncspot"))
|
||||
} else if unix_runtime_directory.exists() {
|
||||
Some(unix_runtime_directory.join(format!("ncspot-{}", unsafe { libc::getuid() })))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn xdg_runtime_directory() -> Option<PathBuf> {
|
||||
std::env::var("XDG_RUNTIME_DIR").ok().map(Into::into)
|
||||
|
||||
Reference in New Issue
Block a user