Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show supported features and audio backend in --version #1312

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ librespot-connect = { version = "0.4" }
toml = "0.7"
color-eyre = "0.6"
directories = "5.0.1"
once_cell = "1.20.2"

[target."cfg(unix)".dependencies]
daemonize = "0.5"
Expand Down
81 changes: 75 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use librespot_playback::{
dither::{mk_ditherer, DithererBuilder, TriangularDitherer},
};
use log::{error, info, warn};
use once_cell::sync::Lazy;
use serde::{de::Error, de::Unexpected, Deserialize, Deserializer};
use sha1::{Digest, Sha1};
use std::{fmt, fs, path::Path, path::PathBuf, str::FromStr};
Expand Down Expand Up @@ -332,34 +333,102 @@ impl From<AudioFormat> for LSAudioFormat {
}
}

#[derive(Debug, Default, StructOpt)]
pub static ABOUT_INFO: Lazy<String> = Lazy::new(format_features_info);

// Features functionality
fn format_features_info() -> String {
let features = get_enabled_features();
let backends = get_enabled_backends();

let features_str = if features.is_empty() {
"No features enabled".to_string()
} else {
features.join(", ")
};

// Get the version from the Cargo environment variable
let version = env!("CARGO_PKG_VERSION");

// Format the string with version under the title, and avoid repeating version elsewhere
format!(
"A Spotify daemon\nspotifyd {}\nfeatures: {}\naudio backends: {}",
version,
features_str,
backends.join(", ")
)
}

pub fn get_enabled_backends() -> Vec<&'static str> {
let backends = vec![
#[cfg(feature = "alsa_backend")]
"alsa",
#[cfg(feature = "pulseaudio_backend")]
"pulseaudio",
#[cfg(feature = "rodio_backend")]
"rodio",
#[cfg(feature = "portaudio_backend")]
"portaudio",
#[cfg(feature = "rodiojack_backend")]
"rodiojack",
#[cfg(feature = "pipe_backend")]
"pipe",
];

backends
}

pub fn get_enabled_features() -> Vec<&'static str> {
let mut features = Vec::new();

#[cfg(feature = "dbus_mpris")]
features.push("MPRIS");

#[cfg(feature = "dbus_keyring")]
features.push("keyring");

features
}

#[derive(Debug, StructOpt)]
#[structopt(
about = "A Spotify daemon",
about = &**ABOUT_INFO, // Use double dereference to get `&str`
author,
name = "spotifyd",
setting(AppSettings::ColoredHelp)
)]
pub struct CliConfig {
/// The path to the config file to use
// The path to the config file to use
#[structopt(long, value_name = "string")]
pub config_path: Option<PathBuf>,

/// If set, starts spotifyd without detaching
// If set, starts spotifyd without detaching
#[structopt(long)]
pub no_daemon: bool,

/// Prints more verbose output
// Prints more verbose output
#[structopt(long)]
pub verbose: bool,

/// Path to PID file.
// Path to PID file.
#[structopt(long)]
pub pid: Option<PathBuf>,

#[structopt(flatten)]
pub shared_config: SharedConfigValues,
}

impl Default for CliConfig {
fn default() -> Self {
CliConfig {
config_path: None,
no_daemon: false,
verbose: false,
pid: None,
shared_config: SharedConfigValues::default(),
}
}
}

// A struct that holds all allowed config fields.
// The actual config file is made up of two sections, spotifyd and global.
#[derive(Clone, Default, Deserialize, PartialEq, StructOpt)]
Expand Down
7 changes: 7 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::config::CliConfig;
use crate::config::ABOUT_INFO;
#[cfg(unix)]
use color_eyre::eyre::eyre;
use color_eyre::{
Expand Down Expand Up @@ -98,6 +99,12 @@ fn main() -> eyre::Result<()> {

color_eyre::install().wrap_err("Couldn't initialize error reporting")?;

// Handle --version explicitly
if std::env::args().any(|arg| arg == "--version") {
println!("{}", &**ABOUT_INFO);
return Ok(());
}

let mut cli_config: CliConfig = CliConfig::from_args();

let is_daemon = !cli_config.no_daemon;
Expand Down