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

feat: client_id_cmd config option #548

Merged
merged 5 commits into from
Aug 17, 2024
Merged
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
1 change: 1 addition & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ All configuration files should be placed inside the application's configuration
| Option | Description | Default |
| --------------------------------- | ---------------------------------------------------------------------------------------- | ------------------------------------------------------- |
| `client_id` | the Spotify client's ID | `65b708073fc0480ea92a077233ca87bd` |
| `client_id_command` | a shell command that prints the Spotify client ID to stdout (overrides `client_id`) | `None` |
| `client_port` | the port that the application's client is running on to handle CLI commands | `8080` |
| `tracks_playback_limit` | the limit for the number of tracks played in a **tracks** playback | `50` |
| `playback_format` | the format of the text in the playback's window | `{status} {track} • {artists}\n{album}\n{metadata}` |
Expand Down
4 changes: 2 additions & 2 deletions spotify_player/src/cli/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ fn try_connect_to_client(socket: &UdpSocket, configs: &config::Configs) -> Resul
let session = rt.block_on(new_session(&auth_config, false))?;

// create a Spotify API client
let client =
client::Client::new(session, auth_config, configs.app_config.client_id.clone());
let client_id = configs.app_config.get_client_id()?;
let client = client::Client::new(session, auth_config, client_id);
rt.block_on(client.refresh_token())?;

// create a client socket for handling CLI commands
Expand Down
30 changes: 30 additions & 0 deletions spotify_player/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl Configs {
pub struct AppConfig {
pub theme: String,
pub client_id: String,
pub client_id_command: Option<Command>,

pub client_port: u16,

Expand Down Expand Up @@ -141,6 +142,26 @@ pub struct Command {
pub args: Vec<String>,
}

impl Command {
/// Execute a command, returning stdout if succeeded or stderr if failed
pub fn execute(&self, extra_args: Option<Vec<String>>) -> anyhow::Result<String> {
let mut args = self.args.clone();
args.extend(extra_args.unwrap_or_default());

let output = std::process::Command::new(&self.command)
.args(&args)
.output()?;

if !output.status.success() {
let stderr = std::str::from_utf8(&output.stderr)?.to_string();
anyhow::bail!(stderr);
}

let stdout = std::str::from_utf8(&output.stdout)?.to_string();
Ok(stdout)
}
}

#[derive(Debug, Deserialize, Serialize, ConfigParse, Clone)]
/// Application device configurations
pub struct DeviceConfig {
Expand Down Expand Up @@ -229,6 +250,7 @@ impl Default for AppConfig {
theme: "dracula".to_owned(),
// official Spotify web app's client id
client_id: "65b708073fc0480ea92a077233ca87bd".to_string(),
client_id_command: None,

client_port: 8080,

Expand Down Expand Up @@ -386,6 +408,14 @@ impl AppConfig {
..Default::default()
}
}

/// Returns stdout of `client_id_command` if set, otherwise it returns the the value of `client_id`
pub fn get_client_id(&self) -> Result<String> {
match self.client_id_command {
Some(ref cmd) => cmd.execute(None),
None => Ok(self.client_id.clone()),
}
}
}

/// gets the application's configuration folder path
Expand Down
3 changes: 2 additions & 1 deletion spotify_player/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ async fn start_app(state: &state::SharedState) -> Result<()> {
let session = auth::new_session(&auth_config, !state.is_daemon).await?;

// create a Spotify API client
let client = client::Client::new(session, auth_config, configs.app_config.client_id.clone());
let client_id = configs.app_config.get_client_id()?;
let client = client::Client::new(session, auth_config, client_id);
client.refresh_token().await?;

// initialize Spotify-related stuff
Expand Down
13 changes: 1 addition & 12 deletions spotify_player/src/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,7 @@ fn execute_player_event_hook_command(
cmd: &config::Command,
event: PlayerEvent,
) -> anyhow::Result<()> {
let mut args = cmd.args.clone();
args.extend(event.args());

let output = std::process::Command::new(&cmd.command)
.args(&args)
.output()?;

// running the player hook command failed, report the command's stderr as an error
if !output.status.success() {
let stderr = std::str::from_utf8(&output.stderr)?.to_string();
anyhow::bail!(stderr);
}
cmd.execute(Some(event.args()))?;

Ok(())
}
Expand Down
Loading